scalaz

How to understand traverse, traverseU and traverseM

白昼怎懂夜的黑 提交于 2019-12-02 14:14:53
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 sequence is used to gather together applicative effects . More concretely, it lets you "flip" F[G[A]] to G[F[A]] ,

How to fix this exercise with Endomorphic wrapper?

走远了吗. 提交于 2019-12-01 23:56:56
This is a follow-up to my previous question . Suppose I need to find an XML node by path. I can write a function to get a child node by name import scala.xml.{Node => XmlNode} def child(name: String): XmlNode = Option[XmlNode] = _.child.find(_.label == name) I compose the child functions with kleisli; e.g. scala> val a = <a><a0><a1><a2/></a1></a0></a> a: scala.xml.Elem = <a><a0><a1><a2/></a1></a0></a> scala> val find01 = Kleisli(child("a0")) >=> Kleisli(child("a1")) findAB: scalaz.Kleisli[Option,scala.xml.Node,scala.xml.Node] = Kleisli(<function1>) scala> find01(a) res85: Option[scala.xml.Node

Combining List, Future and Option in for-comprehension - scalaz

旧城冷巷雨未停 提交于 2019-12-01 20:51:20
I've got following problem: val sth: Future[Seq[T, S]] = for { x <- whatever: Future[List[T]] y <- x: List[T] z <- f(y): Future[Option[S]] n <- z: Option[S] } yield y: T -> n: S I would want to make this code to work(I guess everyone understands the idea as I've added types). By "to work" I mean, that I would want to stay with the for-comprehension structure and fulfil expected types in the end. I know there are "ugly" ways to do it, but I want to learn how to do it pure :) As I read the internet I've reached the conclusion that my problem may be solved by the monad transformers & scalaz.

Can I automatically implement classes?

混江龙づ霸主 提交于 2019-12-01 15:51:05
In Scalaz every Monad instance is automatically an instance of Applicative . implicit val listInstance = new Monad[List] { def point[A](a: => A) = List(a) def bind[A, B](fa: List[A])(f: A => List[B]) = fa flatMap f } List(2) <*> List((x: Int) => x + 1) // Works! Another example: Arrow is automatically a Profunctor . However, in Haskell I must provide an instance of Applicative for every Monad again and again. Is it possible to avoid this repetitive job? It isn't currently possible, though it would be if you changed the existing library to support this. Turning DefaultSignatures on would let

stacking StateT in scalaz

心已入冬 提交于 2019-12-01 15:25:31
I'm trying to understand Monad Transformers in Scala by porting some examples from this tutorial by Dan Piponi: http://blog.sigfpe.com/2006/05/grok-haskell-monad-transformers.html I did a couple of easy ones: import Control.Monad.State import Control.Monad.Identity test1 = do a <- get modify (+1) b <- get return (a,b) test2 = do a <- get modify (++"1") b <- get return (a,b) go1 = evalState test1 0 go2 = evalState test2 "0" becomes: import scalaz._, Scalaz._ val test1 = for { a <- get[Int] _ <- modify[Int](1+) b <- get } yield (a,b) val test2 = for { a <- get[String] _ <- modify[String](_ + "1"

Scalaz Functor typeclass special symbols

落花浮王杯 提交于 2019-12-01 11:13:04
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 new Functor[A, ({ type Alias[A] = Tuple2[X, A]})#Alias] Also, Intellij Idea (14.0.3) cannot resolve the

Calling generic function with Functor using subclass (cats/scalaz)

跟風遠走 提交于 2019-12-01 06:55:57
I've been messing around with some basic examples of Cats/Scalaz and also walking through tutorials to get a feel and I've hit a case which I'm sure there is a solution to. Is it possible to call a generalized function that takes a contextualized value ( F[A] ) with a Functor view ( F[_] : Functor ) with a context that is <: F ? I'm aware that Functor is invariant on type F[_] , and I'm also aware of the existence of Functor.widen , but it seems strange that there is no way to implicitly widen my type for use in a general function. An example in Cats (a similar example with Scalaz exists as

Which Monad Transformer to use?

[亡魂溺海] 提交于 2019-12-01 06:40:44
I am trying to write the validate function below so that the validation stops after the first error encountered. The return type of three is different to the other functions. Which monad transformer do I use in order to make this code compile? import scalaz._ import Scalaz._ import scala.concurrent.Future import scala.concurrent.ExecutionContext.Implicits.global def one(a : String): Disjunction[Int, String] = a == "one" match { case true => \/-("one") case false => -\/(2) } def two(a : String): Disjunction[Int, String] = a == "two" match { case true => \/-("two") case false => -\/(3) } def

Calling generic function with Functor using subclass (cats/scalaz)

北战南征 提交于 2019-12-01 05:24:42
问题 I've been messing around with some basic examples of Cats/Scalaz and also walking through tutorials to get a feel and I've hit a case which I'm sure there is a solution to. Is it possible to call a generalized function that takes a contextualized value ( F[A] ) with a Functor view ( F[_] : Functor ) with a context that is <: F ? I'm aware that Functor is invariant on type F[_] , and I'm also aware of the existence of Functor.widen , but it seems strange that there is no way to implicitly

Which Monad Transformer to use?

こ雲淡風輕ζ 提交于 2019-12-01 05:05:37
问题 I am trying to write the validate function below so that the validation stops after the first error encountered. The return type of three is different to the other functions. Which monad transformer do I use in order to make this code compile? import scalaz._ import Scalaz._ import scala.concurrent.Future import scala.concurrent.ExecutionContext.Implicits.global def one(a : String): Disjunction[Int, String] = a == "one" match { case true => \/-("one") case false => -\/(2) } def two(a : String