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

Scala collections

8 replies
etorreborre
Joined: 2008-09-03,
User offline. Last seen 1 year 22 weeks ago.

Hi,

I think I've read that Scala collection library is undergoing change.

I had this issue today where I needed Seq.map to return the underlying type
of my seq:

List(1, 2).map(_ + 1) // returns a Seq and not a List

Is it planned to have the Seq trait methods return the underlying collection
type when possible?
What kind of side effects would there be with such a design?
Is my question irrelevant and the sign that something is wrong in the way I
handle collections in my program?

Thanks.

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala collections
Eric,

2.7.2 does this already:
scala> List(1,2,3).map(_ + 1)
res62: List[Int] = List(2, 3, 4)

scala> Array(1,2,3).map(_ + 1)
res63: Array[Int] = Array(2, 3, 4)

What collections are not returning the correct type?

Thanks,

David

On Mon, Dec 29, 2008 at 5:35 PM, Eric Torreborre <etorreborre [at] yahoo [dot] com> wrote:

Hi,

I think I've read that Scala collection library is undergoing change.

I had this issue today where I needed Seq.map to return the underlying type
of my seq:

List(1, 2).map(_ + 1) // returns a Seq and not a List

Is it planned to have the Seq trait methods return the underlying collection
type when possible?
What kind of side effects would there be with such a design?
Is my question irrelevant and the sign that something is wrong in the way I
handle collections in my program?

Thanks.


--
View this message in context: http://www.nabble.com/Scala-collections-tp21211931p21211931.html
Sent from the Scala mailing list archive at Nabble.com.




--
Lift, the simply functional web framework http://liftweb.net
Collaborative Task Management http://much4.us
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp
etorreborre
Joined: 2008-09-03,
User offline. Last seen 1 year 22 weeks ago.
Re: mapFilter on Seq (was: Scala collections)

You're right, I need to be more explicit with what I'm doing,...

My issue is that this doesn't compile:

val l1: Seq[Int] = List(1, 2)
val l2: List[Int] = l1.map((x:Int) => x)

:5: error: type mismatch;
found : Seq[Int]
required: List[Int]
val l2: List[Int] = l.map((x:Int) => x)

If I change the type of l1 to List[Int] this is ok.

Reading this I realize that this is perfectly understandable,...

So let's transform that to another question because my objective was to add
a mapFilter method on Seq objects which would combine a map and a filter in
one pass:

something like:

def mapFilter(seq: Seq[T], f: U => Option[U]) = {
// to be implemented with something like
seq.map(f(u)).filter(_.isDefined).map(_.get)
}

Oh,... time to answer my own question. This works ok:

List(1, 2).flatMap((x:Int) => if (x%2 == 0) Some(x*2) else None)

// returns List(4)

There's no need for a mapFilter function and I need more of this Monadic
stuff in my veins.

Thanks,

Eric.

David Pollak-4 wrote:
>
> Eric,
>
> 2.7.2 does this already:
> scala> List(1,2,3).map(_ + 1)
> res62: List[Int] = List(2, 3, 4)
>
> scala> Array(1,2,3).map(_ + 1)
> res63: Array[Int] = Array(2, 3, 4)
>
> What collections are not returning the correct type?
>
> Thanks,
>
> David
>
> On Mon, Dec 29, 2008 at 5:35 PM, Eric Torreborre
> wrote:
>
>>
>> Hi,
>>
>> I think I've read that Scala collection library is undergoing change.
>>
>> I had this issue today where I needed Seq.map to return the underlying
>> type
>> of my seq:
>>
>> List(1, 2).map(_ + 1) // returns a Seq and not a List
>>
>> Is it planned to have the Seq trait methods return the underlying
>> collection
>> type when possible?
>> What kind of side effects would there be with such a design?
>> Is my question irrelevant and the sign that something is wrong in the way
>> I
>> handle collections in my program?
>>
>> Thanks.
>>
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Scala-collections-tp21211931p21211931.html
>> Sent from the Scala mailing list archive at Nabble.com.
>>
>>
>
>

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: mapFilter on Seq (was: Scala collections)

Take a flatMap and to foldLeft's and call me in the morning. ;-)

On Dec 29, 2008 7:37 PM, "Eric Torreborre" <etorreborre [at] yahoo [dot] com> wrote:


You're right, I need to be more explicit with what I'm doing,...

My issue is that this doesn't compile:

