Public Vars

Back

peek (clj)

(source)

function

(peek coll)
For a list or queue, same as first, for a vector, same as, but much more efficient than, last. If the collection is empty, returns nil.

Examples

clojure/core.typed
(ns clojure.core.typed.test.cljs-core
    (:require [cljs.core.typed :as t]
              [cljs.core :as core]
              [clojure.core.typed.test.cljs-utils :refer [is-tc-e tc-e]]
              [clojure.test :refer :all]))

(add-test1 peek
    [{:foo "bar" :baz "zot"} {:foo "bar"}] (t/Map core/Keyword t/JSString))
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
  "Type annotations for the base Clojure distribution."
  #?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
  (:require [clojure.core :as cc]
            [typed.clojure :as t]
            #?(:clj [typed.ann-macros.clojure :as macros])
            #?(:clj typed.ann.clojure.jvm) ;; jvm annotations
            #?(:clj clojure.core.typed))
  #?(:clj
     (:import (clojure.lang PersistentHashSet PersistentList
                            APersistentMap #_IPersistentCollection
                            #_ITransientSet
                            IRef)
              (java.util Comparator Collection))))

#?(:cljs (do
(t/ann-protocol cljs.core/Fn)
(t/ann-protocol cljs.core/IFn)
(t/ann-protocol cljs.core/ICloneable
                -clone [cljs.core/ICloneable :-> t/Any])
(t/ann-protocol [[x :variance :covariant]]
                cljs.core/IIterable
                ;; TODO
                -iterator [(cljs.core/IIterable x) :-> t/Any #_Object])
(t/ann-protocol cljs.core/ICounted
                -count [cljs.core/ICounted :-> t/Num])
(t/ann-protocol cljs.core/IEmptyableCollection
                ;; :/ TFn param on protocol?
                -empty [cljs.core/IEmptyableCollection :-> t/Any])
(t/ann-protocol cljs.core/ICollection
                -conj [cljs.core/ICollection t/Any :-> cljs.core/ICollection])
(t/ann-protocol [[x :variance :covariant]] cljs.core/IIndexed
                -nth (t/All [y]
                            [(cljs.core/IIndexed x) t/JSnumber (t/? y) :-> (t/U x y)]))
(t/ann-protocol cljs.core/ASeq)
(t/ann-protocol [[x :variance :covariant
                  ;;FIXME causes mystery error
                  ;:< t/AnyNilableNonEmptySeq
                  ]]
                cljs.core/ISeqable)
(t/ann-protocol [[x :variance :covariant]] cljs.core/ISeq
                -first [(cljs.core/ISeq x) :-> (t/Nilable x)]
                -rest [(cljs.core/ISeq x) :-> (cljs.core/ISeq x)])
;cljs.core/INext [[x :variance :covariant]]
(t/ann-protocol [[v :variance :covariant]] cljs.core/ILookup)
(t/ann-protocol [[k :variance :covariant]
                 [v :variance :covariant]]
                cljs.core/IAssociative
                -contains-key [(cljs.core/IAssociative k v) t/Any :-> t/Bool]
                -assoc (t/All [k1 v1] [(cljs.core/IAssociative k v) k1 v1 :->
                                       (cljs.core/IAssociative (t/U k k1) (t/U v v1))]))
(t/ann-protocol cljs.core/IMap
                -dissoc [cljs.core/IMap t/Any :-> cljs.core/IMap])
(t/ann-protocol [[k :variance :covariant]
                 [v :variance :covariant]]
                cljs.core/IMapEntry
                -key [(cljs.core/IMapEntry k v) :-> k]
                -val [(cljs.core/IMapEntry k v) :-> v])
(t/ann-protocol cljs.core/ISet
                -disjoin (t/All [s] [s t/Any :-> s]))
(t/ann-protocol [[x :variance :covariant]]
                cljs.core/IStack
                -peek [(cljs.core/IStack x) :-> (t/U nil x)]
                -pop [(cljs.core/IStack x) :-> (cljs.core/IStack x)])
(t/ann-protocol [[x :variance :covariant]]
                cljs.core/IVector
                -assoc-n (t/All [x1] [(cljs.core/IVector x) t/Num x1
                                      :-> (cljs.core/IVector (t/U x x1))]))
(t/ann-protocol [[x :variance :covariant]]
                cljs.core/IDeref
                -deref [(cljs.core/IDeref x) :-> x])
(t/ann-protocol [[x :variance :covariant]]
                cljs.core/IDerefWithTimeout
                -deref-with-timeout [(cljs.core/IDerefWithTimeout x) :-> x])
(t/ann-protocol cljs.core/IMeta
                -meta [cljs.core/IMeta :-> (t/Nilable (t/Map t/Any t/Any))])
(t/ann-protocol cljs.core/IWithMeta
                -with-meta [cljs.core/IWithMeta (t/Nilable (t/Map t/Any t/Any)) :-> cljs.core/IWithMeta])
;TODO
;cljs.core/IReduce [[]]
(t/ann-protocol cljs.core/IKVReduce ;;TODO
                -kv-reduce [cljs.core/IKVReduce [t/Any t/Any t/Any :-> t/Any] :-> t/Any])
(t/ann-protocol cljs.core/IList)
(t/ann-protocol cljs.core/IEquiv
                -equiv [cljs.core/IEquiv t/Any :-> t/Bool])
(t/ann-protocol cljs.core/IHash
                -hash [cljs.core/IHash :-> t/Num])
(t/ann-protocol cljs.core/ISequential)
(t/ann-protocol cljs.core/IRecord)
(t/ann-protocol [[x :variance :covariant]]
                cljs.core/IReversible
                -rseq [(cljs.core/IReversible x) :-> (t/NilableNonEmptySeq x)])
(t/ann-protocol [[k :variance :covariant]
                 [v :variance :covariant]] cljs.core/IFind
                -find [(cljs.core/IFind k v) t/Any :-> (t/Nilable (cljs.core/IMapEntry k v))]
                )
(t/ann-protocol [[x :variance :invariant]]
                cljs.core/ISorted
                -sorted-seq [(cljs.core/ISorted x) t/Bool :-> (t/Nilable (t/ASeq x))]
                ;; second arg => comparable?
                -sorted-seq-from [(cljs.core/ISorted x) t/Any t/Bool :-> (t/Nilable (t/ASeq x))]
                -entry-key [(cljs.core/ISorted x) t/Any :-> t/Any]
                -comparator [(cljs.core/ISorted x) :-> [x x :-> t/Num]])
(t/ann-protocol cljs.core/IPending)
;cljs.core/IWriter [[]]
;cljs.core/IPrintWithWriter [[]]
;    ;TODO
;;cljs.core/IWatchable [[]]
;    ;cljs.core/IEditableCollection [[]]
;    ;cljs.core/ITransientCollection [[]]
;    ;cljs.core/ITransientAssociative [[]]
;    ;cljs.core/ITransientMap [[]]
;    ;cljs.core/ITransientVector [[]]
;    ;cljs.core/ITransientSet [[]]
(t/ann-protocol [[x :variance :invariant]]
                cljs.core/IComparable)
;    ;cljs.core/IChunk [[]]
;    ;cljs.core/IChunkedSeq [[]]
;    ;cljs.core/IChunkedNext [[]]
(t/ann-protocol cljs.core/INamed
                -named [cljs.core/INamed :-> t/Str]
                -namespace [cljs.core/INamed :-> (t/Nilable t/Str)])
(t/ann-protocol [[x :variance :invariant]]
                cljs.core/IVolatile
                -vreset! (t/All [x] [(cljs.core/IVolatile x) x :-> x]))

cc/peek (t/All [x] (t/IFn [(t/I t/NonEmptyCount (t/Stack x)) :-> x]
                          [(t/Stack x) :-> (t/Nilable x)]))
;;TODO assert non-empty arg
cc/pop (t/All [x] (t/IFn [(t/List x) :-> (t/List x)]
                         [(t/Vec x) :-> (t/Vec x)]
                         [(t/Stack x) :-> (t/Stack x)]))
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure.jvm
  "JVM-specific type annotations for the base Clojure distribution."
  (:require [typed.clojure :as t]
            [typed.clojure.jvm :refer [override-classes]]
            clojure.core.typed)
  (:import (clojure.lang Named IMapEntry AMapEntry Seqable
                         LazySeq PersistentHashSet PersistentTreeSet PersistentTreeMap PersistentList APersistentVector
                         APersistentSet IPersistentSet IPersistentMap IPersistentVector
                         APersistentMap IDeref ISeq IPersistentCollection
                         ILookup Indexed Associative IPersistentStack PersistentVector Cons
                         IPersistentList IRef ARef Reversible
                         ITransientCollection ITransientSet ITransientAssociative ITransientMap
                         ITransientVector PersistentHashMap Reduced MultiFn Sorted)
           (java.util Collection RandomAccess)))

IPersistentStack [[[a :variance :covariant]
                   #_[Peek :variance :covariant]
                   ;;TODO "pop-able" parameter
                   #_[Pop :variance :covariant]
                   #_[Seq :variance :covariant]
                   ]
                  #_#_:methods {peek [-> Peek]
                                pop [-> Pop]}
                  :replace
                  {IPersistentCollection (IPersistentCollection a)
                   ;Seqable (Seqable (t/NilableNonEmptySeq a))
                   }]
sneerteam/sneer
(ns sneer.async-test
  (:require [clojure.core.async :refer [chan close!]]
            [midje.sweet :refer :all]
            [sneer.async :refer [state-machine sliding-chan go-trace peek-state!]]
            [sneer.test-util :refer [<!!? >!!? closes]]))

    (<!!? (peek-state! machine)) => 4242