Collecting cards without listing them

Is it possible to create collections mapwithout turning them into lists? I know about mapvfor vectors, but with regards to sets or maps. Is there a common function mapthat treats each collection as a functor (i.e., Something displayed) and saves its type after display?

Here is what I mean against what is happening now:

(map inc [1 2 3])         ; => [2 3 4], instead I get: (2 3 4)
(map inc #{1 2 3})        ; => #{3 2 4}, instead I get: (3 2 4)
(map inc {:a 1 :b 2 :c 3} ; => {:a 2 :b 3 :c 4}, instead I get: ClassCastException
+4
source share
2 answers

The return values ​​from your functions are not exact lists, but simply as they appear in REPL when printed. They really are examples clojure.lang.LazySeq, as you can see here:

(class (map inc [1 2 3]))
=> clojure.lang.LazySeq

mappable, Clojure Sequable. Seq , , (). Clojure .

, :

(-> (map inc [1 2 3])
    (vec))

, into:

(into [] (map inc [1 2 3]))

into , :

(into #{} (map inc #{1 2 3}))
=> #{4 3 2}

, Hashmap, map - clojure.lang.MapEntry, inc, , .

- , , :

(into {} (for [[k v] {:a 1 :b 2 :c 3}]
           [k (inc v)]))
+9

into . , (empty data) :

(def data #{1 2 3})
(into (empty data) (map inc data))
;; #{4 3 2}
0

Source: https://habr.com/ru/post/1677642/


All Articles