scalaz

ZipList with Scalaz

喜夏-厌秋 提交于 2020-01-04 19:54:03
问题 Suppose I have a list of numbers and list of functions: val xs: List[Int] = List(1, 2, 3) val fs: List[Int => Int] = List(f1, f2, f3) Now I would like to use an Applicative to apply f1 to 1 , f2 to 2 , etc. val ys: List[Int] = xs <*> fs // expect List(f1(1), f2(2), f3(3)) How can I do it with Scalaz ? 回答1: pure for zip lists repeats the value forever, so it's not possible to define a zippy applicative instance for Scala's List (or for anything like lists). Scalaz does provide a Zip tag for

Can I use \/ type of scalaz to accumulate failures?

醉酒当歌 提交于 2020-01-03 15:33:26
问题 I know that \/ (disjunction type) in scalaz can be used to "fail fast", i.e. return the first failure in a sequence of functions calls. What if I want to "fail slow", i.e. accumulate the failures ? I can use Validation of scalaz. Now I wonder if \/ (disjunction type) provides any built-in support for accumulating failures. 来源: https://stackoverflow.com/questions/22755577/can-i-use-type-of-scalaz-to-accumulate-failures

How to combine sequence of Kleisli

拜拜、爱过 提交于 2020-01-02 10:33:38
问题 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 ? 回答1: There is a better way: import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future import

Processing a list of Scalaz6 Validation

喜夏-厌秋 提交于 2020-01-02 02:00:11
问题 Is there an idiomatic way to handle a collection of Validation in Scalaz6? val results:Seq[Validation[A,B]] val exceptions = results.collect{case Failure(exception)=>exception} exceptions.foreach{logger.error("Error when starting up ccxy gottware",_)} val success = results.collect{case Success(data)=>data} success.foreach {data => data.push} if (exceptions.isEmpty) containers.foreach( _.start()) I could think of using a fold when looping on results, but what about the final test? 回答1: The

How to avoid stack overflow when using scalaz's free monad?

亡梦爱人 提交于 2020-01-01 09:13:40
问题 I had previously thought that part of the goal of the implementation was to avoid this very problem, so maybe I'm doing something obviously dumb? Here is some code: // Stack overflow import scalaz._ sealed trait Command[T] case class Wait(ms: Long) extends Command[Unit] case object Evaluator extends (Command ~> Id.Id) { override def apply[T](cmd: Command[T]) = cmd match { case Wait(t) => Thread.sleep(t) } } object Api { def sleep(ms: Long): Free.FreeC[Command, Unit] = Free.liftFC(Wait(ms)) }

Scala Option object inside another Option object

大城市里の小女人 提交于 2020-01-01 05:09:08
问题 I have a model, which has some Option fields, which contain another Option fields. For example: case class First(second: Option[Second], name: Option[String]) case class Second(third: Option[Third], title: Option[String]) case class Third(numberOfSmth: Option[Int]) I'm receiving this data from external JSON's and sometimes this data may contain null's, that was the reason of such model design. So the question is: what is the best way to get a deepest field? First.get.second.get.third.get

Cartesian product traverse in scalaz

馋奶兔 提交于 2020-01-01 03:38:24
问题 In Eric Torreborre's blogpost on the paper Essence of the Iterator Pattern, he describes how the cartesian product of a traverse is also a traverse. Can anyone show me an example of this using the scalaz library as I can't figure it out. Let's say the problem is that, for a List[Int] I want to provide both of: The Int sum of the elements in the list A List[String] the elements of which are created by appending the "Z" to the String representation of the Int s My understanding is that I can do

Example of State and Free monad in Scalaz

两盒软妹~` 提交于 2020-01-01 03:19:05
问题 Can somebody give an example how to use ScalaZ Free monad ? For example, if I have a simple State function and want to apply it 10,000 times, I'd get StackOverflowError: def setS(i: Int) :State[List[Int], Unit] = State { l => ( i::l, () ) } val state = (1 to 10000).foldLeft( put(Nil :List[Int]) ) { case (st, i) => st.flatMap(_ => setS(i)) } state(Nil) As I understand, Free monad can help avoid this. How would I re-write this piece of code using Free monad to not cause stack overflow ? 回答1: As

Folding a list of different types using Shapeless in Scala

爷,独闯天下 提交于 2019-12-31 10:51:37
问题 As I known, shapeless provides the HList ( Heterogenous list) type which can include multiple types. Is it possible to fold HList ? for example, // ref - Composable application architecture with reasonably priced monad // code - https://github.com/stew/reasonably-priced/blob/master/src/main/scala/reasonable/App.scala import scalaz.{Coproduct, Free, Id, NaturalTransformation} def or[F[_], G[_], H[_]](f: F ~> H, g: G ~> H): ({type cp[α] = Coproduct[F,G,α]})#cp ~> H = new NaturalTransformation[(

Folding a list of different types using Shapeless in Scala

自古美人都是妖i 提交于 2019-12-31 10:51:16
问题 As I known, shapeless provides the HList ( Heterogenous list) type which can include multiple types. Is it possible to fold HList ? for example, // ref - Composable application architecture with reasonably priced monad // code - https://github.com/stew/reasonably-priced/blob/master/src/main/scala/reasonable/App.scala import scalaz.{Coproduct, Free, Id, NaturalTransformation} def or[F[_], G[_], H[_]](f: F ~> H, g: G ~> H): ({type cp[α] = Coproduct[F,G,α]})#cp ~> H = new NaturalTransformation[(