Skip to content
This repository has been archived by the owner on Jan 25, 2018. It is now read-only.

Adds 'sequence' for Map[A, ErrorOr[B]] #43

Merged
merged 1 commit into from
Sep 11, 2017

Conversation

cjllanwarne
Copy link
Contributor

No description provided.

implicit class MapErrorOrRhs[A, B](val m: Map[A, ErrorOr[B]]) extends AnyVal {
private def elementToErrorOr(kvp: (A, ErrorOr[B])): ErrorOr[(A, B)] = kvp._2 map { v => (kvp._1, v) }

def sequence: ErrorOr[Map[A, B]] = m.toList.traverse[ErrorOr, (A, B)] { x => elementToErrorOr(x) } map { _.toMap }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ToL could you implement traverse and have sequence be a special case that applies identity to the elements?

@mcovarr
Copy link
Contributor

mcovarr commented Sep 1, 2017

👍

Approved with PullApprove

@cjllanwarne
Copy link
Contributor Author

@mcovarr is this what you intended?

@mcovarr
Copy link
Contributor

mcovarr commented Sep 1, 2017

Bravo

@danbills
Copy link
Contributor

danbills commented Sep 1, 2017

You should get sequence and traverse for free (w/ the right import sauce) w/ cats

@cjllanwarne
Copy link
Contributor Author

@danbills I wasn't able to get that to work, even with cats.implicits._, although that by no means rules it out as possible...

@danbills
Copy link
Contributor

danbills commented Sep 5, 2017

@cjllanwarne I wasn't either, but I was able to get it to work in Cats console... Investigating

cats:

scala> import cats.syntax.traverse._
import cats.syntax.traverse._

scala> import cats.instances.map._
import cats.instances.map._

scala> import cats.data.Validated._
import cats.data.Validated._

scala> validNel(1)
res0: cats.data.ValidatedNel[Nothing,Int] = Valid(1)

scala> Map("h" -> res0).sequence
res1: cats.data.ValidatedNel[Nothing,scala.collection.immutable.Map[String,Int]] = Valid(Map(h -> 1))

lenthall:

scala> import cats.syntax.traverse._
import cats.syntax.traverse._

scala> import cats.instances.map._
import cats.instances.map._

scala> import cats.data.Validated._
import cats.data.Validated._

scala> validNel(1)
res0: cats.data.ValidatedNel[Nothing,Int] = Valid(1)

scala> Map("h" -> res0).sequence
<console>:22: error: value sequence is not a member of scala.collection.immutable.Map[String,cats.data.ValidatedNel[Nothing,Int]]
       Map("h" -> res0).sequence

@cjllanwarne cjllanwarne merged commit 7e90b62 into develop Sep 11, 2017
@cjllanwarne cjllanwarne deleted the cjl_errorOrMapSequence branch September 11, 2017 20:51
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants