Back
seq (clj)
(source)variable
(seq coll)
Returns a seq on the collection. If the collection is
empty, returns nil. (seq nil) returns nil. seq also works on
Strings, native Java arrays (of reference types) and any objects
that implement Iterable. Note that seqs cache values, thus seq
should not be used on any Iterable whose iterator repeatedly
returns the same mutable object.
Examples
clojure
(ns clojure.test-clojure.server
(:import java.util.Random)
(:require [clojure.test :refer :all])
(:require [clojure.core.server :as s]))
(deftest test-validate-opts
(check-invalid-opts {} "Missing required socket server property :name")
(check-invalid-opts {:name "a" :accept 'clojure.core/+} "Missing required socket server property :port")
(doseq [port [-1 "5" 999999]]
(check-invalid-opts {:name "a" :port port :accept 'clojure.core/+} (str "Invalid socket server port: " port)))
(check-invalid-opts {:name "a" :port 5555} "Missing required socket server property :accept"))
clojure
(ns clojure.test-clojure.reducers
(:require [clojure.core.reducers :as r]
[clojure.test.generative :refer (defspec)]
[clojure.data.generators :as gen])
(:use clojure.test))
(defmacro defequivtest
;; f is the core fn, r is the reducers equivalent, rt is the reducible ->
;; coll transformer
[name [f r rt] fns]
`(deftest ~name
(let [c# (range -100 1000)]
(doseq [fn# ~fns]
(is (= (~f fn# c#)
(~rt (~r fn# c#))))))))
(deftest test-mapcat-obeys-reduced
(is (= [1 "0" 2 "1" 3]
(->> (concat (range 100) (lazy-seq (throw (Exception. "Too eager"))))
(r/mapcat (juxt inc str))
(r/take 5)
(into [])))))
clojure
(deftest division
(is (= clojure.core// /))
(binding [*ns* *ns*]
(eval '(do (ns foo
(:require [clojure.core :as bar])
(:use [clojure.test]))
(is (= clojure.core// bar//))))))
(deftest Instants
(testing "Instants are read as java.util.Date by default"
(is (= java.util.Date (class #inst "2010-11-12T13:14:15.666"))))
(let [s "#inst \"2010-11-12T13:14:15.666-06:00\""]
(binding [*data-readers* {'inst read-instant-date}]
(testing "read-instant-date produces java.util.Date"
(is (= java.util.Date (class (read-string s)))))
(testing "java.util.Date instants round-trips"
(is (= (-> s read-string)
(-> s read-string pr-str read-string))))
(testing "java.util.Date instants round-trip throughout the year"
(doseq [month (range 1 13) day (range 1 29) hour (range 1 23)]
(let [s (format "#inst \"2010-%02d-%02dT%02d:14:15.666-06:00\"" month day hour)]
(is (= (-> s read-string)
(-> s read-string pr-str read-string))))))
(testing "java.util.Date handling DST in time zones"
(let [dtz (TimeZone/getDefault)]
(try
;; A timezone with DST in effect during 2010-11-12
(TimeZone/setDefault (TimeZone/getTimeZone "Australia/Sydney"))
(is (= (-> s read-string)
(-> s read-string pr-str read-string)))
(finally (TimeZone/setDefault dtz)))))
(testing "java.util.Date should always print in UTC"
(let [d (read-string s)
pstr (print-str d)
len (.length pstr)]
(is (= (subs pstr (- len 7)) "-00:00\"")))))
(binding [*data-readers* {'inst read-instant-calendar}]
(testing "read-instant-calendar produces java.util.Calendar"
(is (instance? java.util.Calendar (read-string s))))
(testing "java.util.Calendar round-trips"
(is (= (-> s read-string)
(-> s read-string pr-str read-string))))
(testing "java.util.Calendar remembers timezone in literal"
(is (= "#inst \"2010-11-12T13:14:15.666-06:00\""
(-> s read-string pr-str)))
(is (= (-> s read-string)
(-> s read-string pr-str read-string))))
(testing "java.util.Calendar preserves milliseconds"
(is (= 666 (-> s read-string
(.get java.util.Calendar/MILLISECOND)))))))
(let [s "#inst \"2010-11-12T13:14:15.123456789\""
s2 "#inst \"2010-11-12T13:14:15.123\""
s3 "#inst \"2010-11-12T13:14:15.123456789123\""]
(binding [*data-readers* {'inst read-instant-timestamp}]
(testing "read-instant-timestamp produces java.sql.Timestamp"
(is (= java.sql.Timestamp (class (read-string s)))))
(testing "java.sql.Timestamp preserves nanoseconds"
(is (= 123456789 (-> s read-string .getNanos)))
(is (= 123456789 (-> s read-string pr-str read-string .getNanos)))
;; truncate at nanos for s3
(is (= 123456789 (-> s3 read-string pr-str read-string .getNanos))))
(testing "java.sql.Timestamp should compare nanos"
(is (= (read-string s) (read-string s3)))
(is (not= (read-string s) (read-string s2)))))
(binding [*data-readers* {'inst read-instant-date}]
(testing "read-instant-date should truncate at milliseconds"
(is (= (read-string s) (read-string s2) (read-string s3))))))
(let [s "#inst \"2010-11-12T03:14:15.123+05:00\""
s2 "#inst \"2010-11-11T22:14:15.123Z\""]
(binding [*data-readers* {'inst read-instant-date}]
(testing "read-instant-date should convert to UTC"
(is (= (read-string s) (read-string s2)))))
(binding [*data-readers* {'inst read-instant-timestamp}]
(testing "read-instant-timestamp should convert to UTC"
(is (= (read-string s) (read-string s2)))))
(binding [*data-readers* {'inst read-instant-calendar}]
(testing "read-instant-calendar should preserve timezone"
(is (not= (read-string s) (read-string s2)))))))
(deftest preserve-read-cond-test
(let [x (read-string {:read-cond :preserve} "#?(:clj foo :cljs bar)" )]
(is (reader-conditional? x))
(is (not (:splicing? x)))
(is (= :foo (get x :no-such-key :foo)))
(is (= (:form x) '(:clj foo :cljs bar)))
(is (= x (reader-conditional '(:clj foo :cljs bar) false))))
(let [x (read-string {:read-cond :preserve} "#?@(:clj [foo])" )]
(is (reader-conditional? x))
(is (:splicing? x))
(is (= :foo (get x :no-such-key :foo)))
(is (= (:form x) '(:clj [foo])))
(is (= x (reader-conditional '(:clj [foo]) true))))
(is (thrown-with-msg? RuntimeException #"No reader function for tag"
(read-string {:read-cond :preserve} "#js {:x 1 :y 2}" )))
(let [x (read-string {:read-cond :preserve} "#?(:cljs #js {:x 1 :y 2})")
[platform tl] (:form x)]
(is (reader-conditional? x))
(is (tagged-literal? tl))
(is (= 'js (:tag tl)))
(is (= {:x 1 :y 2} (:form tl)))
(is (= :foo (get tl :no-such-key :foo)))
(is (= tl (tagged-literal 'js {:x 1 :y 2}))))
(testing "print form roundtrips"
(doseq [s ["#?(:clj foo :cljs bar)"
"#?(:cljs #js {:x 1, :y 2})"
"#?(:clj #clojure.test_clojure.reader.TestRecord [42 85])"]]
(is (= s (pr-str (read-string {:read-cond :preserve} s)))))))
clojure/core.typed
(ns ^:skip-wiki clojure.core.typed.ann.clojure
"Type annotations for the base Clojure distribution."
(:require [#?(:clj clojure.core.typed
:cljs cljs.core.typed)
:refer [defalias] :as t]))
(defalias
^{:doc "A type that can be used to create a sequence of member type x."
:forms '[(Seqable t)]}
t/Seqable
(t/TFn [[x :variance :covariant]]
#?(:clj (clojure.lang.Seqable x)
:cljs (cljs.core/ISeqable x))))
(defalias
^{:doc "A non-empty lazy sequence of type t"
:forms '[(NonEmptyLazySeq t)]}
t/NonEmptyLazySeq
(t/TFn [[t :variance :covariant]]
(t/I (clojure.lang.LazySeq t)
t/NonEmptyCount)))
(defalias
^{:doc "A type that can be used to create a sequence of member type x
with count greater than 0."
:forms '[(NonEmptySeqable t)]}
t/NonEmptySeqable
(t/TFn [[x :variance :covariant]]
(t/I (t/Seqable x)
t/NonEmptyCount)))
(defalias
^{:doc "A type that can be used to create a sequence of member type x
with count 0."
:forms '[(EmptySeqable t)]}
t/EmptySeqable
(t/TFn [[x :variance :covariant]]
(t/I (t/Seqable x)
t/EmptyCount)))
(defalias
^{:doc "A persistent sequence of member type x."
:forms '[(Seq t)]}
t/Seq
(t/TFn [[x :variance :covariant]]
(clojure.lang.ISeq x)))
(defalias
^{:doc "A persistent sequence of member type x with count greater than 0."
:forms '[(NonEmptySeq t)]}
t/NonEmptySeq
(t/TFn [[x :variance :covariant]]
(t/I (t/Seq x)
t/NonEmptyCount)))
(defalias
^{:doc "A persistent sequence of member type x with count greater than 0, or nil."
:forms '[(NilableNonEmptySeq t)]}
t/NilableNonEmptySeq
(t/TFn [[x :variance :covariant]]
(t/Nilable
(t/NonEmptySeq x))))
(defalias
^{:doc "A sequential collection."
:forms '[Sequential]}
t/Sequential
clojure.lang.Sequential)
(defalias
^{:doc "A sequential, seqable collection. Seq's aren't always Sequential."
:forms '[(SequentialSeqable t)]}
t/SequentialSeqable
(t/TFn [[x :variance :covariant]]
(t/I t/Sequential
(t/Seqable x))))
(defalias
^{:doc "A Clojure sequential sequence. Seq's aren't always Sequential."
:forms '[(SequentialSeq t)]}
t/SequentialSeq
(t/TFn [[x :variance :covariant]]
(t/I t/Sequential
(clojure.lang.ISeq x))))
(defalias
^{:doc "A sequential seq returned from clojure.core/seq"
:forms '[(ASeq t)]}
t/ASeq
(t/TFn [[x :variance :covariant]]
(t/I (t/SequentialSeq x)
(Iterable x)
(java.util.Collection x)
(java.util.List x)
clojure.lang.IObj)))
(defalias
^{:doc "A sequential non-empty seq retured from clojure.core/seq"
:forms '[(NonEmptyASeq t)]}
t/NonEmptyASeq
(t/TFn [[x :variance :covariant]]
(t/I (t/ASeq x)
t/NonEmptyCount)))
(defalias
^{:doc "The result of clojure.core/seq."
:forms '[(NilableNonEmptyASeq t)]}
t/NilableNonEmptyASeq
(t/TFn [[x :variance :covariant]]
(t/Nilable
(t/NonEmptyASeq x))))
juxt/yada
(ns yada.async
(:require
[clojure.core.async :as a]
[manifold.stream.async]
[yada.body :refer [MessageBody to-body render-seq]])
(:import
[clojure.core.async Mult]
[clojure.core.async.impl.channels ManyToManyChannel]
[manifold.stream.async CoreAsyncSource]))
ManyToManyChannel
(to-body [ch representation] (render-seq ch representation))
(content-length [_] nil))
mikera/core.matrix
WARNING: because they lack efficient indexed access, sequences will perform badly for most
array operations. In general they should be converted to other implementations before use."
(:require [clojure.core.matrix.protocols :as mp]
[clojure.core.matrix.implementations :as imp]
#?(:clj [clojure.core.matrix.macros :refer [scalar-coerce error]]))
#?(:clj (:import [clojure.lang ISeq])
:cljs (:require-macros [clojure.core.matrix.macros :refer [scalar-coerce error]])))
(extend-protocol mp/PImplementation
ISeq
(implementation-key [m] :sequence)
(meta-info [m]
{:doc "Core.matrix implementation for Clojure ISeq objects"})
(new-vector [m length]
(mp/new-vector [] length))
(new-matrix [m rows columns]
(mp/new-matrix [] rows columns))
(new-matrix-nd [m dims]
(mp/new-matrix-nd [] dims))
(construct-matrix [m data]
(mp/coerce-param [] data))
(supports-dimensionality? [m dims]
true))
(extend-protocol mp/PIndexedAccess
ISeq
(get-1d [m x]
(scalar-coerce (nth m x)))
(get-2d [m x y]
(let [row (nth m x)]
(mp/get-1d row y)))
(get-nd [m indexes]
(if-let [indexes (seq indexes)]
(if-let [next-indexes (next indexes)]
(let [mv (nth m (first indexes))]
(mp/get-nd mv next-indexes))
(nth m (first indexes)))
m ;; TODO: figure out if this is a good return value? should it be an error?
)))
(extend-protocol mp/PSliceSeq
ISeq
(get-major-slice-seq [m] (vec m)))
(extend-protocol mp/PSliceSeq2
ISeq
(get-slice-seq [m dimension]
(let [ldimension (long dimension)]
(cond
(== ldimension 0) (mp/get-major-slice-seq m)
(< ldimension 0) (error "Can't get slices of a negative dimension: " dimension)
:else (mapv #(mp/get-slice m dimension %) (range (mp/dimension-count m dimension)))))))
(extend-protocol mp/PDimensionInfo
ISeq
(dimensionality [m]
(inc (mp/dimensionality (first m))))
(is-vector? [m]
(== 0 (mp/dimensionality (first m))))
(is-scalar? [m]
false)
(get-shape [m]
#?(:cljs (js/console.log (str "shape of seq: " m)))
(cons (count m) (mp/get-shape (first m))))
(dimension-count [m x]
(if (== x 0)
(count m)
(mp/dimension-count (first m) (dec x)))))
(extend-protocol mp/PFunctionalOperations
ISeq
(element-seq [m]
(if (== 0 (long (mp/dimensionality (first m))))
m ;; handle 1D case, just return this sequence unchanged
(mapcat mp/element-seq m)))
(element-map
([m f]
(mapv #(mp/element-map % f) m))
([m f a]
(let [[m a] (mp/broadcast-compatible m a)]
(mapv #(mp/element-map % f %2) m (mp/get-major-slice-seq a))))
([m f a more]
(let [[m a & more] (apply mp/broadcast-compatible m a more)] ; FIXME
(mapv #(mp/element-map % f %2 %3) m (mp/get-major-slice-seq a) (map mp/get-major-slice-seq more)))))
(element-map!
([m f]
(error "Sequence arrays are not mutable!"))
([m f a]
(error "Sequence arrays are not mutable!"))
([m f a more]
(error "Sequence arrays are not mutable!")))
(element-reduce
([m f]
(reduce f (mapcat mp/element-seq m)))
([m f init]
(reduce f init (mapcat mp/element-seq m)))))
(extend-protocol mp/PMapIndexed
ISeq
(element-map-indexed
([ms f]
(mapv (fn [i m] (mp/element-map-indexed m #(apply f (cons i %1) %&)))
(range (count ms)) ms))
([ms f as]
(let [[ms as] (mp/broadcast-compatible ms as)]
(mapv (fn [i m a]
(mp/element-map-indexed m #(apply f (cons i %1) %&) a))
(range (count ms)) ms (mp/get-major-slice-seq as))))
([ms f as more]
(let [[ms as & more] (apply mp/broadcast-compatible ms as more)] ; FIXME
(mapv (fn [i m a & mr]
(mp/element-map-indexed m #(apply f (cons i %1) %&) a mr))
(range (count ms)) ms
(mp/get-major-slice-seq as)
(map mp/get-major-slice-seq more)))))
(element-map-indexed!
([m f]
(error "Sequence arrays are not mutable!"))
([m f a]
(error "Sequence arrays are not mutable!"))
([m f a more]
(error "Sequence arrays are not mutable!"))))
hraberg/deuce
(ns deuce.emacs.marker
(:use [deuce.emacs-lisp :only (defun defvar) :as el])
(:require [clojure.core :as c]
[deuce.emacs.alloc :as alloc]
[deuce.emacs.buffer :as buffer]
[deuce.emacs.data :as data])
(:import [deuce.emacs.data Buffer BufferText Marker])
(:refer-clojure :exclude []))
(defun set-marker (marker position &optional buffer)
"Position MARKER before character number POSITION in BUFFER.
BUFFER defaults to the current buffer.
If POSITION is nil, makes marker point nowhere.
Then it no longer slows down editing in any buffer.
Returns MARKER."
(let [^Buffer buffer (el/check-type 'bufferp (or buffer (buffer/current-buffer)))
^Marker marker marker]
(when-let [^Buffer old-buffer @(.buffer marker)]
(swap! (.markers ^BufferText (.text old-buffer)) #(seq (remove #{marker} %))))
(reset! (.buffer marker) buffer)
(reset! (.charpos marker) position)
(swap! (.markers ^BufferText (.text buffer)) conj marker)
marker))
hraberg/deuce
(ns deuce.emacs.alloc
(:use [deuce.emacs-lisp :only (defun defvar) :as el]
[taoensso.timbre :as timbre
:only (trace debug info warn error fatal spy)])
(:require [clojure.core :as c]
[clojure.walk :as w]
[deuce.emacs-lisp.cons :as cons])
(:refer-clojure :exclude [vector cons list])
(:import [java.util Arrays]
[java.lang.management ManagementFactory MemoryNotificationInfo MemoryType MemoryPoolMXBean]
[javax.management NotificationListener NotificationEmitter Notification]))
(defun cons (car cdr)
"Create a new cons, give it CAR and CDR as components, and return it."
(cons/pair (cons/maybe-seq car) (cons/maybe-seq cdr)))
(defun purecopy (obj)
"Make a copy of object OBJ in pure storage.
Recursively copies contents of vectors and cons cells.
Does not copy symbols. Copies strings without text properties."
(cons/maybe-seq obj))