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

pipeline operator

11 replies
Razvan Cojocaru 3
Joined: 2010-07-28,
User offline. Last seen 42 years 45 weeks ago.

I was watching an F# presentation and I must say that I like the ‘pipeline operator’ |>

 

It has the potential to get rid of the “lines are getting too long” problem.

 

let prices =

                Csv.split ([|’n’|])

                |> Seq.skip 1

                |> Seq.map (…)

                |> Seq.filter (…)

                |> Seq.map (…)

 

Instead of scala’s

 

def prices = csv.split (‘t’).skip(1).map (…).filter (…).map (…)

 

That seems like it would make the code more readable in some scenarios…

 

 

Paul Hudson
Joined: 2011-01-08,
User offline. Last seen 42 years 45 weeks ago.
Re: pipeline operator

> That seems like it would make the code more readable in some scenarios…


Am I missing something?


def prices = csv

                .split (‘t’)

                .skip(1)

                 .map (…)

                 .filter (…)

                 .map (…)


seems pretty equivalently readable to me.

On 6 April 2011 15:16, Razvan Cojocaru <pub [at] razie [dot] com> wrote:

I was watching an F# presentation and I must say that I like the ‘pipeline operator’ |>

 

It has the potential to get rid of the “lines are getting too long” problem.

 

let prices =

                Csv.split ([|’n’|])

                |> Seq.skip 1

                |> Seq.map (…)

                |> Seq.filter (…)

                |> Seq.map (…)

 

Instead of scala’s

 

def prices = csv.split (‘t’).skip(1).map (…).filter (…).map (…)

 

That seems like it would make the code more readable in some scenarios…

 

 


Rustem Suniev
Joined: 2011-04-06,
User offline. Last seen 42 years 45 weeks ago.
Re: pipeline operator

It's easy to define one: def |>[B](f: A => B) = f(a)

On 6 April 2011 21:24, Paul Hudson wrote:
>> That seems like it would make the code more readable in some scenarios…
>
> Am I missing something?
>
> def prices = csv
>
>                 .split (‘t’)
>
>                 .skip(1)
>
>                  .map (…)
>
>                  .filter (…)
>
>                  .map (…)
>
> seems pretty equivalently readable to me.
>
> On 6 April 2011 15:16, Razvan Cojocaru wrote:
>>
>> I was watching an F# presentation and I must say that I like the ‘pipeline
>> operator’ |>
>>
>>
>>
>> It has the potential to get rid of the “lines are getting too long”
>> problem.
>>
>>
>>
>> let prices =
>>
>>                 Csv.split ([|’n’|])
>>
>>                 |> Seq.skip 1
>>
>>                 |> Seq.map (…)
>>
>>                 |> Seq.filter (…)
>>
>>                 |> Seq.map (…)
>>
>>
>>
>> Instead of scala’s
>>
>>
>>
>> def prices = csv.split (‘t’).skip(1).map (…).filter (…).map (…)
>>
>>
>>
>> That seems like it would make the code more readable in some scenarios…
>>
>>
>>
>>
>

Vladimir Kirichenko
Joined: 2009-02-19,
User offline. Last seen 42 years 45 weeks ago.
Re: pipeline operator

Razvan Cojocaru wrote:
> I was watching an F# presentation and I must say that I like the
> ‘pipeline operator’ |>
....
> That seems like it would make the code more readable in some scenarios…

Pipeline is a replacement for:

f1(f2(f3(f4(x))))

x |> f4 |> f3 |> f2 |> f1

not for the:

x.f4.f3.f2.f1

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: pipeline operator
Or even

def prices = (
    csv
    split 't'
    skip 1
    map (...)
    filter (...)
    map (...)
)


On Wed, Apr 6, 2011 at 17:24, Paul Hudson <phudson [at] pobox [dot] com> wrote:

> That seems like it would make the code more readable in some scenarios…


Am I missing something?


def prices = csv

                .split (‘t’)

                .skip(1)

                 .map (…)

                 .filter (…)

                 .map (…)


seems pretty equivalently readable to me.

On 6 April 2011 15:16, Razvan Cojocaru <pub [at] razie [dot] com> wrote:

I was watching an F# presentation and I must say that I like the ‘pipeline operator’ |>

 

It has the potential to get rid of the “lines are getting too long” problem.

 

let prices =

                Csv.split ([|’n’|])

                |> Seq.skip 1

                |> Seq.map (…)

                |> Seq.filter (…)

                |> Seq.map (…)

 

Instead of scala’s

 

def prices = csv.split (‘t’).skip(1).map (…).filter (…).map (…)

 

That seems like it would make the code more readable in some scenarios…

 

 





--
Daniel C. Sobral

I travel to the future all the time.
Razvan Cojocaru 3
Joined: 2010-07-28,
User offline. Last seen 42 years 45 weeks ago.
RE: pipeline operator

…If I manage to overcome my ingrained fear of multi-lines code in scala, which I’m afraid I can’t J It’s just the way my brain works – I guess it doesn’t make a difference between + and .

 

