Created
July 7, 2011 09:23
-
-
Save nipra/1069171 to your computer and use it in GitHub Desktop.
fnil use case
This file contains 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
(defn plus | |
([x] ((fnil + 0) x)) | |
([x y] | |
((fnil + 0 0) x y)) | |
([x y z] | |
((fnil + 0 0 0) x y z)) | |
([x y z & more] | |
(reduce plus (plus x y z) more))) | |
;; user> (plus nil) | |
;; 0 | |
;; user> (plus 0) | |
;; 0 | |
;; user> (plus 1) | |
;; 1 | |
;; user> (plus 1 2 3 nil 5 nil 7 nil nil 12) | |
;; 30 | |
;; user> (plus 12 3 4 5) | |
;; 24 | |
(defn product | |
([x] ((fnil * 1) x)) | |
([x y] | |
((fnil * 1 1) x y)) | |
([x y z] | |
((fnil * 1 1 1) x y z)) | |
([x y z & more] | |
(reduce product (product x y z) more))) | |
;; user> (product nil 1 2 3 3) | |
;; 18 | |
;; user> (product nil) | |
;; 1 | |
;; user> (product 1) | |
;; 1 | |
;; user> (product 12) | |
;; 12 | |
;; user> (product 12 3 4) | |
;; 144 | |
;; user> (product 12 3 4 4) | |
;; 576 | |
;; user> (product 2 3 4 4 nil 2 nil) | |
;; 192 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment