This page is no longer maintained — Please continue to the home page at www.scala-lang.org

|> operator like in F#

16 replies
Michael Stal
Joined: 2011-01-29,
User offline. Last seen 42 years 45 weeks ago.

In F# the pipeline operator |> is available which allows to write
expressions like the following one:

let list2 =
list1 |> List.map (fun x -> x * x) |> List.rev

Its definition is: let (|>) x f = f x

I am wondering whether a pipeline operator would make sense in Scala
and how it would integrate into the language (or library)

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: |> operator like in F#
It makes sense, and I personally like it. Scalaz has one but it isn't usable with everything. There's a simple implementation which is, though.
However, while such an operator is pretty much mandatory in F#, it is much less useful in Scala. For instance, that example translates like this in Scala:
list1.map(x => x * x).reverse
On Sat, Jan 29, 2011 at 14:24, Michael Stal <michael [dot] stal [at] gmail [dot] com> wrote:
In F# the pipeline operator |> is available which allows to write
expressions like the following one:

let list2 =
   list1 |> List.map (fun x -> x * x) |> List.rev

Its definition is: let (|>) x f = f x

I am wondering whether a pipeline operator would make sense in Scala
and how it would integrate into the language (or library)



--
Daniel C. Sobral

I travel to the future all the time.
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: |> operator like in F#
i'm not familiar with f#
what does the operator do? is it similar to a collection view in scala? or more like Stream.cons?

Am 29.01.2011 17:50, schrieb Daniel Sobral:
Ymgf7Q [at] mail [dot] gmail [dot] com" type="cite">It makes sense, and I personally like it. Scalaz has one but it isn't usable with everything. There's a simple implementation which is, though.
However, while such an operator is pretty much mandatory in F#, it is much less useful in Scala. For instance, that example translates like this in Scala:
list1.map(x => x * x).reverse
On Sat, Jan 29, 2011 at 14:24, Michael Stal <michael [dot] stal [at] gmail [dot] com" rel="nofollow">michael [dot] stal [at] gmail [dot] com> wrote:
In F# the pipeline operator |> is available which allows to write
expressions like the following one:

let list2 =
   list1 |> List.map (fun x -> x * x) |> List.rev

Its definition is: let (|>) x f = f x

I am wondering whether a pipeline operator would make sense in Scala
and how it would integrate into the language (or library)



--
Daniel C. Sobral

I travel to the future all the time.

Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: |> operator like in F#


On 29 January 2011 17:30, HamsterofDeath <h-star [at] gmx [dot] de> wrote:
i'm not familiar with f#
what does the operator do? is it similar to a collection view in scala? or more like Stream.cons?



None of the above, it's closer to a pipe on the unix command line, so:
  param |> func
is equivalent to
  func(param)
 
Am 29.01.2011 17:50, schrieb Daniel Sobral:
It makes sense, and I personally like it. Scalaz has one but it isn't usable with everything. There's a simple implementation which is, though.
However, while such an operator is pretty much mandatory in F#, it is much less useful in Scala. For instance, that example translates like this in Scala:
list1.map(x => x * x).reverse
On Sat, Jan 29, 2011 at 14:24, Michael Stal <michael [dot] stal [at] gmail [dot] com> wrote:
In F# the pipeline operator |> is available which allows to write
expressions like the following one:

let list2 =
   list1 |> List.map (fun x -> x * x) |> List.rev

Its definition is: let (|>) x f = f x

I am wondering whether a pipeline operator would make sense in Scala
and how it would integrate into the language (or library)



--
Daniel C. Sobral

I travel to the future all the time.




--
Kevin Wright

gtalk / msn : kev [dot] lee [dot] wright [at] gmail [dot] comkev [dot] lee [dot] wright [at] gmail [dot] commail: kevin [dot] wright [at] scalatechnology [dot] com
vibe / skype: kev.lee.wrightquora: http://www.quora.com/Kevin-Wright
twitter: @thecoda

READ CAREFULLY. By reading this email, you agree, on behalf of your employer, to release me from all obligations and waivers arising from any and all NON-NEGOTIATED  agreements, licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap, confidentiality, non-disclosure, non-compete and acceptable use policies ("BOGUS AGREEMENTS") that I have entered into with your employer, its partners, licensors, agents and assigns, in perpetuity, without prejudice to my ongoing rights and privileges. You further represent that you have the authority to release me from any BOGUS AGREEMENTS on behalf of your employer.
H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: |> operator like in F#
i'm not familiar with unix either. so it allowes a reversed order syntax?

Am 29.01.2011 18:38, schrieb Kevin Wright:
AANLkTikmqqebfKCt-sOGzpPvFViW+H8SHCcVaMai3Vrg [at] mail [dot] gmail [dot] com" type="cite">

On 29 January 2011 17:30, HamsterofDeath <h-star [at] gmx [dot] de" rel="nofollow">h-star [at] gmx [dot] de> wrote:
i'm not familiar with f#
what does the operator do? is it similar to a collection view in scala? or more like Stream.cons?



None of the above, it's closer to a pipe on the unix command line, so:
  param |> func
is equivalent to
  func(param)
 
Am 29.01.2011 17:50, schrieb Daniel Sobral:
It makes sense, and I personally like it. Scalaz has one but it isn't usable with everything. There's a simple implementation which is, though.
However, while such an operator is pretty much mandatory in F#, it is much less useful in Scala. For instance, that example translates like this in Scala:
list1.map(x => x * x).reverse
On Sat, Jan 29, 2011 at 14:24, Michael Stal <michael [dot] stal [at] gmail [dot] com" target="_blank" rel="nofollow">michael [dot] stal [at] gmail [dot] com> wrote:
In F# the pipeline operator |> is available which allows to write
expressions like the following one:

let list2 =
   list1 |> List.map (fun x -> x * x) |> List.rev

Its definition is: let (|>) x f = f x

I am wondering whether a pipeline operator would make sense in Scala
and how it would integrate into the language (or library)



--
Daniel C. Sobral

I travel to the future all the time.




--
Kevin Wright

gtalk / msn : kev [dot] lee [dot] wright [at] gmail [dot] com" target="_blank" rel="nofollow">kev [dot] lee [dot] wright [at] gmail [dot] com mail: kevin [dot] wright [at] scalatechnology [dot] com" target="_blank" rel="nofollow">kevin [dot] wright [at] scalatechnology [dot] com
vibe / skype: kev.lee.wright quora: http://www.quora.com/Kevin-Wright
twitter: @thecoda

READ CAREFULLY. By reading this email, you agree, on behalf of your employer, to release me from all obligations and waivers arising from any and all NON-NEGOTIATED  agreements, licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap, confidentiality, non-disclosure, non-compete and acceptable use policies ("BOGUS AGREEMENTS") that I have entered into with your employer, its partners, licensors, agents and assigns, in perpetuity, without prejudice to my ongoing rights and privileges. You further represent that you have the authority to release me from any BOGUS AGREEMENTS on behalf of your employer.

Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: |> operator like in F#

On Saturday January 29 2011, HamsterofDeath wrote:
> i'm not familiar with unix either. so it allowes a reversed order
> syntax?

Just think of a data flow pipeline. The first (left-most) expression
suplies a value. Thereafter the value is transformed by a sequence of
function applications written left-to-right with the "pipe" operator
between each.

Just like a Unix / Linux shell pipeline has supported since time
immemorial (the 1970s).

Randall Schulz

H-star Development
Joined: 2010-04-14,
User offline. Last seen 2 years 26 weeks ago.
Re: |> operator like in F#

got that :)

Am 29.01.2011 19:09, schrieb Randall R Schulz:
> On Saturday January 29 2011, HamsterofDeath wrote:
>> i'm not familiar with unix either. so it allowes a reversed order
>> syntax?
> Just think of a data flow pipeline. The first (left-most) expression
> suplies a value. Thereafter the value is transformed by a sequence of
> function applications written left-to-right with the "pipe" operator
> between each.
>
> Just like a Unix / Linux shell pipeline has supported since time
> immemorial (the 1970s).
>
>
> Randall Schulz
>

Michael Stal
Joined: 2011-01-29,
User offline. Last seen 42 years 45 weeks ago.
Re: |> operator like in F#

> list1.map(x => x * x).reverse

You could do something similar in F#.

However, where pipelining becomes really useful is whenever you have a
large sequence of pipelined activities
Take as an example the following F# fragment:

let google = http "http://www.google.com"
google |> getWords |> List.filter (fun s -> s = "href") |> List.length

StephaneLD
Joined: 2008-08-28,
User offline. Last seen 4 years 9 weeks ago.
Re: Re: |> operator like in F#
you got to believe..
object Pipelining {  implicit def toPipe[T](x : T) = new {    def |> [U](f : T => U) = f(x)  }}

  import Pipelining._  List(5,6,7) |> (_.map(_.toString)) |> println


On Sat, Jan 29, 2011 at 10:21 PM, Michael Stal <michael [dot] stal [at] gmail [dot] com> wrote:
> list1.map(x => x * x).reverse

You could do something similar in F#.

However, where pipelining becomes really useful is whenever you have a
large sequence of pipelined activities
Take as an example the following F# fragment:

let google = http "http://www.google.com"
google |> getWords |> List.filter (fun s -> s = "href") |> List.length








--
Stéphane Le Dorze
Tel: +33 (0) 6 08  76 70 15

Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: |> operator like in F#

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 30/01/11 03:38, Kevin Wright wrote:
> None of the above, it's closer to a pipe on the unix command line,
> so:
>
> param |> func
>
> is equivalent to
>
> func(param)
>
The pipe on the unix command line is closer to flatMap on IO state.

case class World() // place-holder

case class IO[A](run: World => (World, A)) {
def flatMap[B](f: A => IO[B]) =
IO[B](w => {
val (ww, a) = run(w)
val r = f(a)
r run ww
})
}

|> is more closer to flatMap on Identity.

case class Identity[A](a: A) {
def flatMap[B](f: A => Identity[B]) =
f(a)
}

StephaneLD
Joined: 2008-08-28,
User offline. Last seen 4 years 9 weeks ago.
Re: Re: |> operator like in F#
the example becoming (still too lispy):
    List(5,6,7) |> (_.map (x => x * x)) |> (_.reverse)

On Sat, Jan 29, 2011 at 10:59 PM, Stephane Le Dorze <stephane [dot] ledorze [at] gmail [dot] com> wrote:
you got to believe..
object Pipelining {  implicit def toPipe[T](x : T) = new {     def |> [U](f : T => U) = f(x)  }}

  import Pipelining._  List(5,6,7) |> (_.map(_.toString)) |> println


On Sat, Jan 29, 2011 at 10:21 PM, Michael Stal <michael [dot] stal [at] gmail [dot] com> wrote:
> list1.map(x => x * x).reverse

You could do something similar in F#.

However, where pipelining becomes really useful is whenever you have a
large sequence of pipelined activities
Take as an example the following F# fragment:

let google = http "http://www.google.com"
google |> getWords |> List.filter (fun s -> s = "href") |> List.length








--
Stéphane Le Dorze
Tel: +33 (0) 6 08  76 70 15




--
Stéphane Le Dorze
Tel: +33 (0) 6 08  76 70 15

Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: |> operator like in F#

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 30/01/11 08:04, Stephane Le Dorze wrote:
> List(5,6,7) |> (_.map (x => x * x)) |> (_.reverse)

List(5,6,7) |> (_.map (*.flatten)) |> (_.reverse)

oh wait... :)


- --
Tony Morris
http://tmorris.net/

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk1EkOEACgkQmnpgrYe6r63mAQCghLMhauobTv1SnWKyyoNRYPMN
7K0An3pU9ZZq50g713BGyHzZzOC5GPyy
=AZQa
-----END PGP SIGNATURE-----

odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: Re: |> operator like in F#


On Sat, Jan 29, 2011 at 11:12 PM, Tony Morris <tonymorris [at] gmail [dot] com> wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 30/01/11 08:04, Stephane Le Dorze wrote:
> List(5,6,7) |> (_.map (x => x * x)) |> (_.reverse)

List(5,6,7) |> (_.map (*.flatten)) |> (_.reverse)

oh wait... :)

Indeed!

 -- Martin

StephaneLD
Joined: 2008-08-28,
User offline. Last seen 4 years 9 weeks ago.
Re: Re: |> operator like in F#
man, I don't get it.. (!?)

On Sat, Jan 29, 2011 at 11:24 PM, martin odersky <martin [dot] odersky [at] epfl [dot] ch> wrote:


On Sat, Jan 29, 2011 at 11:12 PM, Tony Morris <tonymorris [at] gmail [dot] com> wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 30/01/11 08:04, Stephane Le Dorze wrote:
> List(5,6,7) |> (_.map (x => x * x)) |> (_.reverse)

List(5,6,7) |> (_.map (*.flatten)) |> (_.reverse)

oh wait... :)

Indeed!

 -- Martin




--
Stéphane Le Dorze
Tel: +33 (0) 6 08  76 70 15

Michael Stal
Joined: 2011-01-29,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: |> operator like in F#
thumbs up :-)

On Sat, Jan 29, 2011 at 11:24 PM, martin odersky <martin [dot] odersky [at] epfl [dot] ch> wrote:


