scala-cats

How to flatten a sequence of cats' ValidatedNel values

点点圈 提交于 2019-12-12 07:49:06
问题 I need to flatten a sequence of cats.data.ValidatedNel[E, T] values to a single ValidatedNel value: val results: Seq[cats.data.ValidatedNel[E, T]] = ??? val flattenedResult: cats.data.ValidatedNel[E, T] I can do it like this: import cats.std.list._, cats.syntax.cartesian._ results.reduce(_ |@| _ map { case _ => validatedValue }) but wonder if a pre-defined library methods exists. 回答1: It depends on how you want to combine them (what is validatedValue in your question ?) import cats.data.

Validating list of strings

与世无争的帅哥 提交于 2019-12-12 03:39:45
问题 This is a follow up to my previous question. Suppose I need to write a function validate in order to make sure that a given list of strings consists of "a", "b", and one or more "c". def validate(ss: List[String]): Either[NonEmptyList[MyError], Unit] = ??? Suppose I have three functions to check if a given string is "a", "b", or "c": def validateA(str: String): Either[MyError, Unit] = ??? def validateB(str: String): Either[MyError, Unit] = ??? def validateC(str: String): Either[MyError, Unit]

Scala monads - “value map is not a member of” error

南楼画角 提交于 2019-12-11 18:29:52
问题 I have created a simple trait and service: @finalAlg @autoFunctorK(true) trait BettingService[F[_]] { def put(bet: Bet): F[Bet] } class BettingServiceMock[F[_] : Async] extends BettingService[F] { override def put(bet: Bet): F[Bet] = { val id = randomUUID().toString for { created <- Bet(BetId(id), bet.stake, bet.name) } yield created } } Bet and BetId are case classes : case class Bet(betId: BetId, stake: BigDecimal, name: String) case class BetId(betId: String) extends AnyVal When I ran this

Kittens - ambiguous imports

人盡茶涼 提交于 2019-12-11 12:07:42
问题 I have taken the following code almost entirely from the 'derive show' example found here: https://github.com/milessabin/kittens import cats._, cats.derived._, cats.implicits._ object Test extends App { case class Address(street: String, city: String, state: String) case class ContactInfo(phoneNumber: String, address: Address) case class People(name: String, age: Double, contactInfo: ContactInfo) val mike = People("Mike", 1.23, ContactInfo("202-295-3928", Address("1 Main ST", "Chicago", "IL")

Why can find `Functor` instance for Tree but not for Branch or Leaf?

て烟熏妆下的殇ゞ 提交于 2019-12-11 07:11:36
问题 I have the following Functor definition: import cats.Functor import cats.syntax.functor._ object Theory { implicit val treeFunctor: Functor[Tree] = new Functor[Tree] { def map[A, B](fa: Tree[A])(f: A => B): Tree[B] = fa match { case Branch(left, right) => Branch(map(left)(f), map(right)(f)) case Leaf(value) => Leaf(f(value)) } } def main(args: Array[String]): Unit = { Branch(Leaf(10), Leaf(20)).map(_ * 2) } } for: sealed trait Tree[+A] final case class Branch[A](left: Tree[A], right: Tree[A])

How to make it a monad?

和自甴很熟 提交于 2019-12-11 05:39:29
问题 I am trying to validate a list of strings sequentially and define the validation result type like that: import cats._, cats.data._, cats.implicits._ case class ValidationError(msg: String) type ValidationResult[A] = Either[NonEmptyList[ValidationError], A] type ListValidationResult[A] = ValidationResult[List[A]] // not a monad :( I would like to make ListValidationResult a monad. Should I implement flatMap and pure manually or there is an easier way ? 回答1: I suggest you to take a totally

Filtering and mixing monads in Slick for comprehension and Cats

筅森魡賤 提交于 2019-12-11 02:02:05
问题 I have the following objective: Create a monad that adds an user with the following computation flow: Check if an user exists with a specified e-mail, if he doesn't then : Check if the credentials given are ok (password long enough, etc.). If they are ok, then: Save the user to the DB My first "draft" would be something like this: val work: DBIO[UserId] = for { userO <- UserRepository.findByEmail(createdUser.email) //userO is Option[User] //This won't work cause Action.withFilter doesnt exist

Deriving a Cats Order for Scala's Enumeration

那年仲夏 提交于 2019-12-10 23:46:06
问题 I would like a generic Cats Order for Scala's Enumeration . I tried implicit def enumOrder[E <: Enumeration, V <: E#Value]: cats.Order[V] = new cats.Order[V] { def compare(x: V, y: V): Int = x.compare(y) } but I get [error] overloaded method value compare with alternatives: [error] ((that: _1.Value)Int) forSome { val _1: E } <and> [error] (that: _1.Value)Int [error] cannot be applied to (V) [error] def compare(x: V, y: V): Int = x.compare(y) [error] ^ Does anybody know how I can implement

Scala: Combine Either per the whole List with Either per elements

匆匆过客 提交于 2019-12-10 18:47:32
问题 I have a list of Either, which represents error: type ErrorType = List[String] type FailFast[A] = Either[ErrorType, A] import cats.syntax.either._ val l = List(1.asRight[ErrorType], 5.asRight[ErrorType]) If all of them are right, I want to get a list of [A], in this case - List[Int] If any Either is left, I want to combine all errors of all either and return it. I've found a similar topic at [How to reduce a Seq[Either[A,B]] to a Either[A,Seq[B]] But it was quite long ago. For instance, one

What is Store in scalaz

萝らか妹 提交于 2019-12-10 14:13:22
问题 I'm trying to understand Lens es in scalaz (surprisingly didn't find something similar in cats-core ) and I came across the so called Store which is a type alias: type StoreT[F[_], A, B] = IndexedStoreT[F, A, A, B] type IndexedStore[I, A, B] = IndexedStoreT[Id, I, A, B] type Store[A, B] = StoreT[Id, A, B] Where final case class IndexedStoreT[F[_], +I, A, B](run: (F[A => B], I)) The question is how to treat this type? The documentation just referes to Lens es. Can someone give an explanation