Last active
August 29, 2015 14:02
-
-
Save keyvanakbary/b30b29b0243cbb22229d to your computer and use it in GitHub Desktop.
Delicious currying
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
; Tons of duplicated code :'( | |
(defn insert-l [a s l] | |
(cond | |
(empty? l) `() | |
(= (first l) s) (cons a l) | |
:else (cons (first l) (insert-l a s (rest l))))) | |
(defn insert-r [a s l] | |
(cond | |
(empty? l) `() | |
(= (first l) s) (concat l [a]) | |
:else (cons (first l) (insert-r a s (rest l))))) | |
; Currying! | |
(defn insert-g [se] | |
(fn [a s l] | |
(cond | |
(empty? l) `() | |
(= (first l) s) (se a l) | |
:else (cons (first l) ((insert-g se) a s (rest l)))))) | |
(def insert-r (insert-g (fn [a l] (cons a l)))) | |
(def insert-l (insert-g (fn [a l] (concat l [a])))) | |
; Usage | |
(insert-l "new" "three" `("one" "two" "three")) | |
; ("one" "two" "three" "new") | |
(insert-r "new" "three" `("one" "two" "three")) | |
; ("one" "two" "new" "three") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment