Last active
August 29, 2015 14:01
-
-
Save kenny-evitt/1abfd28cf193d487ceb4 to your computer and use it in GitHub Desktop.
Wrapping-function for a function with optional parameters
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 f [a b & c] | |
(* a (apply + `(~b ~@c)))) | |
(f 1 2 3) ; -> 5 | |
;; Wrapping the function | |
(defn wrapping-f [a b & c] | |
(apply f `(~a ~b ~@c))) | |
;; Masking the first parameter | |
(defn wrapping-f-1a [b & c] | |
(let [a 2] | |
(apply f `(~a ~b ~@c)))) | |
(defn wrapping-f-1a [b & c] | |
(let [a 2] | |
(apply (partial f a) `(~b ~@c)))) | |
(wrapping-f-1a 2 3) ; -> 10 | |
(wrapping-f-1b 2 3) ; -> 10 | |
;; Masking the second parameter | |
(defn wrapping-f-2 [a & c] | |
(let [b 3] | |
(apply f `(~a ~b ~@c)))) | |
(wrapping-f-2 1 3) ; -> 6 | |
(wrapping-f-2 2 3) ; -> 12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment