Using an alternate form of `apply/2` would work, because, while there’s no
guard from the Elixir side, the second parameter of `apply/2` must be an
array.

```
iex(1)> apply(fn q -> q end, fn -> nil end)
** (ArgumentError) argument error
    :erlang.length(#Function<21.126501267/0 in :erl_eval.expr/5>)
iex(1)> apply(fn q -> q end, [fn -> nil end])
#Function<21.126501267/0 in :erl_eval.expr/5>
iex(2)> apply(& &1, 3)
** (ArgumentError) argument error
    :erlang.length(3)
iex(2)> apply(& &1, [3])
3
iex(3)> apply(& &1, [])
** (BadArityError) #Function<7.126501267/1 in :erl_eval.expr/5> with arity
1 called with no arguments
```

-a

On Tue, Dec 29, 2020 at 3:59 PM Zach Daniel <[email protected]>
wrote:

> That variant doesn’t really work because what if you want to pass a
> function as the first argument to what you’re calling? I don’t think
> muddying the behavior of “apply” is worth the extra confusion. I think
> either `tap/2` or a `with` Inspect option is the way. People will
> ultimately need to look this function up anyway, I don’t think the merit of
> the name is “would I guess this name based on what I want”, because most
> things on programming fail that litmus test 😂. We just need a
> memorable/reasonable name.
>
> On Tue, Dec 29, 2020 at 3:53 PM Louis Pilfold <[email protected]>
> wrote:
>
>> Heya
>>
>> "apply" is the name I first thought of here. In fact I've tried to use
>> apply like this before!
>>
>> Cheers,
>> Louis
>>
>> On Tue, 29 Dec 2020, 20:22 Austin Ziegler, <[email protected]> wrote:
>>
>>> 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
>>> <https://groups.google.com/d/msgid/elixir-lang-core/CAJ4ekQtrodwKZsN0KE_omixojeuge1N2-3DDZjMbwvtNR_naSA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
>> 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/CABu8xFA%2BUen_jgrt96skqmv91JCdV3-1JiRNz1fyL_w6B0SuWQ%40mail.gmail.com
>> <https://groups.google.com/d/msgid/elixir-lang-core/CABu8xFA%2BUen_jgrt96skqmv91JCdV3-1JiRNz1fyL_w6B0SuWQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> 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/CAK-yb0AE1rH--%2BLoNFD54euC%2BSuvYVYWtn1bgBORh%3D3mMVuLOw%40mail.gmail.com
> <https://groups.google.com/d/msgid/elixir-lang-core/CAK-yb0AE1rH--%2BLoNFD54euC%2BSuvYVYWtn1bgBORh%3D3mMVuLOw%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/CAJ4ekQuxuLqZhNv9cZAgBsh9QvcT-s2-sGn9Nf1%2BASDEHDkGqg%40mail.gmail.com.

Reply via email to