Elixir: Pipe operator and multiple parameters

Created on 31 May 2013  路  15Comments  路  Source: elixir-lang/elixir

Acording to Kernel doc:

[1,[2],3] |> List.flatten |> Enum.map(&1 * 2)

and

Enum.map(List.flatten([1,[2],3]), &1 * 2)

Are the same. Implicitly the output of List.flatten is given to the first argument.

Would be too crazy or unreadable if we could pass the argument position? Something like:

[1,[2],3] |> List.flatten |>2 My.func(&1 * 2)

And the expression would translate to:

My.func(&1 * 2, List.flatten([1,[2],3]))

Too confusing?

Most helpful comment

@edgurgel Since Elixir recommends the "subject" argument to be the first one, it is likely that you won't need to use |2> or |>2. I am certain that you are going to need it in some circunstances but since it is supposed to be rather uncommon, I believe we should just skip |2> for now. :)

All 15 comments

Better |2>

@edgurgel Since Elixir recommends the "subject" argument to be the first one, it is likely that you won't need to use |2> or |>2. I am certain that you are going to need it in some circunstances but since it is supposed to be rather uncommon, I believe we should just skip |2> for now. :)

Ok! Thank you @alco and @josevalim! Maybe one day we discuss it again ;)

@edgurgel @alco @josevalim

https://twitter.com/joeerl/status/340707833815388160

He-he :)

Instead of adding |2>, |> could parse &-arguments in the call. Currently, string |> Regex.match?(%r"...", &1) creates a new function. I don't think this is a good way to create a function, as fn in this case would be much clearer. So, & could be overloaded in the context of |> meaning "put the argument into this place".

With that implemented, the code "123" |> f.(&1) |> f.(&1) would become something useful rather than silly.

@manpages Hehehe! :+1:

The example from above would then be rewritten like this:

[1,[2],3] |> List.flatten |>2 My.func(&1 * 2)
# converts to
fn x -> [1,[2],3] |> List.flatten |> My.func(x * 2, &1) end
[1,[2],3] |> List.flatten |> My.func(&1 * 2, _)

What about _ as mentioned (https://twitter.com/josevalim/status/340843098991120384) by @josevalim

_ looks good to me.

Maybe reopen this issue to discuss it again?

Even though I suggested it, I am still not a huge fan of it. This is definitely one of those syntax additions that can wait. :) And if someone wants to try it today, |> is just a macro, so someone can definitely reimplement |> in their projects and give it a try for fun.

Cool! Gonna try it! :+1:

I second @alco, I was confused by string |> Regex.match? not working as expected. Either _ or &1 are good options. I like the later just cause it is in the space of argument goes here, where _ seem to be for something along 'placeholder'.

Please don't resurrect old issues. Use the mailing list for feature proposals.

@josevalim Wouldn't be useful to have some macro for converting functions with arity greater than 1 to a combined monad?

From swift prelude
https://github.com/pointfreeco/swift-overture

let stringWithEncoding = flip(curry(String.init(data:encoding:)))
// (String.Encoding) -> (Data) -> String?

let utf8String = stringWithEncoding(.utf8)
// (Data) -> String?

Or from haskell curry

Using curry, wouldn't in certain way resolve this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ckampfe picture ckampfe  路  3Comments

shadowfacts picture shadowfacts  路  3Comments

vothane picture vothane  路  3Comments

Paddy3118 picture Paddy3118  路  3Comments

cmeiklejohn picture cmeiklejohn  路  3Comments