Skip to content

Instantly share code, notes, and snippets.

View HerbM's full-sized avatar

Herb Martin HerbM

  • LearnQuick.Com
  • Austin, TX
View GitHub Profile
(defn rducts
([f aseq] (lazy-seq (rducts f (f (first aseq)) (rest aseq))))
([f accum aseq]
(lazy-seq (cons accum (let [fs (first aseq) rs (rest aseq)]
(if (not (seq rs))
(list (f accum fs))
(rducts f (f accum fs) rs)))))))
(take 5 (rducts + (range)))
(rducts conj [1] [2 3 4])
@HerbM
HerbM / tipe58.clj
Created September 4, 2016 15:05
4Clojure problem #58 Black box testing: Find type of set, map, vector, list without using built-in typing functions etc.
(defn tipe [s]
(let [sym1 (gensym) sym2 (gensym)
sym3 (gensym) s12 (conj s {sym1 sym2})
s1221 (conj (conj s {sym2 sym1}) s12)]
(if (get s12 sym1) :map
(if (get s12 {sym1 sym2}) :set
(if (= (first s1221) s12) :list :vector)))))
(= :map (tipe {:a 1, :b 2}))
(= :list (tipe (range (rand-int 20))))
@HerbM
HerbM / komp.clj
Created September 3, 2016 05:08
Re-implement Clojure "comp" for 4Clojure problem #58
(ns clojure4.core)
;;; Re-implement Clojure "comp" for 4Clojure problem #58 (as "komp")
(defn komp
([] (fn [n] (identity n)))
([fn1] (fn [& n] (apply fn1 n)) )
([fn1 fn2] (fn [& n]
(fn1 ((fn [m] (apply fn2 m)) n))))
([fn1 fn2 & fns] (fn [& n]
(fn1 ((fn [m] (apply (apply komp (conj fns fn2)) m )) n )))))