Today I am continuing with 3 more useful function concepts: How to define functions, anonymous functions and using apply with functions
Functions can be defined in two ways:
(defn name [params] (body))
(def name (fn [params] (body))
defn is a macro (which I get to some time in the future) doing both a def and fn
The next useful concept is anonymous functions which have a shortened form #() and parameters are specified with a % symbol. Example:
(#(println %) "Hello World!")
Hello World!
=> nil
You can also use the apply functions on function for each parameter, for example
(apply + '(1 2 3 4)) ; applies the + function on all the values in the list
=> 10
That's all on functions, tomorrow I will look at collections
(println "Bye 4 Now!")
No comments:
Post a Comment