Skip to content

Instantly share code, notes, and snippets.

@glucero
Last active December 15, 2015 13:09
Show Gist options
  • Save glucero/5265091 to your computer and use it in GitHub Desktop.
Save glucero/5265091 to your computer and use it in GitHub Desktop.
using the robin to compose clojure map functions to look like ruby map functions

The Robin

λxyz.yzx

Clojure

(fn [x] (fn [y] (fn [z] ((y z) x)))

Because Clojure prefers variadic functions to curried functions, we need to make our own curried-map function

(defn cmap [x] (partial map x))

So if our Ruby map function call example looks like this:

(main) > [1, 2, 3, 4, 5].map do |num|
       *   sqr = num * num
       *   puts "The square of #{num} is #{sqr}"
       *   sqr
       * end
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
=> [1, 4, 9, 16, 25]

Using cmap and the Robin, our Clojure function looks just like the Ruby function call:

#'user=> ((((fn [x] (fn [y] (fn [z] ((y z) x))))
    #_=>
    #_=> [1 2 3 4 5]) cmap) (fn [num]
    #_=>   (let [sqr (* num num)]
    #_=>   (println "The square of " num "is " sqr)
    #_=>   sqr
    #_=> )))
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
The square of 4 is 16
The square of 5 is 25
(1 4 9 16 25)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment