- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Re: pipeline operator
You'd get the same error with |> (whose implementation, by the way, can be found on many places).
Alas,
Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.
scala> List(1, 2, 3)
res0: List[Int] = List(1, 2, 3)
scala> (List(1, 2, 3)
| .map(_ + 2)
| )
res1: List[Int] = List(3, 4, 5)
scala> List(1, 2, 3)
res2: List[Int] = List(1, 2, 3)
scala> .map(_ + 2)
res3: List[Int] = List(3, 4, 5)
scala> List(1, 2, 3).
| map(_ + 2)
res4: List[Int] = List(3, 4, 5)
On Wed, Apr 6, 2011 at 17:52, Razvan Cojocaru <pub [at] razie [dot] com> wrote:
--
Daniel C. Sobral
I travel to the future all the time.
Alas,
Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.
scala> List(1, 2, 3)
res0: List[Int] = List(1, 2, 3)
scala> (List(1, 2, 3)
| .map(_ + 2)
| )
res1: List[Int] = List(3, 4, 5)
scala> List(1, 2, 3)
res2: List[Int] = List(1, 2, 3)
scala> .map(_ + 2)
res3: List[Int] = List(3, 4, 5)
scala> List(1, 2, 3).
| map(_ + 2)
res4: List[Int] = List(3, 4, 5)
On Wed, Apr 6, 2011 at 17:52, 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…
--
Daniel C. Sobral
I travel to the future all the time.










