scalaz

Map on Scalaz Validation failure

青春壹個敷衍的年華 提交于 2019-12-06 17:05:59
问题 import scalaz._ import Scalaz._ "abc".parseInt This will return a Validation[NumberFormatException, Int] . Is there a way I can apply a function on the failure side (such as toString ) to get a Validation[String, Int] ? 回答1: There is a pair of methods <-: and :-> defined on MAB[M[_,_], A, B] that map on the left and right side of any M[A, B] as long as there is a Bifunctor[M] . Validation happens to be a bifunctor, so you can do this: ((_:NumberFormatException).toString) <-: "123".parseInt

Provide implicits for all subtypes of sealed type

自闭症网瘾萝莉.ら 提交于 2019-12-06 14:58:58
In my application I have multiple case classes and objects which are part of sealed trait hierarchy. I use them as messages in Akka. Those classes need to be converted to user friendly form before sending through websocket. Previously I used big pattern match to convert them in single place, but as number of types grows I would like to use implicit conversion: object Types { sealed trait Type case object SubType1 extends Type case object SubType2 extends Type case object SubType3 extends Type trait Converter[T] { def convert(t: T): Int } } object Implicits { import Types._ implicit object

Idiomatic Scala way of deserializing delimited strings into case classes

浪尽此生 提交于 2019-12-06 07:19:47
问题 Suppose I was dealing with a simple colon-delimited text protocol that looked something like: Event:005003:information:2013 12 06 12 37 55:n3.swmml20861:1:Full client swmml20861 registered [entry=280 PID=20864 queue=0x4ca9001b] RSET:m3node:AUTRS:1-1-24:A:0:LOADSHARE:INHIBITED:0 M3UA_IP_LINK:m3node:AUT001LKSET1:AUT001LK1:r OPC:m3node:1-10-2(P):A7:NAT0 .... I'd like to deserialize each line as an instance of a case class, but in a type-safe way. My first attempt uses type classes to define

How to transform disjunction of Future to Future of disjunction

╄→尐↘猪︶ㄣ 提交于 2019-12-06 06:30:29
问题 I have the result of a method: val res: Future[Int] Xor Future[String] = getResult(x) and would like to transform it and use it as Future[Int Xor String] I could not extrapolate my use case from the herding cats blog and am not sure whether a monad transformer would be the right tool here, perhaps rather some form of traverse ? Xor from cats stands in for any disjunction. Scalaz \/ or stdlib Either would be fine as well (though I would prefer a biased disjunction). Thank you 回答1: Just as

Validating a sequence of XML elements with Writer monad

眉间皱痕 提交于 2019-12-06 05:10:00
This is a follow-up to my old question . Suppose I need to validate an XML: <a><a1/><a2/><a3/></a> I need to make sure that the root a has children a1 , a2 , and a3 (in this order). I'd like to use a List (instead of scalaz.Validation ) and Writer monad to collect validation errors. So I define a function to validate a sequence of XML elements like this: type Validate = List[Elem] => Writer[List[String], List[Elem]] Now I can add a function to validate the label of an XML element: val label: String => Validate = ... If I redefine Validate as Kleisli I'll be able to compose the Validate

How to combine sequence of Kleisli

空扰寡人 提交于 2019-12-06 04:32:23
Given a method that return a Kleisli given a parameter def k[A, B, C](a: A) : Kleisli[Future, B, Option[C]] = ??? I want to write a combinator that deals with a sequence of this parameter def ks[A, B, C](as: Seq[A]) : Kleisli[Future, B, Seq[C]] = Kleisli[Future, B, Seq[C]] { ctx => Future.sequence(as.map(a => k(a).run(ctx))).map(_.flatten) } Is there a better way ? Travis Brown There is a better way: import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future import scalaz._, Scalaz._ def k[A, B, C](a: A): Kleisli[Future, B, Option[C]] = ??? def ks[A, B, C](as:

Help me understand this Scala code: scalaz IO Monad and implicits

痞子三分冷 提交于 2019-12-06 03:37:30
问题 This is a followup to this question. Here's the code I'm trying to understand (it's from http://apocalisp.wordpress.com/2010/10/17/scalaz-tutorial-enumeration-based-io-with-iteratees/): object io { sealed trait IO[A] { def unsafePerformIO: A } object IO { def apply[A](a: => A): IO[A] = new IO[A] { def unsafePerformIO = a } } implicit val IOMonad = new Monad[IO] { def pure[A](a: => A): IO[A] = IO(a) def bind[A,B](a: IO[A], f: A => IO[B]): IO[B] = IO { implicitly[Monad[Function0]].bind(() => a

Scalaz: how to compose a map lens with a value lens?

核能气质少年 提交于 2019-12-06 01:39:44
问题 There's an example of a Scalaz map lens here: Dan Burton calls it containsKey , and it's inspired by the Edward Kmett talk. There is also something called mapVPLens in Scalaz 7 which is useful for modifying values in a map. My question is: if I have a lens for modifying type V , and a lens for a Map[K,V] , how can I compose them? I've been searching for a while for a good simple example, but there's still a dearth of examples in Scalaz. I'm interested in both Scalaz 6 and Scalaz 7 solutions.

Summing a List of Options with Applicative Functors

邮差的信 提交于 2019-12-05 20:25:45
问题 I have a List[Option[Int]] and want to sum over it using applicative functors. From [1] I understand that it should be something like the following import scalaz._ import Scalaz._ List(1,2,3).map(some(_)).foldLeft(some(0))({ case (acc,value) => (acc <|*|> value){_+_} }) however I am simply not able to figure out the correct way to write this. I would be glad if somebody could help me with this. Thank you very much [1] How to combine Option values in Scala? Edit Thanks for all the great

Handling fail-fast failures when the return type is Option[Error]

风格不统一 提交于 2019-12-05 17:39:46
I have posted quite several questions about failure handling in Scala and I really thank you all for your answers. I understand my options when dealing with Either and Scalaz or a for comprehension, and I have another (last?) question: How to do a fail-fast sequence of operations when the operations are dealing with the outside non-functional world, like a DB? I mean I have a method like that: def insertItem(item: Item): Either[Error,Item] Thanks to Either and these answers, I know how to do it with Either: Chaining method calls with Either and Method parameters validation in Scala, with for