- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Partition with Either on Traversable
Fri, 2010-08-27, 03:38
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?
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?
Fri, 2010-08-27, 10:37
#2
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?
>
>
>
Fri, 2010-08-27, 18:47
#3
Re: Partition with Either on Traversable
I like it much better than this monstrosity I wrote, namely:
... so much so that I'm going to write my own without looking at your code. ;)
-0xe1a
/**
* 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
Fri, 2010-08-27, 18:57
#4
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:
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
Fri, 2010-08-27, 19:27
#5
Re: Partition with Either on Traversable
On Fri, Aug 27, 2010 at 10:47 AM, Josh Suereth <joshua.suereth@gmail.com> wrote:
Oh, it's just for the challenge/fun, not because of any copyright fear. :)
-0xe1a
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
Sun, 2010-08-29, 18:17
#6
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:
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
On Thu, Aug 26, 2010 at 10:39 PM, Josh Suereth <joshua.suereth@gmail.com> wrote: