scalaz

How do you use scalaz.WriterT for logging in a for expression?

匆匆过客 提交于 2019-12-03 04:53:59
问题 How do you use scalaz.WriterT for logging? 回答1: About monad transformers This is a very short introduction. You may find more information on haskellwiki or this great slide by @jrwest. Monads don't compose, meaning that if you have a monad A[_] and a monad B[_] , then A[B[_]] can not be derived automatically . However in most cases this can be achieved by having a so-called monad transformer for a given monad. If we have monad transformer BT for monad B , then we can compose a new monad A[B[_

Async computation with Validation in Scala using Scalaz

怎甘沉沦 提交于 2019-12-03 03:21:40
Being writing a completely async library to access a remote service (using Play2.0), I'm using Promise and Validation to create non-blocking call, which has a type presenting fail and valid result at once. Promise comes from Play2-scala, where Validation comes from scalaz. So here is the type of examples of such functions f :: A => Promise[Validation[E, B]] g :: B => Promise[Validation[E, C]] So far, so good, now if I want to compose them, I can simple use the fact that Promise present a flatMap , so I can do it with a for-comprehension for ( x <- f(a); y <- g(b) ) yield y Ok, I took a

Is it just a coincidence that Kleisli, ReaderT, and Reader are the same in Scalaz

我只是一个虾纸丫 提交于 2019-12-03 01:54:02
In Scalaz Kleisli[F, A, B] is a wrapper for A => F[B] . ReaderT[F, A, B] -- reader monad transformer -- is just an alias of Kleisli[F, A, B] . Reader[A, B] monad is a specialization of ReaderT with identity monad Id : type Reader[A, B] = ReaderT[Id, A, B] . Is it just a coincidence or there are some deeper reasons that Kleisli , ReaderT , and Reader are isomorphic in Scalaz ? You can think of it as arriving at the same place by two different routes. On one side you start with the reader monad, which is simply a kind of wrapper for functions. Then you realize that you want to integrate this

a clean way to combine two tuples into a new larger tuple in scala?

喜欢而已 提交于 2019-12-02 23:05:52
Let's say I have the following tuples: scala> val t1 = Tuple2("abcd", "efg") t1: (java.lang.String, java.lang.String) = (abcd,efg) scala> val t2 = Tuple2(1234, "lmnop") t2: (Int, java.lang.String) = (1234,lmnop) scala> val t3 = Tuple3("qrs", "tuv", "wxyz") t3: (java.lang.String, java.lang.String, java.lang.String) = (qrs,tuv,wxyz) Is there a friendly way to combine them (in two steps if necessary) into a Tuple7? I'm really looking for a general answer for combining tuples of arbitrary size, and realize that there will be limitations due to the capped maximum tuple size. I am specifically

Folding a list of different types using Shapeless in Scala

柔情痞子 提交于 2019-12-02 21:12:44
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[({type cp[α] = Coproduct[F,G,α]})#cp,H] { def apply[A](fa: Coproduct[F,G,A]): H[A] = fa.run match { case

Avoiding memory leaks with Scalaz 7 zipWithIndex/group enumeratees

a 夏天 提交于 2019-12-02 21:00:11
Background As noted in this question , I'm using Scalaz 7 iteratees to process a large (i.e., unbounded) stream of data in constant heap space. My code looks like this: type ErrorOrT[M[+_], A] = EitherT[M, Throwable, A] type ErrorOr[A] = ErrorOrT[IO, A] def processChunk(c: Chunk, idx: Long): Result def process(data: EnumeratorT[Chunk, ErrorOr]): IterateeT[Vector[(Chunk, Long)], ErrorOr, Vector[Result]] = Iteratee.fold[Vector[(Chunk, Long)], ErrorOr, Vector[Result]](Nil) { (rs, vs) => rs ++ vs map { case (c, i) => processChunk(c, i) } } &= (data.zipWithIndex mapE Iteratee.group(P)) The Problem

Error handling monads in Scala? Try vs Validation

旧街凉风 提交于 2019-12-02 20:33:10
scalaz.Validation is said to be more powerful than the Try monad, because it can accumulate errors. Are there any occasions where you might choose Try over scalaz.Validation or scalaz.\/ ? Travis Brown The most significant argument in favor of Try is that it's in the standard library. It's also used in the standard library—for example the callbacks you register with Future 's onComplete must be functions from Try . It may be used more extensively in the standard library in the future. The fact that it's in the standard library also means it'll look familiar to more people. You'll probably tend

Scala functional programming gymnastics

≯℡__Kan透↙ 提交于 2019-12-02 18:32:46
I am trying to do the following in as little code as possible and as functionally as possible: def restrict(floor : Option[Double], cap : Option[Double], amt : Double) : Double Obviously the following works: = (floor -> cap) match { case (None, None) => amt case (Some(f), None) => f max amt case (None, Some(c)) => c min amt case (Some(f), Some(c)) => (f max amt) min c } I was really hoping for something more elegant and will accept use of the Scalaz library! You can assume that the following is true : floor.forall( f => cap.forall( _ > f)) If anyone is interested, here is some test code :

How do you use scalaz.WriterT for logging in a for expression?

五迷三道 提交于 2019-12-02 18:09:55
How do you use scalaz.WriterT for logging? About monad transformers This is a very short introduction. You may find more information on haskellwiki or this great slide by @jrwest . Monads don't compose, meaning that if you have a monad A[_] and a monad B[_] , then A[B[_]] can not be derived automatically . However in most cases this can be achieved by having a so-called monad transformer for a given monad. If we have monad transformer BT for monad B , then we can compose a new monad A[B[_]] for any monad A . That's right, by using BT , we can put the B inside A . Monad transformer usage in

In Scala, is there a shorthand for reducing a generic type's arity?

拈花ヽ惹草 提交于 2019-12-02 17:43:58
I want to call Scalaz's pure method to put a value into the State monad. The following works: type IntState[A] = State[Int, A] val a = "a".pure[IntState] a(1) (Int, java.lang.String) = (1,a) I can also eliminate the type alias (thanks Scalaz's Pure.scala): val a = "a".pure[({type T[A]=State[Int,A]})#T] a(1) (Int, java.lang.String) = (1,a) But that is extremely clunky. Is there a shorter way to synthesize a type like this? Like placeholder syntax for function literals, is there something like: "a".pure[State[Int, *]] kmizu For concise partial type application (arity-2) in Scala, you can infix