clojure.core/mapv¶
Returns a vector consisting of the result of applying f to the set of first items of each coll, followed by applying f to the set of second items in each coll, until any one of the colls is exhausted. Any remaining items in other colls are ignored. Function f should accept number-of-colls arguments.
Examples¶
Note by num1¶
Like map but returns a vector. In most cases equivalent to calling (into [] (map f c1 c2 ...)). Is much faster than map (but is not lazy!) in the (mapv f coll) case.
Note by KingMob¶
It's not necessarily true at all that mapv will be faster than map. mapv (and related fns like filterv) have increased memory needs (creating unused values, realizing intermediate sequences, etc) that hurt it.
Here's a simple example showing how mapv/filterv can be slower. (Benchmarked with criterium.)
(let [nums (range 10000000)]
(bench
(reduce +' 0 (filter even? (map #(* % %) nums))))
(bench
(reduce +' 0 (filterv even? (mapv #(* % %) nums)))))
map is actually a little faster than mapv:
Evaluation count : 120 in 60 samples of 2 calls.
Execution time mean : 768.870217 ms
Execution time std-deviation : 21.043586 ms
Execution time lower quantile : 756.049838 ms ( 2.5%)
Execution time upper quantile : 835.719808 ms (97.5%)
Overhead used : 1.805367 ns
Found 9 outliers in 60 samples (15.0000 %)
low-severe 3 (5.0000 %)
low-mild 6 (10.0000 %)
Variance from outliers : 14.2178 % Variance is moderately inflated by outliers
Evaluation count : 120 in 60 samples of 2 calls.
Execution time mean : 800.356084 ms
Execution time std-deviation : 49.667013 ms
Execution time lower quantile : 756.382597 ms ( 2.5%)
Execution time upper quantile : 883.471627 ms (97.5%)
Overhead used : 1.805367 ns
Found 12 outliers in 60 samples (20.0000 %)
low-severe 5 (8.3333 %)
low-mild 1 (1.6667 %)
high-mild 6 (10.0000 %)
Variance from outliers : 46.7536 % Variance is moderately inflated by outliers
The takeaway is, don't pick mapv just for speed reasons.
See also¶
- clojure.core/map (13)
- clojure.core/vec (1)
- clojure.core/reduce (22)
- clojure.core/pmap (3)
Content from the matching ClojureDocs page, with authors credited on each contribution.