Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cjfrisz
Created September 6, 2013 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjfrisz/7f3e118098bb7a472e62 to your computer and use it in GitHub Desktop.
Save cjfrisz/7f3e118098bb7a472e62 to your computer and use it in GitHub Desktop.
;; Without helpers
(apply * (map inc (reduce + 0 [1 2 3 4 5])))
;; using ->>
(->> (reduce + 0 [1 2 3 4 5])
(map inc)
(apply *))
;; broken up using let
(let [reduced (reduce + 0 [1 2 3 4 5])
mapped (map inc reduced)]
(apply * mapped)
;; using as-> (best, in my opinion)
(as-> [1 2 3 4 5] num-vec
(reduce + 0 num-vec)
(map inc num-vec)
(apply * num-vec))
@sdegutis
Copy link

sdegutis commented Sep 6, 2013

(->> [1 2 3 4 5]
     (reduce + 0)
     (map inc)
     (apply *))

@brandonbloom
Copy link

(let [num-vec [1 2 3 4 5]]
  (reduce + 0 num-vec)
  (map inc num-vec)
  (apply * num-vec))

@brandonbloom
Copy link

But yeah, clearly ->> wins

@cjfrisz
Copy link
Author

cjfrisz commented Sep 6, 2013

Yeah, dude, that let don't work.

@danneu
Copy link

danneu commented Sep 6, 2013

@brandonbloom, I guess you meant:

(let [num-vec [1 2 3 4 5]
      num-vec (reduce + 0 num-vec)
      num-vec (map inc num-vec)]
  (apply * num-vec)))

I never even knew about as-> until now. But it's nice in that it doesn't depend on arg order like -> and ->>.

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