scalaz7

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

Convert scala 2.10 future to scalaz.concurrent.Future // Task

一世执手 提交于 2019-12-31 10:42:33
问题 did anybody come to piece of code how to properly convert scala's Future (2.10) to new scalaz7 future ? I know hot to convert scalaz future via scala Promise to scala Future, but not sure how to do it properly around For example import scalaz.concurrent.{Future => Zuture} import scala.concurrent.Future I want to have implementation of implicit def scalaF2scalazF[A](in:Future[A]):Zuture[A]=??? Then obviously would be piece of cake to write implicit def scalaF2scalazTask[A](in:Future[A]):Task[A

Why awakeEvery was removed from scalaz-stream

一世执手 提交于 2019-12-24 12:44:05
问题 I found that there is no more awakeEvery inside scalaz.stream.Process in modern scalaz-stream. How to run something with period then? 回答1: It was moved to the scalaz.stream.time package: import scalaz.stream._ scala> implicit val sc = new java.util.concurrent.ScheduledThreadPoolExecutor(1) sc: java.util.concurrent.ScheduledThreadPoolExecutor = java.util.concurrent.ScheduledThreadPoolExecutor@6b9013a5[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0] scala>

Trampolining scalaz' Monad.whileM_ to prevent stack overflow

假装没事ソ 提交于 2019-12-24 11:25:58
问题 I'm using scalaz' Monad.whileM_ to implement a while loop in a functional way as follows: object Main { import scalaz._ import Scalaz._ import scala.language.higherKinds case class IState(s: Int) type IStateT[A] = StateT[Id, IState, A] type MTransT[S[_], A] = EitherT[S, String, A] type MTrans[A] = MTransT[IStateT, A] def eval(k: Int): MTrans[Int] = { for { state <- get[IState].liftM[MTransT] _ <- put(state.copy(s = (state.s + 1) % k)).liftM[MTransT] } yield (k + 1) } def evalCond(): MTrans

Generic transform/fold/map over tuple/hlist containing some F[_]

℡╲_俬逩灬. 提交于 2019-12-21 05:27:07
问题 I recently asked Map and reduce/fold over HList of scalaz.Validation and got a great answer as to how to transform a fixed sized tuple of Va[T] (which is an alias for scalaz.Validation[String, T] ) into a scalaz.ValidationNel[String, T] . I've since then been studying Shapeless and type level programming in general to try to come up with a solution that works on tuples of any size. This is what I'm starting out with: import scalaz._, Scalaz._, shapeless._, contrib.scalaz._, syntax.std.tuple._

Scalaz Functor typeclass special symbols

为君一笑 提交于 2019-12-19 10:29:20
问题 Recently I have come across this Scalaz code (e.g. https://github.com/scalaz/scalaz/blob/series/7.2.x/core/src/main/scala/scalaz/Functor.scala): def compose[G[_]](implicit G0: Functor[G]): Functor[λ[α => F[G[α]]]] = new CompositionFunctor[F, G] { implicit def F = self implicit def G = G0 } What is the meaning/purpose of the type expression inside the "Functor", i.e. λ[α => F[G[α]]]? Sofar, I have seen just type aliases like e.g. in http://like-a-boss.net/2014/09/27/type-lambda-in-scala.html

Managing imports in Scalaz7

吃可爱长大的小学妹 提交于 2019-12-18 04:39:07
问题 I am using scalaz7 in a project and sometimes I run into issues with imports. The simplest way get started is import scalaz._ import Scalaz._ but sometimes this can lead to conflicts. What I have been doing until now the following slightly painful process: work out a minimal example that needs the same imports as my actual code copy that example in a separate project compile it with the option -Xprint:typer to find out how the code looks after implicit resolution import the needed implicits

Merge maps in scalaz with a complex (double) operation

爱⌒轻易说出口 提交于 2019-12-13 18:43:27
问题 I am using a map to associate certain values with a tuple (Int, Double) where the int is the order they appeared and the double the number of times they show (it is not, but is clearer like this using int and double to distinguish) The tricky part is that I want to use different monoids for each element of the tuple, for the int I want to keep the min value, to remember first appearance, while for the double I want to use the addition monoid So for an existing key we have: val map1 = Map("a"

Best way to handle object's fields validation => Either / Try (scala 2.10) / ValidationNEL (scalaz)

[亡魂溺海] 提交于 2019-12-12 08:48:08
问题 Let's assume an object constructed using a builder pattern. This builder pattern would contain a build method focusing on fields validation and then on conversion to the targeted type. This validation could be implemented using: Either[FailureObject, TargetObject] type Try[TargetObject] (new feature from Scala 2.10) Validation[FailureObject, TargetObject] or ValidationNEL[FailureObject, TargetObject] from scalaz library I read that one of the main advantages of Validation over Either type is

Lifting a function which takes implicit parameter using functor (Scalaz7)

时光毁灭记忆、已成空白 提交于 2019-12-12 03:47:34
问题 Just started learning Scalaz. Here is my code trait Monoid[A] { def mappend(a1: A, a2: A): A def mzero: A } object Monoid { implicit val IntMonoid: Monoid[Int] = new Monoid[Int] { def mappend(a1: Int, a2: Int): Int = a1 + a2 def mzero: Int = 0 } implicit val StringMonoid: Monoid[String] = new Monoid[String] { def mappend(a1: String, a2: String): String = a1 + a2 def mzero: String = "" } } trait MonoidOp[A] { val F: Monoid[A] val value: A def |+|(a2: A): A = F.mappend(value, a2) } object