Created
September 11, 2012 07:26
-
-
Save tooky/3696665 to your computer and use it in GitHub Desktop.
Working through FP for OO Programers by @marick
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
;solution presented | |
(def a | |
(fn [type & args] | |
(apply type args))) | |
;my original solution | |
(def a | |
(fn [type & args] | |
(eval (cons type args)))) | |
;which I realised was the same as this | |
(def a | |
(fn [& args] | |
(eval args))) | |
;The solution presented in the book is definitely clearer as to what's going on. | |
;I'm wondering though if there's anything to be aware of about using eval like this? |
Probably a clearer example of how eval
is blind would just be this:
user=> (let [args [1]]
(eval '(inc args)))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: args in this context, compiling:(NO_SOURCE_PATH:169)
Urk. Pasted wrong thing:
user=> (let [x 1] (eval 'x))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: x in this context, compiling:(NO_SOURCE_PATH:171)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In this particular case, it doesn't matter. The big difference between
eval
andapply
is thateval
can't "see" symbols bound in alet
. Compare this:This doesn't become important until (I think) chapter 10.