Back
take (clj)
(source)function
(take n)
(take n coll)
Returns a lazy sequence of the first n items in coll, or all items if
there are fewer than n. Returns a stateful transducer when
no collection is provided.
Examples
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))
(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 [])))))
logseq/logseq
(ns frontend.pubsub
"All mults and pubs are collected to this ns.
vars with suffix '-mult' is a/Mult, use a/tap and a/untap on them. used by event subscribers
vars with suffix '-pub' is a/Pub, use a/sub and a/unsub on them. used by event subscribers
vars with suffix '-ch' is chan used by event publishers."
{:clj-kondo/config {:linters {:unresolved-symbol {:level :off}}}}
#?(:cljs (:require-macros [frontend.pubsub :refer [def-mult-or-pub chan-of]]))
(:require [clojure.core.async :as a :refer [chan mult pub]]
[clojure.core.async.impl.protocols :as ap]
[malli.core :as m]
[malli.dev.pretty :as mdp]
[clojure.pprint :as pp]))
;;; helper macro
(defmacro chan-of [malli-schema malli-schema-validator & chan-args]
`(let [ch# (chan ~@chan-args)]
(reify
ap/ReadPort
(~'take! [~'_ fn1-handler#]
(ap/take! ch# fn1-handler#))
ap/WritePort
(~'put! [~'_ val# fn1-handler#]
(if (~malli-schema-validator val#)
(ap/put! ch# val# fn1-handler#)
(do (mdp/explain ~malli-schema val#)
(throw (ex-info "validate chan value failed" {:val val#}))))))))
clojure/core.async
;; The clojure.core.async namespace contains the public API.
(require '[clojure.core.async :as async :refer :all])
;; `close!` a channel to stop accepting puts. Remaining values are still
;; available to take. Drained channels return nil on take. Nils may
;; not be sent over a channel explicitly!
;; In ordinary threads, we use `>!!` (blocking put) and `<!!`
;; (blocking take) to communicate via channels.
;; The `go` macro asynchronously executes its body in a special pool
;; of threads. Channel operations that would block will pause
;; execution instead, blocking no threads. This mechanism encapsulates
;; the inversion of control that is external in event/callback
;; systems. Inside `go` blocks, we use `>!` (put) and `<!` (take).
;; Instead of the explicit thread and blocking call, we use a go block
;; for the producer. The consumer uses a go block to take, then
;; returns a result channel, from which we do a blocking take.
;; We can create a background thread with alts that combines inputs on
;; either of two channels. `alts!!` takes a set of operations
;; to perform - either a channel to take from or a [channel value] to put
;; and returns the value (nil for put) and channel that succeeded:
hoplon/hoplon
(ns hoplon.binding
(:refer-clojure :exclude [binding bound-fn])
(:require [clojure.core :as clj]
[cljs.analyzer :as a]))
(defmacro binding
"See clojure.core/binding."
[bindings & body]
(let [env (assoc &env :ns (a/get-namespace a/*cljs-ns*))
value-exprs (take-nth 2 (rest bindings))
bind-syms (map #(:name (a/resolve-existing-var env %)) (take-nth 2 bindings))
bind-syms' (map (partial list 'quote) bind-syms)
set-syms (repeatedly (count bind-syms) gensym)
setfn (fn [x y]
{:push! `(fn []
(let [z# ~x]
(set! ~x ~y)
(fn [] (set! ~x z#))))})
thunkmaps (map setfn bind-syms set-syms)]
(a/confirm-bindings env bind-syms)
`(let [~@(interleave set-syms value-exprs)]
(hoplon.binding/push-thread-bindings ~(zipmap bind-syms' thunkmaps))
(try ~@body (finally (hoplon.binding/pop-thread-bindings))))))
clj-commons/manifold
(ns manifold.stream.async
{:no-doc true}
(:require
[manifold.deferred :as d]
[clojure.core.async :as a]
[manifold.stream
[graph :as g]
[core :as s]]
[manifold
[executor :as executor]
[utils :as utils]])
(:import
[java.util.concurrent.atomic
AtomicReference]))
(s/def-source CoreAsyncSource
[ch ^AtomicReference last-take]
(take [this default-val blocking?]
(if blocking?
(let [d (d/deferred)
d' (.getAndSet last-take d)
f (fn [_]
(a/take! ch
(fn [msg]
(d/success! d
(if (nil? msg)
(do
(.markDrained this)
default-val)
msg)))))]
(if (d/realized? d')
(f nil)
(d/on-realized d' f f))
d)))
(take [this default-val blocking? timeout timeout-val]
(let [d (d/deferred)
d' (.getAndSet last-take d)
;; if I don't take this out of the goroutine, core.async OOMs on compilation
mark-drained #(.markDrained this)
f (fn [_]
(a/go
(let [result (a/alt!
ch ([x] (if (nil? x)
(do
(mark-drained)
default-val)
x))
(a/timeout timeout) timeout-val
:priority true)]
(d/success! d result))))]
(if (d/realized? d')
(f nil)
(d/on-realized d' f f))
(if blocking?
@d
d))))
(assert (not (nil? x)) "core.async channel cannot take `nil` as a message")
(if (nil? timeout)
(.put this x blocking?)
(assert (not (nil? x)) "core.async channel cannot take `nil` as a message"))
hraberg/deuce
(ns deuce.emacs.indent
(:use [deuce.emacs-lisp :only (defun defvar)])
(:require [clojure.core :as c]
[deuce.emacs.buffer :as buffer]
[deuce.emacs.data :as data]
[deuce.emacs.editfns :as editfns])
(:refer-clojure :exclude []))
OFFSETS is either nil or a cons cell (HSCROLL . TAB-OFFSET).
HSCROLL is the number of columns not being displayed at the left
margin; this is usually taken from a window's hscroll member.
TAB-OFFSET is the number of columns of the first tab that aren't
being displayed, perhaps because the line was continued within it.
If OFFSETS is nil, HSCROLL and TAB-OFFSET are assumed to be zero.
LINES can optionally take the form (COLS . LINES), in which case
the motion will not stop at the start of a screen line but on
its column COLS (if such exists on that line, that is).