Last active
May 23, 2018 20:18
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem is how can we have all the errors and still produce results whenever possible?