RE: pipeline operator
…but to answer your answer… yes, I know it would fail if it wasn’t a symbol specially understood by the compiler or some other special construct I can’t think of, which is why I posted under “debate” J
Chances of the language spec changing for something like this are sub-zero, but I thought I’d note it anyways, in case there was something…like the simple () you mentioned J
From: Daniel Sobral [mailto:dcsobral [at] gmail [dot] com]
Sent: April-06-11 4:56 PM
To: Razvan Cojocaru
Cc: Paul Hudson; scala-debate [at] googlegroups [dot] com
Subject: Re: [scala-debate] pipeline operator
You'd get the same error with |> (whose implementation, by the way, can be found on many places).
Alas,
Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.
scala> List(1, 2, 3)
res0: List[Int] = List(1, 2, 3)
scala> (List(1, 2, 3)
| .map(_ + 2)
| )
res1: List[Int] = List(3, 4, 5)
scala> List(1, 2, 3)
res2: List[Int] = List(1, 2, 3)
scala> .map(_ + 2)
res3: List[Int] = List(3, 4, 5)
scala> List(1, 2, 3).
| map(_ + 2)
res4: List[Int] = List(3, 4, 5)
On Wed, Apr 6, 2011 at 17:52, 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…
--
Daniel C. Sobral
I travel to the future all the time.
RE: pipeline operator
HEY… how does this work? It just failed in my example you replied to…
scala> List(1, 2, 3)
res2: List[Int] = List(1, 2, 3)
scala> .map(_ + 2)
res3: List[Int] = List(3, 4, 5)
if I follow the same sequence you did, it works. If I just type this directly after starting the interpreter, it doesn’t work!
From: Daniel Sobral [mailto:dcsobral [at] gmail [dot] com]
Sent: April-06-11 4:56 PM
To: Razvan Cojocaru
Cc: Paul Hudson; scala-debate [at] googlegroups [dot] com
Subject: Re: [scala-debate] pipeline operator
You'd get the same error with |> (whose implementation, by the way, can be found on many places).
Alas,
Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.
scala> List(1, 2, 3)
res0: List[Int] = List(1, 2, 3)
scala> (List(1, 2, 3)
| .map(_ + 2)
| )
res1: List[Int] = List(3, 4, 5)
scala> List(1, 2, 3)
res2: List[Int] = List(1, 2, 3)
scala> .map(_ + 2)
res3: List[Int] = List(3, 4, 5)
scala> List(1, 2, 3).
| map(_ + 2)
res4: List[Int] = List(3, 4, 5)
On Wed, Apr 6, 2011 at 17:52, 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…
--
Daniel C. Sobral
I travel to the future all the time.
Re: pipeline operator
Answering your other question, it doesn't matter if |> was part of the language or not, because it has nothing to do with the language. This:
val x = List(1, 2, 3)
.map(_ + 2)
works inside compiled Scala code. It works in :paste mode on REPL (need a recent trunk). It just doesn't work on plain REPL because REPL has no way of knowing you are going to continue an expression. The compiler, on the other hand, look at the next line and notices the expression is continuing there.
The same thing will happen with |>, even if it is built in the language.
On Wed, Apr 6, 2011 at 18:00, Razvan Cojocaru <pub [at] razie [dot] com> wrote:
--
Daniel C. Sobral
I travel to the future all the time.
RE: pipeline operator
Hold on… if |> is just any operator, it has no reason to behave any different than + or ++, which won’t work – classic problem – don’t think this was addressed in 2.9?
1
+2
^ compile error
. is interpreted differently by the compiler. In that sense, the |> I was looking for was different than the + because the compiler would treat it more like . than + at which point we gotta ask how is it would be different than . so you are right. Dot should suffice for methods.
For functional composition, what they really mean by pipeline, no operator is treated differently so nothing would work short of a ‘special |>’ operator, which is what I guess I was looking for, but fuzzed it up by using ‘.’ in my example L
Thanks,
Razie
From: Daniel Sobral [mailto:dcsobral [at] gmail [dot] com]
Sent: April-06-11 5:26 PM
To: Razvan Cojocaru
Cc: Paul Hudson; scala-debate [at] googlegroups [dot] com
Subject: Re: [scala-debate] pipeline operator
It works _except_ for res0. What happens is that REPL provides the last res automatically if the line starts with a dot.
Answering your other question, it doesn't matter if |> was part of the language or not, because it has nothing to do with the language. This:
val x = List(1, 2, 3)
.map(_ + 2)
works inside compiled Scala code. It works in :paste mode on REPL (need a recent trunk). It just doesn't work on plain REPL because REPL has no way of knowing you are going to continue an expression. The compiler, on the other hand, look at the next line and notices the expression is continuing there.
The same thing will happen with |>, even if it is built in the language.
On Wed, Apr 6, 2011 at 18:00, Razvan Cojocaru <pub [at] razie [dot] com> wrote:
HEY… how does this work? It just failed in my example you replied to…
scala> List(1, 2, 3)
res2: List[Int] = List(1, 2, 3)
scala> .map(_ + 2)
res3: List[Int] = List(3, 4, 5)
if I follow the same sequence you did, it works. If I just type this directly after starting the interpreter, it doesn’t work!
From: Daniel Sobral [mailto:dcsobral [at] gmail [dot] com]
Sent: April-06-11 4:56 PM
To: Razvan Cojocaru
Cc: Paul Hudson; scala-debate [at] googlegroups [dot] com
Subject: Re: [scala-debate] pipeline operator
You'd get the same error with |> (whose implementation, by the way, can be found on many places).
Alas,
Welcome to Scala version 2.8.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.
scala> List(1, 2, 3)
res0: List[Int] = List(1, 2, 3)
scala> (List(1, 2, 3)
| .map(_ + 2)
| )
res1: List[Int] = List(3, 4, 5)
scala> List(1, 2, 3)
res2: List[Int] = List(1, 2, 3)
scala> .map(_ + 2)
res3: List[Int] = List(3, 4, 5)
scala> List(1, 2, 3).
| map(_ + 2)
res4: List[Int] = List(3, 4, 5)
On Wed, Apr 6, 2011 at 17:52, 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…
--
Daniel C. Sobral
I travel to the future all the time.
--
Daniel C. Sobral
I travel to the future all the time.
Re: pipeline operator
As long as you put your expression inside parenthesis, you can make it go through multiple lines with infix operator style.
On Wed, Apr 6, 2011 at 18:40, Razvan Cojocaru <pub [at] razie [dot] com> wrote:
--
Daniel C. Sobral
I travel to the future all the time.
Re: pipeline operator
I had forgotten it, so it would be a good reminder to all who read the guide...
Thanks,Razvan
On 2011-04-06, at 8:09 PM, Daniel Sobral <dcsobral [at] gmail [dot] com> wrote: