Skip to content

Instantly share code, notes, and snippets.

@rxacevedo
Created July 23, 2014 17:40
Show Gist options
  • Select an option

  • Save rxacevedo/6cb89e07f700b9c30bdc to your computer and use it in GitHub Desktop.

Select an option

Save rxacevedo/6cb89e07f700b9c30bdc to your computer and use it in GitHub Desktop.
Monoids, reducers, map, etc.
(require '[clojure.core.reducers :as rd])
;; => nil
(letfn [(monoid-mapping [f]
(rd/monoid
(fn [l r] (conj l (f r))) vector))
(monoid-reducer-map [f coll]
(reduce (monoid-mapping f)
((monoid-mapping f))
coll))]
(do
(println (monoid-reducer-map inc (range 10)))
(println (monoid-reducer-map #(.toUpperCase %) ["wind it up"
"f*** you and yo bassline"
"turn down for what"]))))
; [1 2 3 4 5 6 7 8 9 10]
; [WIND IT UP F*** YOU AND YO BASSLINE TURN DOWN FOR WHAT]
;; => nil
@rxacevedo
Copy link
Author

Doesn't work gracefully on maps because monoid-mapping uses vector as the identity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment