scalaz

Where is `sequence` in Scalaz7

北慕城南 提交于 2019-12-04 16:01:26
问题 I am learning Scalaz and I have a project that already makes use of Scalaz7. Following this question I would like to use the function sequence[T](l: List[Option[T]]): Option[List[T]] (not that it is hard to write it myself). But the aforementioned question mentions Scalaz6. Where to find the sequence function in Scalaz7? 回答1: It's defined in the scalaz.Traverse type class, where it looks like this: def sequence[G[_]:Applicative,A](fga: F[G[A]]): G[F[A]] = traversal[G].run[G[A], A](fga)(ga =>

How to transform disjunction of Future to Future of disjunction

a 夏天 提交于 2019-12-04 15:07:33
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 Just as sequence allows you to turn a F[G[A]] into a G[F[A]] when F has a Traverse instance and G is applicative,

Idiomatic Scala way of deserializing delimited strings into case classes

China☆狼群 提交于 2019-12-04 14:44:38
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 'read' methods for each possible type that I can encounter, in addition to the 'tupled' method on the case

Sequencing both Scalaz WriterT and Either with for-yield

纵饮孤独 提交于 2019-12-04 13:28:13
问题 If I have: import scala.concurrent._ import scalaz._, Scalaz._ type Z = List[String] type F[α] = Future[α] type WT[α] = WriterT[F, Z, α] implicit val z: Monoid[Z] = new Monoid[Z] { def zero = Nil def append (f1: Z, f2: => Z) = f1 ::: f2 } implicit val f: Monad[F] = scalaz.std.scalaFuture.futureInstance I can write code like this: def fooA (): WT[Int] = WriterT.put[F, Z, Int] (f.point (18))(z.zero) def fooB (): WT[Int] = WriterT.put[F, Z, Int] (f.point (42))(z.zero) def fooLog (msg: String):

Weird nested structural type in generics

一笑奈何 提交于 2019-12-04 12:12:24
Can someone explain weird construction of structural type nested in generics: implicit def Function1Functor[R]: Functor[({type λ[α]=(R) => α})#λ] = new Functor[({type λ[α]=(R) => α})#λ] .... This example comes from Scalaz library: Functor.scala Why this construction is needed there? Wouldn't be simpler to write: implicit def Function1Functor[R,A]: Functor[R =>A] or implicit def Function1Functor[R,A]: Functor[Function1[R,A]] The signature of the Functor type constructor shows that it is parameterised with another, unary, type constructor F : trait Functor[F[_]] extends InvariantFunctor[F]

Async computation with Validation in Scala using Scalaz

徘徊边缘 提交于 2019-12-04 09:32:35
问题 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

Avoiding memory leaks with Scalaz 7 zipWithIndex/group enumeratees

喜欢而已 提交于 2019-12-04 08:02:01
问题 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

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

我是研究僧i 提交于 2019-12-04 07:55:31
问题 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:

How to understand traverse, traverseU and traverseM

人走茶凉 提交于 2019-12-04 07:43:28
问题 I am confused about the usage case about traverse, traverseU and traverseM, I searched it in the scalaz website, the simple code example: def sum(x: Int) = x + 1 List(1,2,3).traverseU(sum) it looks like it is similar to (map and aggregate): List(1,2,3).map(sum).reduceLeft(_ + _) I think it is more than that for traverseU, I just wonder what is the difference between those 3 method, it would be better I will have some sample code to show the difference Many thanks in advance 回答1: sequence is

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

谁都会走 提交于 2019-12-04 07:39:10
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.unsafePerformIO, (x:A) => () => f(x).unsafePerformIO)() } } } This code is used like this (I'm