On Sat, Jan 29, 2011 at 11:12 PM, Tony Morris <tonymorris [at] gmail [dot] com> wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 30/01/11 08:04, Stephane Le Dorze wrote:
> List(5,6,7) |> (_.map (x => x * x)) |> (_.reverse)

List(5,6,7) |> (_.map (*.flatten)) |> (_.reverse)

oh wait... :)

Indeed!

 -- Martin




--
mit vielen Grüßen / best regards

Prof. Dr. Michael Stal

Editor-in-Chief of JavaSPEKTRUM
Principal Engineer @ Siemens Corporate Research & Technology

"The role of a trainer or consultant is to empower the customer,
not to make himself indispensable"
[Bertrand Meyer]


Blog: http://stal.blogspot.com

Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: |> operator like in F#

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

flatten has the form M[M[A]] => M[A] **

as in:
Option[Option[A]] => Option[A]
List[List[A]] => List[A]

suppose now:
Function1[T, Function1[T, A]] => Function1[T, A]

then:
let (*) = (a: Int) => (b: Int) => a * b

and so:
*.flatten has the type Int => Int and will square its argument as in
the example.

Essentially, any expression of the form x => f(x)(x) can be
hypothetically rewritten f.flatten

This is a point-free form of the expression x => x * x. Of course,
there are a number of reasons why this won't work in practice; the
flatten method is unavailable, (*) is overloaded requiring type
annotations, (*) is uncurried... unless you use scalaz where "flatten"
is called join or ?.

This use-case comes up often, so sorry about your confusion :)

** note entirely true anymore with the current collections :(

On 30/01/11 08:58, Stephane Le Dorze wrote:
> man, I don't get it.. (!?)
>
> On Sat, Jan 29, 2011 at 11:24 PM, martin odersky
> wrote:
>
>>
>>
>> On Sat, Jan 29, 2011 at 11:12 PM, Tony Morris
>> wrote:
>>
>>>
>>> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
>>>
>>> On 30/01/11 08:04, Stephane Le Dorze wrote:
>>>> List(5,6,7) |> (_.map (x => x * x)) |> (_.reverse)
>>>
>>> List(5,6,7) |> (_.map (*.flatten)) |> (_.reverse)
>>>
>>> oh wait... :)
>>>
>>>
>>> Indeed!
>>

StephaneLD
Joined: 2008-08-28,
User offline. Last seen 4 years 9 weeks ago.
Re: Re: |> operator like in F#
I though it was a value level joke, not a type level one :)

On Sun, Jan 30, 2011 at 12:22 AM, Tony Morris <tonymorris [at] gmail [dot] com> wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

flatten has the form M[M[A]] => M[A] **

as in:
Option[Option[A]] => Option[A]
List[List[A]] => List[A]

suppose now:
Function1[T, Function1[T, A]] => Function1[T, A]

then:
let (*) = (a: Int) => (b: Int) => a * b

and so:
*.flatten has the type Int => Int and will square its argument as in
the example.

Essentially, any expression of the form x => f(x)(x) can be
hypothetically rewritten f.flatten

This is a point-free form of the expression x => x * x. Of course,
there are a number of reasons why this won't work in practice; the
flatten method is unavailable, (*) is overloaded requiring type
annotations, (*) is uncurried... unless you use scalaz where "flatten"
is called join or ?.

This use-case comes up often, so sorry about your confusion :)




** note entirely true anymore with the current collections :(


On 30/01/11 08:58, Stephane Le Dorze wrote:
> man, I don't get it.. (!?)
>
> On Sat, Jan 29, 2011 at 11:24 PM, martin odersky
> <martin [dot] odersky [at] epfl [dot] ch>wrote:
>
>>
>>
>> On Sat, Jan 29, 2011 at 11:12 PM, Tony Morris
>> <tonymorris [at] gmail [dot] com>wrote:
>>
>>>
>>> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
>>>
>>> On 30/01/11 08:04, Stephane Le Dorze wrote:
>>>> List(5,6,7) |> (_.map (x => x * x)) |> (_.reverse)
>>>
>>> List(5,6,7) |> (_.map (*.flatten)) |> (_.reverse)
>>>
>>> oh wait... :)
>>>
>>>
>>> Indeed!
>>
>> -- Martin
>>
>>
>
>


- --
Tony Morris
http://tmorris.net/

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk1EoSUACgkQmnpgrYe6r61rIgCdE85iNACYDrFiAil97oWpm2Qf
iDAAnA095zDEn8Eee4wDNJX2qxCYu3CC
=JwkO
-----END PGP SIGNATURE-----




--
Stéphane Le Dorze
Tel: +33 (0) 6 08  76 70 15

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland