Database Reference
In-Depth Information
1.41e+03 7.32e+02 3.72e+02
1.31e+04 6.64e+03 3.49e+03
3.02e+04 9.60e+03 6.90e+03
user=> ($= (first va-matrix) * 4)
A 1x3 matrix
-------------
3.28e+04 1.71e+04 8.22e+03
3.
Using this, we can build complex expressions, such as this expression that takes the
mean of the values in the irst row of the matrix:
user=> ($= (sum (first va-matrix)) /
(count (first va-matrix)))
4839.333333333333
4.
Or we can build expressions take the mean of each column, as follows:
user=> ($= (reduce plus va-matrix) / (count va-matrix))
A 1x3 matrix
-------------
9.19e+03 3.83e+03 2.25e+03
How it works…
Any time you're working with macros and you wonder how they work, you can always get
at their output expressions easily, so you can see what the computer is actually executing. The
tool to do this is macroexpand-1 . This expands the macro one step and returns the result.
It's sibling function, macroexpand , expands the expression until there is no macro expression
left. Usually, this is more than we want, so we just use macroexpand-1 .
Let's see what these macros expand into:
user=> (macroexpand-1 '($= 7 * 4))
(incanter.core/mult 7 4)
user=> (macroexpand-1 '($= 7 * 4 + 3))
(incanter.core/plus (incanter.core/mult 7 4) 3)
user=> (macroexpand-1 '($= 3 + 7 * 4))
(incanter.core/plus 3 (incanter.core/mult 7 4))
Here, we can see that the expression doesn't expand into Clojure's * or + functions, but it
uses Incanter's matrix functions, mult and plus , instead. This allows it to handle a variety of
input types, including matrices, intelligently.
Otherwise, it switches around the expressions the way we'd expect. Also, we can see by
comparing the last two lines of code that it even handles operator precedence correctly.
 
Search WWH ::




Custom Search