Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zsolt-donca/35e6c5b842a1cecd0a6f72a9e50427bc to your computer and use it in GitHub Desktop.
Save zsolt-donca/35e6c5b842a1cecd0a6f72a9e50427bc to your computer and use it in GitHub Desktop.
Part 1: Followup of the FP meetup "Abstractions of higher kind" - using ValidatedNel
import cats._
import cats.data._
import cats.implicits._
case class User(age: Int)
def complicatedFunction[F[_]: Traverse, G[_]: Applicative, A: Monoid](ids: F[Int], fetchUser: Int => G[User], consume: User => A): G[A] = {
ids.traverse(fetchUser)
.map(_.foldMap(consume))
}
def fetchUserFromMem(i: Int): ValidatedNel[String, User] = {
i match {
case 1 => User(25).validNel
case 2 => User(25).validNel
case 3 => User(33).validNel
case _ => s"User with id $i not found".invalidNel
}
}
val validIds = List(1, 2, 3)
val invalidIds = List(21, 22, 1, 2, 3)
complicatedFunction(validIds, fetchUserFromMem, u => Map(u.age -> 1))
// Valid(Map(25 -> 2, 33 -> 1))
complicatedFunction(invalidIds, fetchUserFromMem, u => Map(u.age -> 1))
// Invalid(NonEmptyList(User with id 21 not found, User with id 22 not found))
@zsolt-donca
Copy link
Author

The problem is how can we have all the errors and still produce results whenever possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment