Created
January 1, 2012 01:28
-
-
Save amalloy/1545887 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
;; fixed indenting | |
(fn my-count [coll] | |
(let [my-count-plus (fn my-count-plus [coll accumulator] | |
(let [r (rest coll)] | |
(cond | |
(= '() coll) accumulator | |
:else (my-count-plus r (+ 1 accumulator)))))] | |
(my-count-plus coll 0))) | |
;; fixing newlines too | |
(fn my-count [coll] | |
(let [my-count-plus (fn my-count-plus [coll accumulator] | |
(let [r (rest coll)] | |
(cond (= '() coll) accumulator | |
:else (my-count-plus r (+ 1 accumulator)))))] | |
(my-count-plus coll 0))) | |
;; letfn instead of let..fn | |
(fn my-count [coll] | |
(letfn [(my-count-plus [coll accumulator] | |
(let [r (rest coll)] | |
(cond (= '() coll) accumulator | |
:else (my-count-plus r (+ 1 accumulator)))))] | |
(my-count-plus coll 0))) | |
;; if instead of cond | |
(fn my-count [coll] | |
(letfn [(my-count-plus [coll accumulator] | |
(let [r (rest coll)] | |
(if (= '() coll) | |
accumulator | |
(my-count-plus r (+ 1 accumulator)))))] | |
(my-count-plus coll 0))) | |
;; seq/nil? instead of =/() | |
(fn my-count [coll] | |
(letfn [(my-count-plus [coll accumulator] | |
(let [r (rest coll)] | |
(if (seq coll) | |
(my-count-plus r (+ 1 accumulator)) | |
accumulator)))] | |
(my-count-plus coll 0))) | |
;; destructuring instead of rest | |
(fn my-count [coll] | |
(letfn [(my-count-plus [coll accumulator] | |
(if-let [[_ & r] (seq coll)] | |
(my-count-plus r (+ 1 accumulator)) | |
accumulator))] | |
(my-count-plus coll 0))) | |
;; loop/recur instead of a named inner fn | |
(fn my-count [coll] | |
(loop [coll coll, accumulator 0] | |
(if-let [[_ & r] (seq coll)] | |
(recur r (+ 1 accumulator)) | |
accumulator))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment