iex(1)> &(&2 + 1)
error: capture argument &2 cannot be defined without &1 (you cannot skip arguments, all arguments must be numbered)
└─ iex:1
** (CompileError) cannot compile code (errors have been logged)
iex(1)> &(1)
error: capture argument &1 must be used within the capture operator &
└─ iex:1
** (CompileError) cannot compile code (errors have been logged)
iex(9)> fn () -> :ok end
#Function<43.105768164/0 in :erl_eval.expr/6>
iex(10)> fn (_a, b) -> b * 2 end
#Function<41.105768164/2 in :erl_eval.expr/6>
--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/g2tQI1FjcNU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/11ca2681-4b0e-4875-ac48-455a5e684f6en%40googlegroups.com.
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAAoaZy4gPokWC7urFA7sjqHzL%3DyVKdkHqGd6PM_Sc5o0MDtVGg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4K9SLYYFOU0dPAQ0uqAc7ms4Dq%2B8gPfX4Y0zY9uHJ4Frg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAAoaZy7-w%2BvyWY9t6v03wrfyMfc4XKy1UyEs0K7AtEedEGt5sQ%40mail.gmail.com.
Hi,
You could use a function that manipulates your function:
def shift_args(f), do: fn _x, y -> f(y) end
Now, with that, your proposed:
&(&2 + 1)
becomes
shift_args(&(&1 + 1))
This is just food for thought. In my opinion, it's often better to just resort to good old functions:
fn _, x -> x + 1 end
In Haskell, you get away with "end" due to indentation sensitivity, and they use the "\" to construct a lambda (it looks like Greek lambda letter :)
\_, x -> x + 1
Or uncurried:
\_ -> \x -> x + 1
So if at all, I would like of a way to reuse or "fn" literal without the verbose "end". That likely requires parenthesis, so you really would only save one character in the end.
You might also try to mess around with macros. So I think something like below could work:
f(_2 + 1, arity: 2)
The macro "f" would translate that into:
fn _, arg_2 -> arg_2 + 1 end
Just be aware that you'd need to do the variable substitution correctly :).
Regards,
Michael
Right. Sorry, please ignore the proposal to use &X to denote arity, the intent was to rather test the idea to be able to do that in general, not a particular syntax that would facilitate that. Using &/X might be a better candidate since it reflects the arity just the same way function capture does (minus function name, which totally makes sense in case of an anonymous function). Yet there might be way better candidates which can be discovered through discussion I'm sure, if the community is in favor of such extension in principle.
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAAoaZy4tQ4AfmrQvYUFngVbyZQvH5pyXDo%3Dx55wcTFOacUn68w%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4KBLEF9umPV9dQpeQEgFnOoTGmXo%3DM0MfaTFfO4eg-54Q%40mail.gmail.com.