Besides…

 

$ scala

Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.

6.0_22).

Type in expressions to have them evaluated.

Type :help for more information.

 

scala> List(1,2,4)

res0: List[Int] = List(1, 2, 4)

 

scala> .map(_+2)

<console>:7: error: value map is not a member of scala.tools.nsc.InterpreterSett

ings

       settings.map(_+2)

                ^

 

scala>

 

From: Paul Hudson [mailto:phudson [at] pobox [dot] com]
Sent: April-06-11 4:24 PM
To: Razvan Cojocaru
Cc: scala-debate [at] googlegroups [dot] com
Subject: Re: [scala-debate] pipeline operator

 

> That seems like it would make the code more readable in some scenarios…

 

Am I missing something?

 

def prices = csv

                .split (‘t’)

                .skip(1)

                 .map (…)

                 .filter (…)

                 .map (…)


seems pretty equivalently readable to me.

On 6 April 2011 15:16, Razvan Cojocaru <pub [at] razie [dot] com> wrote:

I was watching an F# presentation and I must say that I like the ‘pipeline operator’ |>

 

It has the potential to get rid of the “lines are getting too long” problem.

 

let prices =

                Csv.split ([|’n’|])

                |> Seq.skip 1

                |> Seq.map (…)

                |> Seq.filter (…)

                |> Seq.map (…)

 

Instead of scala’s

 

def prices = csv.split (‘t’).skip(1).map (…).filter (…).map (…)

 

That seems like it would make the code more readable in some scenarios…

 

 

 

Razvan Cojocaru 3
Joined: 2010-07-28,
User offline. Last seen 42 years 45 weeks ago.
RE: pipeline operator

Ya – thanks, that’s it. I’ve forgotten this style J Must write more code!

 

scala> (List(1,2,3)

     | map (_+2)

     | map (_+5)

    | )

res3: List[Int] = List(8, 9, 10)

 

scala>

 

I’m assuming the code formatters know this… and preserve the line breaks in this case… didn’t try it.

 

Note however the | inserted by the interpreter – looks familiar… J

 

From: Daniel Sobral [mailto:dcsobral [at] gmail [dot] com]
Sent: April-06-11 4:51 PM
To: Paul Hudson
Cc: Razvan Cojocaru; scala-debate [at] googlegroups [dot] com
Subject: Re: [scala-debate] pipeline operator

 

Or even

def prices = (
    csv
    split 't'
    skip 1
    map (...)
    filter (...)
    map (...)
)

On Wed, Apr 6, 2011 at 17:24, Paul Hudson <phudson [at] pobox [dot] com> wrote:

> That seems like it would make the code more readable in some scenarios…

 

Am I missing something?

 

def prices = csv

                .split (‘t’)

                .skip(1)

                 .map (…)

                 .filter (…)

                 .map (…)

 

seems pretty equivalently readable to me.

 

On 6 April 2011 15:16, Razvan Cojocaru <pub [at] razie [dot] com> wrote:

I was watching an F# presentation and I must say that I like the ‘pipeline operator’ |>

 

It has the potential to get rid of the “lines are getting too long” problem.

 

let prices =

                Csv.split ([|’n’|])

                |> Seq.skip 1

                |> Seq.map (…)

                |> Seq.filter (…)

                |> Seq.map (…)

 

Instead of scala’s

 

def prices = csv.split (‘t’).skip(1).map (…).filter (…).map (…)

 

That seems like it would make the code more readable in some scenarios…

 

 

 




--
Daniel C. Sobral

I travel to the future all the time.

Razvan Cojocaru 3
Joined: 2010-07-28,
User offline. Last seen 42 years 45 weeks ago.
RE: pipeline operator

Yes - I did find it funny them not using object notation x.map(_) but static library "Seq.map(_)(x)". I know that's Haskell style but I don't appreciate learning statics without content assist. The world is not made of 3 letters... well, biblically it is but you know what I mean ;)

-----Original Message-----
From: scala-debate [at] googlegroups [dot] com [mailto:scala-debate [at] googlegroups [dot] com] On Behalf Of Volodymyr Kyrychenko
Sent: April-06-11 4:35 PM
To: scala-debate [at] googlegroups [dot] com
Subject: Re: [scala-debate] pipeline operator

Razvan Cojocaru wrote:
> I was watching an F# presentation and I must say that I like the
> ‘pipeline operator’ |>
....
> That seems like it would make the code more readable in some
> scenarios…

Pipeline is a replacement for:

f1(f2(f3(f4(x))))

x |> f4 |> f3 |> f2 |> f1

not for the:

x.f4.f3.f2.f1

--
Best Regards,
Volodymyr Kyrychenko

jibal
Joined: 2010-12-01,
User offline. Last seen 1 year 45 weeks ago.
Re: pipeline operator
Seq is .NET/CLR stuff.

-- Jim



