This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-09-06
Channels
- # beginners (147)
- # boot (12)
- # chestnut (12)
- # cider (22)
- # clara (10)
- # cljs-dev (6)
- # cljs-experience (3)
- # cljsrn (12)
- # clojure (58)
- # clojure-austin (3)
- # clojure-dusseldorf (25)
- # clojure-finland (20)
- # clojure-gamedev (1)
- # clojure-greece (3)
- # clojure-italy (32)
- # clojure-new-zealand (5)
- # clojure-russia (12)
- # clojure-serbia (1)
- # clojure-spec (4)
- # clojure-uk (51)
- # clojurescript (75)
- # cursive (8)
- # datomic (81)
- # fulcro (29)
- # graphql (16)
- # heroku (6)
- # incanter (1)
- # keechma (1)
- # lumo (44)
- # off-topic (21)
- # onyx (22)
- # parinfer (5)
- # portkey (40)
- # re-frame (43)
- # reagent (5)
- # spacemacs (37)
- # specter (8)
- # unrepl (3)
Buondì!
La giornata comincia bene con la frustrazione di dovermi reimplementare map-vals
e map-keys
in ogni progetto
(Si, potrei usare una lib, ma non voglio dipendenze a caso, e dovrebbero davvero stare in core)
E.g. se voglio trasformare le keys di una map da stringhe a keywords l’interfaccia che voglio è (map-keys keyword {"a" 1 "b" 2})
@reborg reduce-kv
è un livello sotto, la usi effettivamente per implementare map-keys e map-vals
user> (def mappetta {"a" 1 "b" 2})
#'user/mappetta
user> (map keyword (keys mappetta))
(:a :b)
user>
@nilrecurring tu parli di una cosa del genere?
@kors no, per (map-keys keyword {"a" 1 "b" 2})
, out: {:a 1 :b 2}
@reborg in realtà c’è un bug aperto da un anno: https://dev.clojure.org/jira/browse/CLJ-1959
“Feature, Major”
@nilrecurring nel nostro codebase interno abbiamo qualcosa di simile per gestire i JSON in ingresso all'applicazione, basato su clojure.walk/postwalk
tradotto in quello che ti serve sarebbe
(defn map-keys
"Recursively translates keys of a map using f"
[f m]
(let [f (fn [[k v]] [(f k) v])]
(w/postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) m)))
user> (def _x {"a" [{"b" 42} {"c" 24}]})
#'user/_x
user> (map-keys keyword _x)
{:a [{:b 42} {:c 24}]}
Oh, molto pratico 🙂
L’avrei chiamata map-keys*
qui sono le 10AM