Returns a new coll consisting of to-coll with all of the items of from-coll conjoined. A transducer may be supplied.
; Maps can be constructed from a sequence of 2-vectors or a sequence ; of maps user=> (into (sorted-map) [ [:a 1] [:c 3] [:b 2] ] ) {:a 1, :b 2, :c 3} user=> (into (sorted-map) [ {:a 1} {:c 3} {:b 2} ] ) {:a 1, :b 2, :c 3} ; When maps are the input source, they convert into an unordered sequence ; of key-value pairs, encoded as 2-vectors user=> (into [] {1 2, 3 4}) [[1 2] [3 4]]
; Items are conj'ed one at a time, which puts them at the head of ; the destination list user=> (into () '(1 2 3)) (3 2 1) ; This does not happen for a vector, however, due to the behavior of conj: user=> (into [1 2 3] '(4 5 6)) [1 2 3 4 5 6]
(defn test-key-inclusion-cols "return all values in column1 that aren't in column2" [column1 column2] (filter (complement (into #{} column2)) column1))
; Change from one type of map to another user=> (into (sorted-map) {:b 2 :c 3 :a 1}) {:a 1, :b 2, :c 3}
; Convert a nested ordering map to hash-map (or another) user=> (use 'flatland.ordered.map) user=> (def ord-map (ordered-map :a "a" :b "b" :c {:d "d" :e "e"})) user=> ord-map #ordered/map ([:a "a"] [:b "b"] [:c {:d "d", :e "e"}]) user=> (use 'clojure.walk) user=> (defn disorder [ordering-map map-fn] (postwalk #(if (map? %) (into map-fn %) %) ordering-map)) user=> (disorder ord-map {}) {:a "a", :b "b", :c {:d "d", :e "e"}}
;; How do we use a transducer? ; Define the transducer with `comp` but in `->` order: (def xform (comp (map #(+ 2 %)) (filter odd?))) ; adds 2, then omits if result is even. (into [-1 -2] xform (range 10)) ; => [-1 -2 3 5 7 9 11] ; Alternatively, using `transduce` directly: (transduce xform conj [-1 -2] (range 10)) ; => [-1 -2 3 5 7 9 11] ; Alternatively, using reduce and explicitly calling `map` and `filter`: (reduce conj [-1 -2] (->> (range 10) (map #(+ 2 %)) (filter odd?))) ; => [-1 -2 3 5 7 9 11] ;; Let's benchmark, using Criterium (https://github.com/hugoduncan/criterium) (require '[criterium.core :refer [quick-bench]]) (quick-bench (into [-1 -2] xform (range 1000000))) ; Execution time lower quantile : 54.368948 ms ( 2.5%) ; Execution time upper quantile : 55.976303 ms (97.5%) (quick-bench (transduce xform conj [-1 -2] (range 1000000))) ; Execution time lower quantile : 77.738505 ms ( 2.5%) ; Execution time upper quantile : 87.088016 ms (97.5%): 1.5x slower than into (quick-bench (reduce conj [-1 -2] (->> (range 1000000) (map #(+ 2 %)) (filter odd?)))) ; Execution time lower quantile : 92.607522 ms ( 2.5%) ; Execution time upper quantile : 100.426780 ms (97.5%): 1.8x slower than into
;; Interesting case you can't directly convert list or sequence into map (due performance reasons). One should use vector instead. ;; This is ok: (into {} [[:a "a"] [:b "b"]]) ;;=> {:a "a", :b "b"} ;; But this isn't: (into {} ['(:a "a") '(:b "b")]) ;;=> ClassCastException clojure.lang.Keyword cannot be cast to java.util.Map$Entry clojure.lang.ATransientMap.conj (ATransientMap.java:44)
;; merging two arrays using the transducer `cat` (into [] cat [[1 2 3 ] [4 5 6 ]]) ;=> [1 2 3 4 5 6] (into '() cat [[1 2 3 ] [4 5 6 ]]) ;=> (6 5 4 3 2 1) (into '() [1 2 3 4 5 6]) ;=> (6 5 4 3 2 1)
;Into is useful to concatenate vectors of maps (def a [{:a 1 :b 2} {:a 3 :b 4}]) (def b [{:c "c" :d "d"} {:c "cc" :d "dd"}]) (into a b) ;=> [{:a 1, :b 2} {:a 3, :b 4} {:c "c", :d "d"} {:c "cc", :d "dd"}] ;concat will return a similar result, but will be a lazy-seq instead of a vector
;; Java 9 introduced immutable collections constructed with static 'of' methods ;; These collections have specific implementation details and are used usually along with Java streams ;; They cannot be used as operators like this (a-map :key) ;; Use 'into' to convert them to Clojure collections with improved utility (into [] (java.util.List/of :a :b :c)) ;=> [:a :b :c] (into #{} (java.util.Set/of :a :b :c)) ;=> #{:c :b :a} (into {} (java.util.Map/of :a "aa" :b "bb" :c "cc")) ;=> {:c "cc", :b "bb", :a "aa"}
; into will return the coll type of the first parameter (def list-example '(1 2 3 4)) ;=> #'list-example (def vector-example [1 2 3 4]) ;=> #'vector-example (def set-example #{1 2 3 4}) ;=> #'set-example (class (into list-example vector-example)) ;=> clojure.lang.PersistentList (class (into vector-example list-example)) ;=> clojure.lang.PersistentVector (class (into set-example list-example)) ;=> clojure.lang.PersistentHashSet
conj[oin]. Returns a new collection with the xs 'added'. (conj nil item) returns (item). (co...
Returns a lazy seq representing the concatenation of the elements in the supplied colls.
assoc[iate]. When applied to a map, returns a new map of the same (hashed/sorted) type, that con...
into