Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a method: <code>separate</code> <a href="https://github.com/scalaz/scalaz/blob/scalaz-seven/core/src/main/scala/scalaz/MonadPlus.scala#L24" rel="nofollow noreferrer">defined</a> in MonadPlus. This typeclass is a combination a Monad with PlusEmpty (generalized Monoid). So you need to define instance for <code>Seq</code>:</p> <p>1) MonadPlus[Seq]</p> <pre><code>implicit val seqmp = new MonadPlus[Seq] { def plus[A](a: Seq[A], b: =&gt; Seq[A]): Seq[A] = a ++ b def empty[A]: Seq[A] = Seq.empty[A] def point[A](a: =&gt; A): Seq[A] = Seq(a) def bind[A, B](fa: Seq[A])(f: (A) =&gt; Seq[B]): Seq[B] = fa.flatMap(f) } </code></pre> <p>Seq is already monadic, so <code>point</code> and <code>bind</code> are easy, <code>empty</code> and <code>plus</code> are monoid operations and <code>Seq</code> is a <a href="http://blog.higher-order.com/blog/2013/08/20/free-monads-and-free-monoids/" rel="nofollow noreferrer">free monoid</a></p> <p>2) Bifoldable[\/]</p> <pre><code>implicit val bife = new Bifoldable[\/] { def bifoldMap[A, B, M](fa: \/[A, B])(f: (A) =&gt; M)(g: (B) =&gt; M)(implicit F: Monoid[M]): M = fa match { case \/-(r) =&gt; g(r) case -\/(l) =&gt; f(l) } def bifoldRight[A, B, C](fa: \/[A, B], z: =&gt; C)(f: (A, =&gt; C) =&gt; C)(g: (B, =&gt; C) =&gt; C): C = fa match { case \/-(r) =&gt; g(r, z) case -\/(l) =&gt; f(l, z) } } </code></pre> <p>Also easy, standard folding, but for type constructors with two parameters.</p> <p>Now you can use separate:</p> <pre><code>val seq: Seq[String \/ Int] = List(\/-(10), -\/("wrong"), \/-(22), \/-(1), -\/("exception")) scala&gt; seq.separate res2: (Seq[String], Seq[Int]) = (List(wrong, number exception),List(10, 22, 1)) </code></pre> <p><strong>Update</strong></p> <p>Thanks to <a href="https://stackoverflow.com/users/605582/kenji-yoshida">Kenji Yoshida</a>, there <a href="https://github.com/scalaz/scalaz/blob/v7.1.0-M3/core/src/main/scala/scalaz/Either.scala#L373" rel="nofollow noreferrer">is</a> a Bitraverse[\/], so you need only MonadPlus.</p> <p>And a simple solution using <code>foldLeft</code>:</p> <pre><code>seq.foldLeft((Seq.empty[String], Seq.empty[Int])){ case ((as, ai), either) =&gt; either match { case \/-(r) =&gt; (as, ai :+ r) case -\/(l) =&gt; (as :+ l, ai) } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload