Skip to content

Instantly share code, notes, and snippets.

@tcrayford
Created May 14, 2010 20:25
Show Gist options
  • Save tcrayford/401618 to your computer and use it in GitHub Desktop.
Save tcrayford/401618 to your computer and use it in GitHub Desktop.
(defn my-count [coll]
(reduce (fn [n x] (inc n)) 0 coll))
(my-count [1 2 3 3])
(defn my-reverse [coll] ;;only works for vectors atm
(reduce (fn [xs x] (into [x] xs)) [] coll))
(my-reverse [1 2 3])
(defn my-map [f coll] ;;
(reduce (fn [xs x] (conj xs (f x))) [] coll))
(my-map inc [1 2 3])
(defn my-filter [pred coll]
(reduce (fn [xs x] (if (pred x)
(conj xs x)
xs))
[]
coll))
(my-filter even? [1 2 3])
(defn my-binary-comp [f g]
(fn [x] (f (g x))))
(defn my-comp [& fs]
(reduce my-binary-comp identity fs))
((my-comp inc inc inc) 1)
(defn foldl [f value coll]
(reduce f value (reverse coll)))
(defn my-reverse [coll]
(foldl (fn [xs x] (conj xs x)) [] coll))
(my-reverse [1 2 3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment