How to split F[A \/ B] into (F[A], F[B])
Asked Answered
I

3

8

I occasionally hit code like this:

val things : List[A \/ B] = ???
val (as, bs) : (List[A], List[B]) = ??? //insert something to do this

or in my current case I want Map[A, B \/ C] => (Map[A, B], Map[A, C])

Is there a nice way to do this in the general case F[A \/ B] with appropriate restrictions on F? It looks vaguely like a variation on the theme of Unzip.

Isometrics answered 29/5, 2013 at 14:24 Comment(0)
D
9

Here's how we deal with this for / but also Either and Validation, and not just for Lists, but other Foldable.

object Uncozip {
  implicit val wtf = language.higherKinds

  // Typeclass which covers sum types such as \/, Either, Validation
  trait Sum2[F[_, _]] {
    def cata[A, B, X](a: A ⇒ X, b: B ⇒ X)(fab: F[A, B]): X
  }

  implicit val sumEither: Sum2[Either] = new Sum2[Either] {
    def cata[A, B, X](a: A ⇒ X, b: B ⇒ X)(fab: Either[A, B]): X = {
      fab match {
        case Left(l)  ⇒ a(l)
        case Right(r) ⇒ b(r)
      }
    }
  }

  implicit val sumEitherz: Sum2[\/] = new Sum2[\/] {
    def cata[A, B, X](a: A ⇒ X, b: B ⇒ X)(fab: A \/ B): X = {
      fab.fold(a(_), b(_))
    }
  }

  implicit val sumValidation: Sum2[Validation] = new Sum2[Validation] {
    def cata[A, B, X](a: A ⇒ X, b: B ⇒ X)(fab: A Validation B): X = {
      fab.fold(a(_), b(_))
    }
  }

  abstract class Uncozips[F[_], G[_, _], A, B](fab: F[G[A, B]]) {
    def uncozip: (F[A], F[B])
  }

  implicit def uncozip[F[_]: Foldable, G[_, _], A, B](fab: F[G[A, B]])(implicit g: Sum2[G], mfa: ApplicativePlus[F], mfb: ApplicativePlus[F]): Uncozips[F, G, A, B] = new Uncozips[F, G, A, B](fab) {
    def uncozip = {
      implicitly[Foldable[F]].foldRight[G[A, B], (F[A], F[B])](fab, (mfa.empty, mfb.empty)) { (l, r) ⇒
        g.cata[A, B, (F[A], F[B])]({ (a: A) ⇒ (mfa.plus(mfa.point(a), r._1), r._2) },
          { (b: B) ⇒ (r._1, mfa.plus(mfa.point(b), r._2)) })(l)
      }
    }
  }
}
Dkl answered 29/5, 2013 at 14:54 Comment(1)
Doesn't conveniently cover the Map case, because we end up viewing the Map as an Iterable[(A, B \/ C)] and then splitting that into A and B \/ C, but still very useful.Isometrics
D
2

You can map things in to a list of (Option[A], Option[B]), unzip that list in to two lists, and then unite the resulting lists:

import scalaz._
import Scalaz._

val things: List[String \/ Int] = List("foo".left, 42.right)
val (strs, ints): (List[String], List[Int]) = things.
  map { d => (d.swap.toOption, d.toOption) }. // List[(Option[String], Option[Int])]
  unzip.                                      // (List[Option[String]], List[Option[Int]])
  bimap(_.unite, _.unite)                     // (List[String], List[Int])

This isn't particularly efficient due to traversing the list three times.

Diecious answered 29/5, 2013 at 14:44 Comment(0)
H
1

Here is one way (for lists):

val things : List[A \/ B] = ???
val (as, bs) = (things.map(_.swap.toList).join, things.map(_.toList).join)

And for a map:

val things: Map[String, String \/ Int] = ???
val (as, bs) = (things.mapValues(_.swap.toList).filterNot(e => e._2.isEmpty), 
                things.mapValues(_.toList).filterNot(e => e._2.isEmpty))

I'm having a hard time coming up with a way to generalize this over any F (I believe you would need instances of Monoid and Applicative for F).

Helfant answered 29/5, 2013 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.