AI
Size: a a a
AI
ST
filter
and keep
. filter
returns the original items in a collection that satisfy the predicate. keep
returns the non-nil *results* of the given function.keep
is like a map
that filters out nil
values produced by f
and could be defined as:map
, keep
doesn’t accept multiple collections:clojure
(filter identity (map get [{:a 1} {:b 2} {:d 3}] [:a :b :c]))
; => (1 2)
(keep get [{:a 1} {:b 2} {:d 3}] [:a :b :c])
; => ArityException Wrong number of args (3) passed to: core/keep
VM
MK
(defmacro cond-let
([] nil)
([x] (throw (IllegalArgumentException. "cond-let error: last one form")))
([a b & forms] (if (vector? a)
`(let ~a (cond-let ~b ~@forms))
`(if ~a ~b (cond-let ~@forms)))))
ST
keep
возник из-за отсутствия аналога в хаскеле. Возможно, там это и не нужно, если композиция шагов обработки коллекций и ленивость выполнения бесплатны или незначительны с точки зрения производительности.(->> '(0 1 2 3 4 5 6 7 8 9) (map identity) (map identity) (last))) ; 2,90 µs (~1.5× slower)
(->> '(0 1 2 3 4 5 6 7 8 9) (map (comp identity identity)) (last)) ; 1,75 µs
filter`→`map`→`filter
, то очень хорошо, что это можно заменить на единичный keep
.mapv
/filterv
не использует (именно с точки зрения производительности, а не типа возвращаемого результата). А кто-то понимает, что ленивость в кложе не бесплатная, и где-то это приходится учитывать:(->> '(0 1 2 3 4 5 6 7 8 9) (mapv (comp identity identity)) (last)) ; 678,00 ns (~2.5× faster)
keep
нужен :-PST
T
T
T
ST
; If you define a defrecord in one namespace and want to use it
; from another, there are 2 options:
; 1. use the constructor (->Record)
; (only available in clojure >= 1.4)
;
; 2. first require the namespace and then import
; the record as a regular class.
; The require+import order makes sense if you consider that first
; the namespace has to be compiled--which generates a class for
; the record--and then the generated class must be imported.
; (Thanks to raek in #clojure for the explanations!)
DM
ST
dissoc-in
из книжкиAI
AI
keep
возник из-за отсутствия аналога в хаскеле. Возможно, там это и не нужно, если композиция шагов обработки коллекций и ленивость выполнения бесплатны или незначительны с точки зрения производительности.(->> '(0 1 2 3 4 5 6 7 8 9) (map identity) (map identity) (last))) ; 2,90 µs (~1.5× slower)
(->> '(0 1 2 3 4 5 6 7 8 9) (map (comp identity identity)) (last)) ; 1,75 µs
filter`→`map`→`filter
, то очень хорошо, что это можно заменить на единичный keep
.mapv
/filterv
не использует (именно с точки зрения производительности, а не типа возвращаемого результата). А кто-то понимает, что ленивость в кложе не бесплатная, и где-то это приходится учитывать:(->> '(0 1 2 3 4 5 6 7 8 9) (mapv (comp identity identity)) (last)) ; 678,00 ns (~2.5× faster)
keep
нужен :-PST
ST
AI
AI