Created
March 7, 2018 00:03
-
-
Save taxigy/adc857a196035e940a246ec74966c456 to your computer and use it in GitHub Desktop.
Haskell vs Clojure: function composition — Learn You a Haskell for Great Good!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; without composition | |
(repeat 2 (apply * (map #(* 3 %) (map max [1 2] [4 5])))) | |
;; with composition | |
((comp (partial repeat 2) | |
(partial apply *) | |
(partial map (partial * 3))) | |
(map max [1 2] [4 5])) | |
;; with thread macro | |
(->> (map max [1 2] [4 5]) | |
(map (partial * 3)) | |
(apply *) | |
(repeat 2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- without composition | |
replicate 2 (product (map (* 3) (zipWith max [1, 2] [4, 5]))) | |
-- with composition | |
replicate 2 . product . map (* 3) $ zipWith max [1, 2] [4, 5] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The trade-off being that haskell doesn't support multi-arity functions.
Of course, with clojure's macros, you can create new syntax. Something as simple as this:
let's you write:
(dot-comp map max [1 2] [4 5] . map (partial * 3) . apply * . repeat 2)
I also like that in the clojure example, the operations get applied in left->right order, as opposed to the haskell example's right->left ordering.
Learn you clojure for great good?