Monday, July 27, 2015

Useful functions, macros

I'm collecting here the useful functions mentioned in Brave Clojure, Chapter 8 and Chapter 9.

read-string: transforms a string to a data representation
eval: evaluastes a data representation; e.g.:
(eval (read-string "(+ 1 2 3)"))
-->
6
macroexpand: shows the data structure returned by a macro; e.g.
(macroexpand '(when true "Hello world"))
-->
(if true (do "Hello world"))

threading macro ->
Calls the function chain from left to right, i.e. passes the first element as the first parameter of function at second place, then the result of this is the parameter for the function at third place, etc. (c.f. thread-last macro, which does the same, but uses the first value as the last parameter of the next function)
gensym:
Produces unique symbols, with an optional prefix. N.B.: auto-gensym is also possible, with the # suffix.

No comments:

Post a Comment