I have prepared small example of macro that does this already:

defmodule F do
  defmacro pipe_to(pipe, {function, _env, args}) when is_atom(function) and 
is_list(args) do
    func_arg = quote do: a
    args =
      for arg <- args do
        case arg do
          {:_, _, _} -> func_arg
          _ -> arg
        end
      end

    quote do
      (fn unquote(func_arg) -> unquote(function)(unquote_splicing(args)) 
end).(unquote(pipe))
    end
  end
end

It is not perfect yet, as for example this do not allow `Mod.func()` calls, 
but is is working pretty well as is.


W dniu wtorek, 13 sierpnia 2019 17:42:17 UTC+2 użytkownik OvermindDL1 
napisał:
>
> On Tuesday, August 13, 2019 at 3:04:03 AM UTC-6, José Valim wrote:
>>
>> I really want to avoid new syntactical constructs or special casing 
>> anonymous functions as I believe it will be more confusing in the long term.
>>
>
> At that point adding in `.()` isn't a big deal, that's why it's pretty 
> well fine as it is honestly for those kind of things.
>
> The main issue is just piping into another 'argument' of a function call, 
> like the erlang standard library very much expects the 'subject' to be at 
> the end, so I end up having to do a lot of things like:
> ```
> args
> |> transform_them()
> |> (&send(pid, &1)).()
> ```
> When it would look and feel so much more clean via anything like:
> ```
> args
> |> transform_them()
> |> &send(pid, &1) # Special case when `&` is in first position, though 
> that does run into the expecting passing a `fun` to work
>
> args
> |> transform_them()
> |> send(pid, _) # Pipe into the 'hole'
>
> args
> |> transform_them()
> |> send(pid, _1) # Maybe index it, though this seems entirely needless
>
> args
> |> transform_them()
> |> send(pid, &0) # Or use `&0` as a pipe value, though then it looks less 
> like an obvious hole and thus more easy to 'scan' past
>
> args
> |> transform_them()
> ~> send(pid, _) # Perhaps a different pipe operator for piping into, but 
> this seems needless when `|>` can do it just fine
> ```
>
> All of which are much more easily read.  And there are libraries for doing 
> just this, they 'feel' so much more clean when I use them.  With all of all 
> of the above no closures even need to be created, it doesn't look like an 
> anonymous function (except maybe the `&0` one), the prior value is just 
> dumped into 'that' slot instead of the front slot.  Honestly I'd prefer if 
> it always required `_`, no magic 'stuff into front position' or anything, 
> this would even allow you to reference the value more than once (set it to 
> a gensym'd name then and put that in the holes).
>

-- 
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/d7eabed9-9d60-49b3-bf91-0840be4ed4b2%40googlegroups.com.

Reply via email to