- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Using the foldLeft operator /:
Tue, 2009-04-21, 13:32
I am trying to figure out how to use the /: operator in scala.
According to the doc it's similar to foldLeft
from http://www.scala-lang.org/docu/files/api/scala/Iterable.html#%2F%3A%28B%29:
Similar to foldLeft but can be used as an operator with the order of
list and zero arguments reversed. That is, z /: xs is the same as xs
foldLeft z
So, say I want to sum a range of numbers:
(1 to 100).foldLeft(0)(_+_)
This works, as does:
(1 to 100)./:(0)(_+_)
But how do I write the same expression using /: without the "."?
I have tried:
scala> (1 to 100) /:(0)(_+_)
:5: error: 0 of type Int(0) does not take parameters
as well as:
(_+_) /: (1 to 100)
:5: error: missing parameter type for expanded function
((x$1, x$2) => x$1.$plus(x$2))
(_+_) /: (1 to 100)
How should I sum a set off numbers using the /: operator? How would I
do it if I need to change to BigInt on the fly (because off int
overflow)?
The last one is easy with foldLeft:
(1 to 100).foldLeft(BigInt(0))(_+_)
Tue, 2009-04-21, 13:47
#2
Re: Using the foldLeft operator /:
scala> (0 /: (1 to 100)) (_ + _)
res1: Int = 5050
On Tue, Apr 21, 2009 at 14:30, Ståle Undheim wrote:
> I am trying to figure out how to use the /: operator in scala.
> According to the doc it's similar to foldLeft
> from http://www.scala-lang.org/docu/files/api/scala/Iterable.html#%2F%3A%28B%29:
>
> Similar to foldLeft but can be used as an operator with the order of
> list and zero arguments reversed. That is, z /: xs is the same as xs
> foldLeft z
>
> So, say I want to sum a range of numbers:
> (1 to 100).foldLeft(0)(_+_)
>
> This works, as does:
> (1 to 100)./:(0)(_+_)
>
> But how do I write the same expression using /: without the "."?
>
> I have tried:
> scala> (1 to 100) /:(0)(_+_)
> :5: error: 0 of type Int(0) does not take parameters
>
> as well as:
> (_+_) /: (1 to 100)
> :5: error: missing parameter type for expanded function
> ((x$1, x$2) => x$1.$plus(x$2))
> (_+_) /: (1 to 100)
>
> How should I sum a set off numbers using the /: operator? How would I
> do it if I need to change to BigInt on the fly (because off int
> overflow)?
> The last one is easy with foldLeft:
> (1 to 100).foldLeft(BigInt(0))(_+_)
>
Wed, 2009-07-22, 17:47
#3
Pattern matching on Stream using #::
On trunk I can use #:: as an infix way of writing Stream.cons:
scala> 1 #:: Stream.empty
res2: scala.collection.immutable.Stream[Int] = Stream(1, ?)
I like it. There's also an extractor so you can pattern match
the same way:
scala> Stream(1,2,3) match { case x #:: xs => (x,xs) }
res3: (Int, scala.collection.immutable.Stream[Int]) = (1,Stream(2, ?))
I like this too, but it doesn't work unless you do this first:
import Stream.#::
it seems like this may have been an oversight? shall I open a ticket?
I don't need to import :: in order to pattern match lists (thanks
to some magic in Predef.scala)
Wed, 2009-07-22, 17:57
#4
Re: Pattern matching on Stream using #::
+1 on putting it in Predef
On Thu, Jul 23, 2009 at 12:37 AM, Seth Tisue <seth [at] tisue [dot] net> wrote:
--
.......__o
.......\<,
....( )/ ( )...
On Thu, Jul 23, 2009 at 12:37 AM, Seth Tisue <seth [at] tisue [dot] net> wrote:
On trunk I can use #:: as an infix way of writing Stream.cons:
scala> 1 #:: Stream.empty
res2: scala.collection.immutable.Stream[Int] = Stream(1, ?)
I like it. There's also an extractor so you can pattern match
the same way:
scala> Stream(1,2,3) match { case x #:: xs => (x,xs) }
res3: (Int, scala.collection.immutable.Stream[Int]) = (1,Stream(2, ?))
I like this too, but it doesn't work unless you do this first:
import Stream.#::
it seems like this may have been an oversight? shall I open a ticket?
I don't need to import :: in order to pattern match lists (thanks
to some magic in Predef.scala)
--
Seth Tisue / http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/
--
.......__o
.......\<,
....( )/ ( )...
Wed, 2009-07-22, 18:07
#5
Fwd: Pattern matching on Stream using #::
Forgot, reply to all...
---------- Forwarded message ----------
From: John Nilsson
Date: Wed, Jul 22, 2009 at 6:54 PM
Subject: Re: [scala] Pattern matching on Stream using #::
To: Walter Chang
Why #:: and not just ::
Is it because they are objects, and you can't overload objects? If so,
should anything be done to fix this situation?
BR,
John
On Wed, Jul 22, 2009 at 6:45 PM, Walter Chang wrote:
> +1 on putting it in Predef
>
> On Thu, Jul 23, 2009 at 12:37 AM, Seth Tisue wrote:
>>
>> On trunk I can use #:: as an infix way of writing Stream.cons:
>>
>> scala> 1 #:: Stream.empty
>> res2: scala.collection.immutable.Stream[Int] = Stream(1, ?)
>>
>> I like it. There's also an extractor so you can pattern match
>> the same way:
>>
>> scala> Stream(1,2,3) match { case x #:: xs => (x,xs) }
>> res3: (Int, scala.collection.immutable.Stream[Int]) = (1,Stream(2, ?))
>>
>> I like this too, but it doesn't work unless you do this first:
>>
>> import Stream.#::
>>
>> it seems like this may have been an oversight? shall I open a ticket?
>> I don't need to import :: in order to pattern match lists (thanks
>> to some magic in Predef.scala)
>>
>> --
>> Seth Tisue / http://tisue.net
>> lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/
>
>
>
> --
> .......__o
> .......\<,
> ....( )/ ( )...
>
Wed, 2009-07-22, 18:57
#6
Re: Fwd: Pattern matching on Stream using #::
Dne Wed, 22 Jul 2009 16:55:14 -0000 John Nilsson
napsal(a):
> Forgot, reply to all...
>
>
> ---------- Forwarded message ----------
> From: John Nilsson
> Date: Wed, Jul 22, 2009 at 6:54 PM
> Subject: Re: [scala] Pattern matching on Stream using #::
> To: Walter Chang
>
>
> Why #:: and not just ::
>
> Is it because they are objects, and you can't overload objects? If so,
> should anything be done to fix this situation?
Actually, you don't need :: to be "overloaded object". :: just needs to be
a value with unapply method handling both alternatives (both Streams and
Lists). There's a little obstacle that the tail method is not an abstract
method of a common base of these two, but that can be solved easily in
Scala. See:
scala> val fff = new { def unapply[A, T](x : { def head : A; def tail : T;
def isEmpty : Boolean }) = if(x.isEmpty) None else Some(x.head, x.tail) }
fff: java.lang.Object{def unapply[A,T](AnyRef{def head: A; def tail: T;
def isEmpty: Boolean}): Option[(A, T)]} = $anon$1@32b90a0
scala> fff.unapply(List(1,2))
res112: Option[(Int, List[Int])] = Some((1,List(2)))
scala> List(1,2,3) match { case x fff xs => (x, xs
| )}
res113: (Int, List[Int]) = (1,List(2, 3))
scala> Stream(1,2,3) match { case x fff xs => (x, xs)}
res114: (Int, Stream[Int]) = (1,Stream(2, ?))
Regards
Jiri Palecek
PS: please, Cc: me in replies.
> BR,
> John
>
> On Wed, Jul 22, 2009 at 6:45 PM, Walter Chang wrote:
>> +1 on putting it in Predef
>>
>> On Thu, Jul 23, 2009 at 12:37 AM, Seth Tisue wrote:
>>>
>>> On trunk I can use #:: as an infix way of writing Stream.cons:
>>>
>>> scala> 1 #:: Stream.empty
>>> res2: scala.collection.immutable.Stream[Int] = Stream(1, ?)
>>>
>>> I like it. There's also an extractor so you can pattern match
>>> the same way:
>>>
>>> scala> Stream(1,2,3) match { case x #:: xs => (x,xs) }
>>> res3: (Int, scala.collection.immutable.Stream[Int]) = (1,Stream(2, ?))
>>>
>>> I like this too, but it doesn't work unless you do this first:
>>>
>>> import Stream.#::
>>>
>>> it seems like this may have been an oversight? shall I open a ticket?
>>> I don't need to import :: in order to pattern match lists (thanks
>>> to some magic in Predef.scala)
>>>
>>> --
>>> Seth Tisue / http://tisue.net
>>> lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/
>>
>>
>>
>> --
>> .......__o
>> .......\<,
>> ....( )/ ( )...
>>
>
Mon, 2009-08-03, 11:57
#7
Re: Pattern matching on Stream using #::
The wrapper for :: is actually in the scala package object, not in
Predef. I have put another one fdor #:: in he same place now, so
extractors should work with tomorrows nightly. Thanks for the
suggestion.
Cheers
/: ends with a colon, thus it is right-associative, i.e., it is a member of the object on its right, not its left.
2009/4/21 Ståle Undheim <staaleu [at] gmail [dot] com>