Hi!!
Im looking to do this but cant due to nested captures:
Enum.map( &Kernel.apply( &Kernel.-/2, &1 ) )
Certainly this can be Enum.map(fn [a,b] -> a-b end)
so … yeah … but was just curious Enum.map_apply(&Kernel.-/2)
?
Thx!!
Hi!!
Im looking to do this but cant due to nested captures:
Enum.map( &Kernel.apply( &Kernel.-/2, &1 ) )
Certainly this can be Enum.map(fn [a,b] -> a-b end)
so … yeah … but was just curious Enum.map_apply(&Kernel.-/2)
?
Thx!!
No, there is no such thing.
The three-argument form of apply
makes this cleaner, if the function you’re calling is public:
Enum.map(some_list, &apply(Kernel, :-, &1))
IMO even if a map_apply
existed, it would still be clearer to use Enum.map(fn [a,b] -> a-b end)
than any apply
form.
For instance, the explicit pattern-match gives a better error message when passed malformed data:
iex(7)> a = [[2, 1], [4, 1], [1, 2, 3]]
[[2, 1], [4, 1], [1, 2, 3]]
iex(8)> Enum.map(a, &apply(Kernel, :-, &1))
** (UndefinedFunctionError) function Kernel.-/3 is undefined or private. Did you mean:
* -/1
* -/2
(elixir 1.13.4) Kernel.-(1, 2, 3)
(elixir 1.13.4) lib/enum.ex:1593: Enum."-map/2-lists^map/1-0-"/2
(elixir 1.13.4) lib/enum.ex:1593: Enum."-map/2-lists^map/1-0-"/2
iex(8)> Enum.map(a, fn [a, b] -> b - a end)
** (FunctionClauseError) no function clause matching in :erl_eval."-inside-an-interpreted-fun-"/1
The following arguments were given to :erl_eval."-inside-an-interpreted-fun-"/1:
# 1
[1, 2, 3]
(stdlib 3.15.2) :erl_eval."-inside-an-interpreted-fun-"/1
(stdlib 3.15.2) erl_eval.erl:834: :erl_eval.eval_fun/6
(elixir 1.13.4) lib/enum.ex:1593: Enum."-map/2-lists^map/1-0-"/2
(elixir 1.13.4) lib/enum.ex:1593: Enum."-map/2-lists^map/1-0-"/2
Then again, you might want the behavior for single-element lists:
iex(8)> a = [[2, 1], [4, 1], [1]]
[[2, 1], [4, 1], [1]]
iex(9)> Enum.map(a, &apply(Kernel, :-, &1))
[1, 3, -1]
iex(10)> Enum.map(a, fn [a, b] -> b - a end)
** (FunctionClauseError) no function clause matching in :erl_eval."-inside-an-interpreted-fun-"/1
The following arguments were given to :erl_eval."-inside-an-interpreted-fun-"/1:
# 1
[1]
(stdlib 3.15.2) :erl_eval."-inside-an-interpreted-fun-"/1
(stdlib 3.15.2) erl_eval.erl:834: :erl_eval.eval_fun/6
(elixir 1.13.4) lib/enum.ex:1593: Enum."-map/2-lists^map/1-0-"/2
(elixir 1.13.4) lib/enum.ex:1593: Enum."-map/2-lists^map/1-0-"/2
Here the single-element list has called Kernel.-/1
. Note that the nested-captures version (if it compiled) wouldn’t have this property, since it captures a single arity.
thx!
and duh on me! should have seen and registered the /3 version!