val l1: Seq[Int] = List(1, 2)
val l2: List[Int] = l1.map((x:Int) => x)

<console>:5: error: type mismatch;
 found   : Seq[Int]
 required: List[Int]
      val l2: List[Int] = l.map((x:Int) => x)

If I change the type of l1 to List[Int] this is ok.

Reading this I realize that this is perfectly understandable,...

So let's transform that to another question because my objective was to add
a mapFilter method on Seq objects which would combine a map and a filter in
one pass:

something like:

def mapFilter(seq: Seq[T], f: U => Option[U]) = {
 // to be implemented with something like
seq.map(f(u)).filter(_.isDefined).map(_.get)
}

Oh,... time to answer my own question. This works ok:

List(1, 2).flatMap((x:Int) => if (x%2 == 0) Some(x*2) else None)

// returns List(4)

There's no need for a mapFilter function and I need more of this Monadic
stuff in my veins.

Thanks,

Eric.








David Pollak-4 wrote:
>
> Eric,
>
> 2.7.2 does this already:
> scala> List(1,2,3).map(_ + 1)
> res62: List[Int] = List(2, 3, 4)
>
> scala> Array(1,2,3).map(_ + 1)
> res63: Array[Int] = Array(2, 3, 4)
>
> What collections are not returning the correct type?
>
> Thanks,
>
> David
>
> On Mon, Dec 29, 2008 at 5:35 PM, Eric Torreborre
> <etorreborre [at] yahoo [dot] com>wrote:
>
>>
>> Hi,
>>
>> I think I've read that Scala collection library is undergoing change.
>>
>> I had this issue today where I needed Seq.map to return the underlying
>> type
>> of my seq:
>>
>> List(1, 2).map(_ + 1) // returns a Seq and not a List
>>
>> Is it planned to have the Seq trait methods return the underlying
>> collection
>> type when possible?
>> What kind of side effects would there be with such a design?
>> Is my question irrelevant and the sign that something is wrong in the way
>> I
>> handle collections in my program?
>>
>> Thanks.
>>
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Scala-collections-tp21211931p21211931.html
>> Sent from the Scala mailing list archive at Nabble.com.
>>
>>
>
>
> --
> Lift, the simply functional web framework http://liftweb.net
> Collaborative Task Management http://much4.us
> Follow me: http://twitter.com/dpp
> Git some: http://github.com/dpp
>
>

--
View this message in context: http://www.nabble.com/Scala-collections-tp21211931p21212744.html
Sent from the Scala mailing list archive at Nabble.com.

Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 3 days ago.
Re: mapFilter on Seq (was: Scala collections)
If flatMap didn't already do the trick, the higher-kinded collection library would let you write your mapFilter function, looking maybe something like:

  def mapFilter[S, T, C[X] <: Iterable[X]](c: C[S], f: S => Option[T]): C[T] = {
    val b = c.newBuilder[T]
    for (x <- c; y <- f(x)) b ++= y
    b.result
  }

--j

On Mon, Dec 29, 2008 at 9:37 PM, Eric Torreborre <etorreborre [at] yahoo [dot] com> wrote:

You're right, I need to be more explicit with what I'm doing,...

My issue is that this doesn't compile:

val l1: Seq[Int] = List(1, 2)
val l2: List[Int] = l1.map((x:Int) => x)

<console>:5: error: type mismatch;
 found   : Seq[Int]
 required: List[Int]
      val l2: List[Int] = l.map((x:Int) => x)

If I change the type of l1 to List[Int] this is ok.

Reading this I realize that this is perfectly understandable,...

So let's transform that to another question because my objective was to add
a mapFilter method on Seq objects which would combine a map and a filter in
one pass:

something like:

def mapFilter(seq: Seq[T], f: U => Option[U]) = {
 // to be implemented with something like
seq.map(f(u)).filter(_.isDefined).map(_.get)
}

Oh,... time to answer my own question. This works ok:

List(1, 2).flatMap((x:Int) => if (x%2 == 0) Some(x*2) else None)

// returns List(4)

There's no need for a mapFilter function and I need more of this Monadic
stuff in my veins.

Thanks,

Eric.








