Couldn’t this be a special case of `Kernel.apply/2`?

```elixir
@spec apply(fun | any, list(any) | fun) :: any
def apply(arg, fun) when is_function(fun, 1)) do
  fun.(arg)
end

def apply(fun, args) do
  :erlang.apply(fun, args)
end
```

This would read really well (IMO):

```elixir
[1, 2, 3, 5, 7]
|> apply(&Enum.map(&1, fn x -> x * 2 end))
```

If it’s preferred not to make a special form of apply/2, then perhaps
`then_apply/2`?

-a

On Tue, Dec 29, 2020 at 1:11 PM Paul Clegg <[email protected]> wrote:

> On Tue, Dec 29, 2020 at 1:47 AM José Valim <[email protected]> wrote:
>
>> I propose we simply add two functions to Kernel: tap and then.
>>
>> 1. tap receives an anonymous function, invokes it, and returns the
>> argument. It can be found in Ruby and .NET Rx.
>>
>> 2. then receives an anonymous function, invokes it, and returns the
>> result of the anonymous function. It will be how we can pipe to anonymous
>> functions in Elixir. It is named andThen in Scala and known as then in many
>> promise libraries across ecosystems.
>>
>
> I'm all for "tap"; I've been writing this function myself for a while now,
> as easy as it is...  Not as sure about "then", though, I've been able to
> get this to work without much problem, ala;
>
> iex(30)> foo = &(IO.puts(&1))
> &IO.puts/1
> iex(31)> "biff" |> (foo).()
> biff
> :ok
> iex(32)> foo = &(&1 <> &2)
> #Function<43.97283095/2 in :erl_eval.expr/5>
> iex(33)> "biff" |> (foo).("bar")
> "biffbar"
>
> The parens + .() combination seem to work just fine without any additional
> work.  It's just the tap that gets messy if you want to do it "inline"
> often:
>
> iex(34)> "biff" |> (fn x -> IO.puts("foo"); x; end).()
> foo
> "biff"
>
> ...so for that, I'd love 'tap()' so I don't forget to return the original
> x.  :D
>
> ...Paul
>
>
>
>
>
>
> --
> 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/CAD3kWz-MCWvdbmTRe0qP5gz57%3Diy5fOcsmNLNT1cZRe5N_D9MA%40mail.gmail.com
> <https://groups.google.com/d/msgid/elixir-lang-core/CAD3kWz-MCWvdbmTRe0qP5gz57%3Diy5fOcsmNLNT1cZRe5N_D9MA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>


-- 
Austin Ziegler • [email protected][email protected]
http://www.halostatue.ca/http://twitter.com/halostatue

-- 
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/CAJ4ekQtrodwKZsN0KE_omixojeuge1N2-3DDZjMbwvtNR_naSA%40mail.gmail.com.

Reply via email to