Another potential name for this could be `Function.apply` which actually seems more accurate as `capture` has another meaning in Elixir.
On Friday, August 9, 2019 at 9:34:50 PM UTC-4, Khaja Minhajuddin wrote: > > > I like the idea, I think it would benefit the larger community as there > might be valid cases where you want to use an anonymous function (if it is > a really small one). However, the name seems confusing like Miguel pointed. > I would name it `Function.capture` as it is kind of capturing the anonymous > function and setting it up to be piped. Writing out the implementation for > this makes this name more obvious > ``` > defmodule Function do > def capture(arg, function) do > apply(function, [arg]) > end > end > > > "foo" > |> Function.capture(fn x -> String.upcase(x) end) > |> IO.puts > ``` > > > On Friday, August 9, 2019 at 10:28:25 AM UTC-4, José Valim wrote: >> >> Hi everyone, >> >> With the addition of Function.identity/1, I would like to propose another >> function to the Function module: pipe_to/2. >> >> The idea is that instead of: >> >> "foo >> |> String.upcase() >> |> (&Regex.scan(~r/foo/, &1)).() >> >> One can do: >> >> "foo >> |> String.upcase() >> |> Function.pipe_to(&Regex.scan(~r/foo/, &1)) >> >> Or if you import it before: >> >> "foo >> |> String.upcase() >> |> pipe_to(&Regex.scan(~r/foo/, &1)) >> >> While I wouldn't write the "pipe to anonymous" code, I have seen enough >> code in the wild that uses it that having a more readable (albeit more >> verbose) approach in the language sounds reasonable to me. The >> implementation can be inlined by the compiler to avoid the extra dispatch >> cost. >> >> What are your thoughts? If you "pipe to anonymous functions" in your code >> today, would you prefer to use the new function? Yes/no? Why? >> >> Thank you, >> >> *José Valim* >> www.plataformatec.com.br >> Skype: jv.ptec >> Founder and Director of R&D >> > -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/92edd3e3-8dea-4369-bbb3-affad0fba331%40googlegroups.com.