David Pollak-4 wrote:
>
> Eric,
>
> 2.7.2 does this already:
> scala> List(1,2,3).map(_ + 1)
> res62: List[Int] = List(2, 3, 4)
>
> scala> Array(1,2,3).map(_ + 1)
> res63: Array[Int] = Array(2, 3, 4)
>
> What collections are not returning the correct type?
>
> Thanks,
>
> David
>
> On Mon, Dec 29, 2008 at 5:35 PM, Eric Torreborre
> <etorreborre [at] yahoo [dot] com>wrote:
>
>>
>> Hi,
>>
>> I think I've read that Scala collection library is undergoing change.
>>
>> I had this issue today where I needed Seq.map to return the underlying
>> type
>> of my seq:
>>
>> List(1, 2).map(_ + 1) // returns a Seq and not a List
>>
>> Is it planned to have the Seq trait methods return the underlying
>> collection
>> type when possible?
>> What kind of side effects would there be with such a design?
>> Is my question irrelevant and the sign that something is wrong in the way
>> I
>> handle collections in my program?
>>
>> Thanks.
>>
>>
>> --
>> View this message in context:
>> http://www.nabble.com/Scala-collections-tp21211931p21211931.html
>> Sent from the Scala mailing list archive at Nabble.com.
>>
>>
>
>
> --
> Lift, the simply functional web framework http://liftweb.net
> Collaborative Task Management http://much4.us
> Follow me: http://twitter.com/dpp
> Git some: http://github.com/dpp
>
>

--
View this message in context: http://www.nabble.com/Scala-collections-tp21211931p21212744.html
Sent from the Scala mailing list archive at Nabble.com.


etorreborre
Joined: 2008-09-03,
User offline. Last seen 1 year 22 weeks ago.
Re: mapFilter on Seq (was: Scala collections)

Yes, I also tried doing something like that when I remembered that I needed
the "Builder" abstraction described in the "Generics of a higher kind"
paper.

And this is going to be part of the rework of the Scala collections, right?

E.

Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 3 days ago.
Re: mapFilter on Seq (was: Scala collections)
Yup, Builder is baked in to the new collections.

You can check out the (in progress) code:

http://lampsvn.epfl.ch/trac/scala/browser/scala/trunk/src/library/scalax/collection/

--j

On Tue, Dec 30, 2008 at 1:19 AM, Eric Torreborre <etorreborre [at] yahoo [dot] com> wrote:

Yes, I also tried doing something like that when I remembered that I needed
the "Builder" abstraction described in the "Generics of a higher kind"
paper.

And this is going to be part of the rework of the Scala collections, right?

E.

--
View this message in context: http://www.nabble.com/Scala-collections-tp21211931p21213910.html
Sent from the Scala mailing list archive at Nabble.com.


odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: mapFilter on Seq (was: Scala collections)

On Tue, Dec 30, 2008 at 8:19 AM, Eric Torreborre wrote:
>
> Yes, I also tried doing something like that when I remembered that I needed
> the "Builder" abstraction described in the "Generics of a higher kind"
> paper.
>
> And this is going to be part of the rework of the Scala collections, right?
>
Yes, it is.

But I don't think you need to define mapFilter in any case. flatMap
alone does the trick:

scala> val xs = List(1, 2, 3)
xs: List[Int] = List(1, 2, 3)

scala> def f(x: Int) = if (x % 2 == 1) Some(x) else None
f: (Int)Option[Int]

scala> xs flatMap f
res0: List[Int] = List(1, 3)

Or, if you prefer:

for (xo <- List(xs); x <- xo) yield x

The for expression expands into the same flatMap.

Cheers

Tony Morris
Joined: 2008-12-19,
User offline. Last seen 30 weeks 4 days ago.
Re: mapFilter on Seq

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

Eric Torreborre wrote:
> Oh,... time to answer my own question. This works ok:
>
> List(1, 2).flatMap((x:Int) => if (x%2 == 0) Some(x*2) else None)
>
> // returns List(4)
>
> There's no need for a mapFilter function and I need more of this
> Monadic stuff in my veins.
>
> Thanks,
>
> Eric.
>

Hi Eric,
You can write mapFilter using Scalaz which has a MonadEmptyPlus data
structure. I suggest you do not use the available subversion of the
List.flatMap type signature from which to learn ("Monadic stuff in my
veins").

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

S, K and I ought to be enough for anybody.

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

iEYEARECAAYFAklai8wACgkQmnpgrYe6r60TbwCgkJdHJaeOXgZpzbCZx1/WBjCk
E0oAnRxkId2pTqjM2pTBHThxJkvve9uf
=VxCb
-----END PGP SIGNATURE-----

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