Skip to content

Instantly share code, notes, and snippets.

@tomjack
Created December 27, 2010 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomjack/f04db48b5f33d4810de2 to your computer and use it in GitHub Desktop.
Save tomjack/f04db48b5f33d4810de2 to your computer and use it in GitHub Desktop.
scratch.case-fact> (defmacro caseify [f n] `(fn [x#] (case x# ~@(mapcat (juxt identity (resolve f)) (range n)))))
#'scratch.case-fact/caseify
scratch.case-fact> (macroexpand-1 '(caseify fact 10))
(clojure.core/fn [x__9437__auto__] (clojure.core/case x__9437__auto__ 0 1 1 1 2 2 3 6 4 24 5 120 6 720 7 5040 8 40320 9 362880))
scratch.case-fact> (def digit-fact (caseify fact 10))
#'scratch.case-fact/digit-fact
scratch.case-fact> (digit-fact 9)
362880
scratch.case-fact> (def memo-fact (memoize fact))
#'scratch.case-fact/memo-fact
scratch.case-fact> (memo-fact 9)
362880
scratch.case-fact> (time (dotimes [_ 10000000] (memo-fact 9)))
"Elapsed time: 1464.514172 msecs"
nil
scratch.case-fact> (time (dotimes [_ 10000000] (digit-fact 9)))
"Elapsed time: 173.222382 msecs"
nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment