This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-03-16
Channels
- # atlanta-clojurians (8)
- # beginners (103)
- # boot (22)
- # boot-dev (1)
- # cider (36)
- # cljs-dev (55)
- # cljsrn (3)
- # clojars (25)
- # clojure (104)
- # clojure-brasil (1)
- # clojure-dusseldorf (2)
- # clojure-italy (8)
- # clojure-norway (9)
- # clojure-russia (15)
- # clojure-spec (6)
- # clojure-uk (26)
- # clojurescript (246)
- # cursive (26)
- # data-science (1)
- # datomic (22)
- # dirac (11)
- # editors (1)
- # emacs (8)
- # fulcro (50)
- # graphql (11)
- # hoplon (1)
- # jobs-discuss (27)
- # leiningen (44)
- # luminus (33)
- # lumo (2)
- # mount (1)
- # off-topic (8)
- # onyx (5)
- # parinfer (4)
- # reagent (94)
- # ring-swagger (14)
- # shadow-cljs (37)
- # spacemacs (10)
- # specter (3)
- # tools-deps (4)
- # unrepl (14)
- # yada (5)
Hi - my first question to this channel, hope you all are doing well. I am processing some CSV input - fairly large file with four types of records in them. All records are grouped with a header line and they are separated with blank (empty) lines. I have come up with the following attempt to use Specter for the transformation:
(let [data (->>
;; my data comes in as text from a csv file
(csv/parse-csv (slurp "some-file-name.csv")
;; group by number of elements and remove group of empty rows
(dissoc (group-by count csv) 1)
;; remove trailing empty cells within records
(setval [MAP-VALS ALL LAST empty?] NONE)
;; prepare fields that will become map keys
(transform [MAP-VALS FIRST ALL] keify)
;; turn the groups into maps
(transform [MAP-VALS] sc/mappify)
;; lastly, read our data fields
(transform [MAP-VALS ALL ALL] read-field)
)]
(def a-records (get data 6))
(def b-records (get data 7))
(def c-records (get data 11))
(def d-records (get data 15)))
While this works for my immediate needs, I find myself navigating to MAP-VALS over and over again, thinking this can probably be done in a more elegant way. Is there a way in Specter to group the four transformations (setval + 3 transforms) into a sub-select of some sort? Should I even care about this type of optimization?@marcus165 There is multi-transform with multi-path to group setval and transform together.
(multi-transform [MAP-VALS (multi-path [ALL LAST empty? (terminal-val NONE)]
[FIRST ALL (terminal keify)]
[(terminal sc/mappify)]
[ALL ALL (terminal read-file)])])
awesome - thank you @drowsy!