scalaz

Scala: SeqT monad transformer?

廉价感情. 提交于 2021-02-08 04:48:09
问题 If we have such two functions... def findUserById(id: Long): Future[Option[User]] = ??? def findAddressByUser(user: User): Future[Option[Address]] = ??? ...then we are able to use cats OptionT monad transformer to write for-comprehension with them easily: for { user <- OptionT(findUserById(id)) address <- OptionT(findAddressByUser(user)) } ... I'd like to compose future of sequences this way, like this: def findUsersBySomeField(value: FieldValue): Future[Seq[User]] = ??? def

Scala: SeqT monad transformer?

佐手、 提交于 2021-02-08 04:47:34
问题 If we have such two functions... def findUserById(id: Long): Future[Option[User]] = ??? def findAddressByUser(user: User): Future[Option[Address]] = ??? ...then we are able to use cats OptionT monad transformer to write for-comprehension with them easily: for { user <- OptionT(findUserById(id)) address <- OptionT(findAddressByUser(user)) } ... I'd like to compose future of sequences this way, like this: def findUsersBySomeField(value: FieldValue): Future[Seq[User]] = ??? def

Replace scalaz ListT with semantically equivalent cats functionality

柔情痞子 提交于 2020-03-14 18:37:06
问题 cats does not provide ListT monad transformer so how could we rewrite the following snippet which uses scalaz ListT in a for-comprehension to a semantically equivalent snippet in cats import scalaz._ import ListT._ import scalaz.std.option._ val seeds: Option[List[String]] = Some(List("apple", "orange", "tomato")) def grow(seed: String): Option[List[String]] = Some(List(seed.toUpperCase)) def family(seed: String, plant: String): Option[List[(String, String)]] = Some(List(seed -> plant)) (for

How to turn a List of Eithers to a Either of Lists using scalaz.MonadPlus.separate

蹲街弑〆低调 提交于 2020-01-25 19:22:07
问题 How to turn a List of Eithers to a Either of Lists , using MonadPlus.separate ? In this answer the author claims this solution, but fails to provide the imports or complete example: If scalaz is one of your dependencies I would simply use separate: val el : List[Either[Int, String]] = List(Left(1), Right("Success"), Left(42)) scala> val (lefts, rights) = el.separate lefts: List[Int] = List(1, 42) rights: List[String] = List(Success) Is this a real working solution? I see that MonadPlus has a

Scalaz: how can I accumulate Failures or apply a function to Validations with different types?

寵の児 提交于 2020-01-21 03:19:13
问题 I have 19 strings that need to be validated into various types. When all validate successfully, I would like to instantiate a class that represents a row of a spreadsheet (where the columns do not all have the same type). When one or more of the strings fails to validate, I would like to have the errors accumulated in a NonEmptyList. If there were 12 or fewer items, I could use |@| or apply12. If I use a for expression, it fails fast and no accumulation happens. I could sequence the failures

How to parse JSON with lift-json in Scala?

这一生的挚爱 提交于 2020-01-15 15:46:15
问题 When I am trying parse the json object I am getting the below error. import net.liftweb.json._ object SarahEmailPluginConfigTest { implicit val formats = DefaultFormats case class Mailserver(url: String, username: String, password: String) val json = parse("""{"url": "imap.yahoo.com", "username": "myusername", "password": "mypassword" }""") def main(args: Array[String]) { val m = json.extract[Mailserver] println(m.url) println(m.username) println(m.password) } } I have added "lift-json_2.9.0

List[Try[T]] to Try[List[T]] in Scala

不打扰是莪最后的温柔 提交于 2020-01-14 13:59:07
问题 I would like to know how to convert a List[Try[T]] to Try[List[T]] in Scala? I have tried using an accumulator and folding right but it doesn't seem ideal. 回答1: Using cats it's as easy as: import cats._ import cats.data._ import cats.implicits._ import scala.util.{Try, Success, Failure} val tries: List[Try[Int]] = List(Success(1), Success(2), Success(3)) tries.sequence More information in the Traverse docs. 回答2: Try(list.map(_.get)) This will return only the first failure, so you need

Map and reduce/fold over HList of scalaz.Validation

丶灬走出姿态 提交于 2020-01-12 05:21:05
问题 I started out with something like this: def nonEmpty[A] = (msg: String) => (a: Option[A]) => a.toSuccess(msg) val postal: Option[String] = request.param("postal") val country: Option[String] = request.param("country") val params = (postal |> nonEmpty[String]("no postal" )).toValidationNel |@| (country |> nonEmpty[String]("no country")).toValidationNel params { (postal, country) => ... } Now I thought it would be nice to reduce the boilerplate for better readability and for not having to

Validation versus disjunction

亡梦爱人 提交于 2020-01-09 04:22:10
问题 Suppose I want to write a method with the following signature: def parse(input: List[(String, String)]): ValidationNel[Throwable, List[(Int, Int)]] For each pair of strings in the input, it needs to verify that both members can be parsed as integers and that the first is smaller than the second. It then needs to return the integers, accumulating any errors that turn up. First I'll define an error type: import scalaz._, Scalaz._ case class InvalidSizes(x: Int, y: Int) extends Exception( s

Problems using Nothing bottom type while trying to create generic zeros for parametrized monoids

两盒软妹~` 提交于 2020-01-06 14:15:13
问题 Here's my code. It permits to create typesafe MongoDB queries using Casbah trait TypesafeQuery[ObjectType, BuildType] { def build: BuildType } trait TypesafeMongoQuery[ObjectType] extends TypesafeQuery[ObjectType, DBObject] case class TypesafeMongoQueryConjunction[ObjectType](queries: Seq[TypesafeMongoQuery[ObjectType]]) extends TypesafeMongoQuery[ObjectType] { override def build(): DBObject = $and(queries.map(_.build)) } case class TypesafeMongoQueryDisjunction[ObjectType](queries: Seq