Quoting Alexei Sholik on the elixir-talk mailing list:
Normally, &1 only makes into a function the primitive expression it belongs to. In other words, it walks up the AST to the first parent and replaces that parent with a fn.
Expressions like this work just fine:
&1 * &1
&1 + 2
&1 * &2
But it can't be involved in a more complicated expression.
For instance, when you write:
&1 * &1 * &1
... you're writing something akin to:
fn x -> fn x -> x * x end * x end
There's discussion on elixir-core about whether the behavior of &1
needs to be modified to be less confusing in these cases.
To answer your specific question, though, you want something more like this:
cube = fn x -> x * x * x end
If you want to use &1
, you can use a simple expression with math.pow/2
:
cube = :math.pow(&1, 3)
... note that math.pow/2
always gives back a float.