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

Partition with Either on Traversable

6 replies
Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 6 days ago.
What does everyone think about a partionEither method on traversable as a way to partition into separately typed lists.   You can use it as follows:

scala> val x = List(1, "String", 3)
x: List[Any] = List(1, String, 3)

scala> x.partitionEither {
     |  case i : Int => Left(i)
     |  case s : String => Right(s)
     | }
res0: (List[Int], List[String]) = (List(1, 3),List(String))

scala> val y = Vector(1,"HAI",3)
y: scala.collection.immutable.Vector[Any] = Vector(1, HAI, 3)

scala> y.partitionEither {
     |  case i : Int => Left(i)   
     |  case s : String => Right(s)
     | }
res1: (scala.collection.immutable.Vector[Int], scala.collection.immutable.Vector[String]) = (Vector(1, 3),Vector(HAI))

scala> val z = collection.mutable.BitSet(1,2,3)
z: scala.collection.mutable.BitSet = BitSet(1, 2, 3)

scala> z.partitionEither {          
     | case i if i % 2 == 0 => Left("Even")
     | case i => Right(i)                 
     | }
res3: (scala.collection.mutable.Set[java.lang.String], scala.collection.mutable.BitSet) = (Set(Even),BitSet(1, 3))



I have a patch I could commit if we think this is a generally useful method on the collections.   I personally feel this method provides a nice way to partition into two separately-typed lists.   What does everyone think?



Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 6 days ago.
Re: Partition with Either on Traversable
I should mention that this method is based on a recent StackOverflow question.

On Thu, Aug 26, 2010 at 10:39 PM, Josh Suereth <joshua.suereth@gmail.com> wrote:
What does everyone think about a partionEither method on traversable as a way to partition into separately typed lists.   You can use it as follows:

scala> val x = List(1, "String", 3)
x: List[Any] = List(1, String, 3)

scala> x.partitionEither {
     |  case i : Int => Left(i)
     |  case s : String => Right(s)
     | }
res0: (List[Int], List[String]) = (List(1, 3),List(String))

scala> val y = Vector(1,"HAI",3)
y: scala.collection.immutable.Vector[Any] = Vector(1, HAI, 3)

scala> y.partitionEither {
     |  case i : Int => Left(i)   
     |  case s : String => Right(s)
     | }
res1: (scala.collection.immutable.Vector[Int], scala.collection.immutable.Vector[String]) = (Vector(1, 3),Vector(HAI))

scala> val z = collection.mutable.BitSet(1,2,3)
z: scala.collection.mutable.BitSet = BitSet(1, 2, 3)

scala> z.partitionEither {          
     | case i if i % 2 == 0 => Left("Even")
     | case i => Right(i)                 
     | }
res3: (scala.collection.mutable.Set[java.lang.String], scala.collection.mutable.BitSet) = (Set(Even),BitSet(1, 3))



I have a patch I could commit if we think this is a generally useful method on the collections.   I personally feel this method provides a nice way to partition into two separately-typed lists.   What does everyone think?




Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: Partition with Either on Traversable

Such a list is better represented List[Either[Int, String]]. I suspect
this would then alter your question.

Josh Suereth wrote:
> What does everyone think about a partionEither method on traversable
> as a way to partition into separately typed lists. You can use it as
> follows:
>
> scala> val x = List(1, "String", 3)
> x: List[Any] = List(1, String, 3)
>
> scala> x.partitionEither {
> | case i : Int => Left(i)
> | case s : String => Right(s)
> | }
> res0: (List[Int], List[String]) = (List(1, 3),List(String))
>
> scala> val y = Vector(1,"HAI",3)
> y: scala.collection.immutable.Vector[Any] = Vector(1, HAI, 3)
>
> scala> y.partitionEither {
> | case i : Int => Left(i)
> | case s : String => Right(s)
> | }
> res1: (scala.collection.immutable.Vector[Int],
> scala.collection.immutable.Vector[String]) = (Vector(1, 3),Vector(HAI))
>
> scala> val z = collection.mutable.BitSet(1,2,3)
> z: scala.collection.mutable.BitSet = BitSet(1, 2, 3)
>
> scala> z.partitionEither {
> | case i if i % 2 == 0 => Left("Even")
> | case i => Right(i)
> | }
> res3: (scala.collection.mutable.Set[java.lang.String],
> scala.collection.mutable.BitSet) = (Set(Even),BitSet(1, 3))
>
>
>
> I have a patch I could commit if we
> think this is a generally useful method on the collections. I
> personally feel this method provides a nice way to partition into two
> separately-typed lists. What does everyone think?
>
>
>

Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: Partition with Either on Traversable
I like it much better than this monstrosity I wrote, namely:
  /**
   * Splits a list by type, returning a tuple of lists.  Elements of type B are returned in the first list, and Cs are
   * returned in the second list.  Any element that conforms to both B and C will be returned in the first list. 
   */
  def split[A <: AnyRef, B <: A, C <: A](source: List[A], bsInProgress: List[B], csInProgress: List[C])
                                        (implicit manifestB: Manifest[B], manifestC: Manifest[C]): (List[B], List[C]) = ...

... so much so that I'm going to write my own without looking at your code. ;)
-0xe1a
Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 6 days ago.
Re: Partition with Either on Traversable
Consider any code I post here under a BSD-license or just ask.   No need to rewrite from scratch ;)

On Fri, Aug 27, 2010 at 1:42 PM, Alex Cruise <alex@cluonflux.com> wrote:
I like it much better than this monstrosity I wrote, namely:
  /**
   * Splits a list by type, returning a tuple of lists.  Elements of type B are returned in the first list, and Cs are
   * returned in the second list.  Any element that conforms to both B and C will be returned in the first list. 
   */
  def split[A <: AnyRef, B <: A, C <: A](source: List[A], bsInProgress: List[B], csInProgress: List[C])
                                        (implicit manifestB: Manifest[B], manifestC: Manifest[C]): (List[B], List[C]) = ...

... so much so that I'm going to write my own without looking at your code. ;)
-0xe1a

Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: Partition with Either on Traversable
On Fri, Aug 27, 2010 at 10:47 AM, Josh Suereth <joshua.suereth@gmail.com> wrote:
Consider any code I post here under a BSD-license or just ask.   No need to rewrite from scratch ;)

Oh, it's just for the challenge/fun, not because of any copyright fear. :)
-0xe1a
Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 6 days ago.
Re: Partition with Either on Traversable
I'm going to guess that with the all the tearful emails praising the genius and elegance of this function, that I should commit this right now, lest users gnash their teeth in anticipation of the blessed power it entails.

In other words:  I'm assuming the lack of interest implies this is not a huge win to include in the Standard library.   I'll just hold on to the patch for now and see if any other interest shows up.

- Josh

On Fri, Aug 27, 2010 at 2:19 PM, Alex Cruise <alex@cluonflux.com> wrote:
On Fri, Aug 27, 2010 at 10:47 AM, Josh Suereth <joshua.suereth@gmail.com> wrote:
Consider any code I post here under a BSD-license or just ask.   No need to rewrite from scratch ;)

Oh, it's just for the challenge/fun, not because of any copyright fear. :)
-0xe1a

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