Skip to content

Instantly share code, notes, and snippets.

@keyvanakbary
Last active August 29, 2015 14:02
Show Gist options
  • Save keyvanakbary/b30b29b0243cbb22229d to your computer and use it in GitHub Desktop.
Save keyvanakbary/b30b29b0243cbb22229d to your computer and use it in GitHub Desktop.
Delicious currying
; 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