On Wed, Apr 6, 2011 at 2:09 PM, Razvan Cojocaru <pub [at] razie [dot] com> wrote:
Yes - I did find it funny them not using object notation x.map(_) but static library "Seq.map(_)(x)". I know that's Haskell style but I don't appreciate learning statics without content assist. The world is not made of 3 letters... well, biblically it is but you know what I mean ;)


-----Original Message-----
From: scala-debate [at] googlegroups [dot] com [mailto:scala-debate [at] googlegroups [dot] com] On Behalf Of Volodymyr Kyrychenko
Sent: April-06-11 4:35 PM
To: scala-debate [at] googlegroups [dot] com
Subject: Re: [scala-debate] pipeline operator

Razvan Cojocaru wrote:
> I was watching an F# presentation and I must say that I like the
> ‘pipeline operator’ |>
....
> That seems like it would make the code more readable in some
> scenarios…

Pipeline is a replacement for:

f1(f2(f3(f4(x))))

x |> f4 |> f3 |> f2 |> f1

not for the:

x.f4.f3.f2.f1

--
Best Regards,
Volodymyr Kyrychenko


Vladimir Kirichenko
Joined: 2009-02-19,
User offline. Last seen 42 years 45 weeks ago.
Re: pipeline operator

Razvan Cojocaru wrote:
> Yes - I did find it funny them not using object notation x.map(_) but static library "Seq.map(_)(x)". I know that's Haskell style

F# is derivative from OCaml. But its _objective_ part is not very
popular in it.

Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: pipeline operator
In 2.9 you can enter :paste and have enter multi-line code.

On Wed, Apr 6, 2011 at 4:52 PM, Razvan Cojocaru <pub [at] razie [dot] com> wrote:

…If I manage to overcome my ingrained fear of multi-lines code in scala, which I’m afraid I can’t J It’s just the way my brain works – I guess it doesn’t make a difference between + and .

 

Besides…

 

$ scala

Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.

6.0_22).

Type in expressions to have them evaluated.

Type :help for more information.

 

scala> List(1,2,4)

res0: List[Int] = List(1, 2, 4)

 

scala> .map(_+2)

<console>:7: error: value map is not a member of scala.tools.nsc.InterpreterSett

ings

       settings.map(_+2)

                ^

 

scala>

 

From: Paul Hudson [mailto:phudson [at] pobox [dot] com]
Sent: April-06-11 4:24 PM
To: Razvan Cojocaru
Cc: scala-debate [at] googlegroups [dot] com
Subject: Re: [scala-debate] pipeline operator

 

> That seems like it would make the code more readable in some scenarios…

 

Am I missing something?

 

def prices = csv

                .split (‘t’)

                .skip(1)

                 .map (…)

                 .filter (…)

                 .map (…)


seems pretty equivalently readable to me.

On 6 April 2011 15:16, Razvan Cojocaru <pub [at] razie [dot] com> wrote:

I was watching an F# presentation and I must say that I like the ‘pipeline operator’ |>

 

It has the potential to get rid of the “lines are getting too long” problem.

 

let prices =

                Csv.split ([|’n’|])

                |> Seq.skip 1

                |> Seq.map (…)

                |> Seq.filter (…)

                |> Seq.map (…)

 

Instead of scala’s

 

def prices = csv.split (‘t’).skip(1).map (…).filter (…).map (…)

 

That seems like it would make the code more readable in some scenarios…

 

 

 


Razvan Cojocaru 3
Joined: 2010-07-28,
User offline. Last seen 42 years 45 weeks ago.
RE: pipeline operator

Cool. I don’t think .Net is functional at the core and now I’m left wondering how are they grafting the map() on it…

 

From: jqbalter [at] gmail [dot] com [mailto:jqbalter [at] gmail [dot] com] On Behalf Of Jim Balter
Sent: April-06-11 5:42 PM
To: Razvan Cojocaru
Cc: Volodymyr Kyrychenko; scala-debate [at] googlegroups [dot] com
Subject: Re: [scala-debate] pipeline operator

 

Seq is .NET/CLR stuff.

-- Jim


On Wed, Apr 6, 2011 at 2:09 PM, Razvan Cojocaru <pub [at] razie [dot] com> wrote:

Yes - I did find it funny them not using object notation x.map(_) but static library "Seq.map(_)(x)". I know that's Haskell style but I don't appreciate learning statics without content assist. The world is not made of 3 letters... well, biblically it is but you know what I mean ;)



-----Original Message-----
From: scala-debate [at] googlegroups [dot] com [mailto:scala-debate [at] googlegroups [dot] com] On Behalf Of Volodymyr Kyrychenko
Sent: April-06-11 4:35 PM
To: scala-debate [at] googlegroups [dot] com
Subject: Re: [scala-debate] pipeline operator

Razvan Cojocaru wrote:
> I was watching an F# presentation and I must say that I like the
> ‘pipeline operator’ |>
....
> That seems like it would make the code more readable in some
> scenarios…

Pipeline is a replacement for:

f1(f2(f3(f4(x))))

x |> f4 |> f3 |> f2 |> f1

not for the:

x.f4.f3.f2.f1

--
Best Regards,
Volodymyr Kyrychenko

 

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