Open squest opened 8 years ago
loop sama for segitu gak ngaruhnya da di clojure??
Loop hampir ga pernah dipake di realworld programming. For clojure beda total sama for yg ada, for clojure itu kayak list-comprehension di haskell or python, tapi much more powerful.
Btw, loop & for itu macros bukan functions.
Based on 1 project gue yang lumayan kompleks, jumlah keywords yang paling banyak muncul di app gue sbb (sesuai urutan yang paling sering)
defn, let, dissoc, if, merge, when-let, into, if-let, doseq, assoc, dosync, every?, into, str, recur, nil?, mapv, fn, defrecord, select-keys, read-string, get-in, contains?, map, set, future, update-in, try, catch, ref-set, some, when, and, or...
Ya kira2x ini yang paling sering muncul di satu sample app gue yang lumayan kompleks.
Ini kode utk baca files di app + ngitung kemunculan keywords.
(defn read-those []
(->> (for [file files]
(slurp (str dir file)))
(apply concat)
(apply str)
(cs/split-lines)
(mapcat #(cs/split % #" "))
frequencies
(sort-by val)
(filter #(= \( (ffirst %)))))
;; dir is the directory
;; files is a list of files in that directory or sub-dirs
Elo bisa liat cara ngoding di clojure stylenya gitu, bisa langsung satu jalan with ->> macro
ffirst, map-indexed, keep, keep-indexed, (using #{} as lambda function in map/some/filter/remove), group-by, partition, partition-by, get-in, assoc-in, update-in, split-at, split-with, take-while, drop-while, drop-last, interleave, interpose, comp, juxt, partial, complement, zipmap, mapcat, take-last, reductions, merge, merge-with, select-keys, (map destructuring {:keys [some-key another-key]})