scalaz

How to use lift from ToFunctorOps

给你一囗甜甜゛ 提交于 2019-12-05 13:17:51
ToFunctorOps defines a lift method via the ToLiftV implicit, but I can't seem to make it find my functor instances: import scalaz.std.option._ import scalaz.syntax.functor._ import scalaz.syntax.id._ import scalaz.syntax.std.option._ def inc(x: Int) = x + 1 1.some |> (inc _).lift <console>:16: error: could not find implicit value for parameter F: scalaz.Functor[F] 1.some |> (inc _).lift The functor instance for option is visible but the compiler can't seem to find it. Any suggestions as to how I can fix this? Travis Brown I don't understand exactly why this isn't working (and I've just asked a

scalaz validation and list monad

雨燕双飞 提交于 2019-12-05 06:54:47
I am trying to come up with something similar to the following: val s: Validation[String, Int] = 1.success def s2(i: Int): Validation[String, Int] = i.success val result = for { i <- s j <- List(1, 2) k <- s2(j) } yield "fine"; The above code does not compile and I understand, syntactically it does not make sense. I am trying to execute a list of validations in a monadic way. How do I achieve that? If you have a list of validations of A , you can turn it into a validation of lists of A using sequence : List(1, 2).map(s2).sequence[({type l[a]=Validation[String, a]})#l, Int] (if I understand the

What does “ap” of \\/ in Scalaz do?

瘦欲@ 提交于 2019-12-05 05:35:53
I am looking at disjunction type of scalaz and I noticed method ap /** Apply a function in the environment of the right of this disjunction. */ def ap[AA >: A, C](f: => AA \/ (B => C)): (AA \/ C) = f flatMap (ff => map(ff(_))) I guess I understand what it does. Now I wonder when and why one should actually use it ? Are there any examples of using this ap function ? The disjunction you are looking for: import scalaz.{ \/, -\/ \/-, EitherT } import scalaz.syntax.ToIdOps object Testing extends ToIdOps // left and right methods come from there { // say you have the following method def someMethod

Processing a list of Scalaz6 Validation

孤者浪人 提交于 2019-12-05 03:01:07
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? Travis Brown The usual way to work with a list of validations is to use sequence to turn the list into a Validation

How to use >=> in Scala?

旧街凉风 提交于 2019-12-05 02:23:41
I am trying to use >=> (Kleisli arrow) in Scala. As I understand, it composes functions returning monads. Now I am trying it as follows: scala> val f = {i:Int => Some(i + 1)} f: Int => Some[Int] = <function1> scala> val g = {i:Int => Some(i.toString)} g: Int => Some[String] = <function1> scala> val h = f >=> g <console>:15: error: value >=> is not a member of Int => Some[Int] val h = f >=> g ^ Why does not it compile ? How to compose f and g with >=> ? There are two problems here. The first is that the inferred types of your functions are too specific. Option is a monad, but Some is not. In

Map on Scalaz Validation failure

元气小坏坏 提交于 2019-12-05 00:18:30
import scalaz._ import Scalaz._ "abc".parseInt This will return a Validation[NumberFormatException, Int] . Is there a way I can apply a function on the failure side (such as toString ) to get a Validation[String, Int] ? There is a pair of methods <-: and :-> defined on MAB[M[_,_], A, B] that map on the left and right side of any M[A, B] as long as there is a Bifunctor[M] . Validation happens to be a bifunctor, so you can do this: ((_:NumberFormatException).toString) <-: "123".parseInt Scala's type inference generally flows from left to right, so this is actually shorter: "123".parseInt.<-:(_

switch function and object with scalaz' |>

天涯浪子 提交于 2019-12-04 23:59:23
I can use scalaz |> operator when I want to switch function and object so there can be a little more readability acquired. Let me introduce you a model function : def length2(x:String) = x.length * 2 Now, I can write it in both ways: "aoeu" |> length2 length2("aoeu") But if I define this function more generic, it stops working. def length2(x:SeqLike[ _ , _ ]) = x.length * 2 length2("aoeu") // ok "aoeu" |> length2 // doesn't work Why the compiler doesn't understand this? There is definitely an implicit conversion from String to some class mixing in trait SeqLike . scala> "aoeu" |> length2

Pimping scalaz Memo

假如想象 提交于 2019-12-04 19:22:46
I like the new scalaz Memo functionality but find it lacks 2 things: 1) it hides the underlying Map, which I need access to--at least a List of all the values, and 2) I want a version that's implemented using a val scala.collection.concurrent.TrieMap, which I read somewhere is preferable than a var Map. I'm not yet an implicit wizard. Is there a way to pimp this Memo class to add versions that support this capability or am I going to have to cut/paste into a distinct, new class? This can be accomplished with the built-in Memo.memo function. Memo.memo creates a Memo instance from a function F =

What's the difference between Task and IO in Scalaz?

主宰稳场 提交于 2019-12-04 18:23:55
问题 These two Scalaz types scalaz.concurrent.Task[+A] scalaz.effect.IO[A] seem very conceptually similar. They both: Represent a potentially side-effecting computation Produce a success ( A ) or failure ( Exception ) result Have Monad instances Can be unsafely unwrapped with run or unsafePerformIO How do they differ? Why do they both exist? 回答1: Core difference is that IO simply delays the execution of something but does it within a current thread. Task on the other hand is capable of executing

Scala - compose function n times

只愿长相守 提交于 2019-12-04 18:07:01
问题 I have a function that looks like this: def emulate: (Cpu => Cpu) => (Cpu => Cpu) = render => { handleOpcode andThen handleTimers andThen handleInput andThen debug andThen render } I want to call the handleOpcode function n number of times (say 10 times). In Haskell I might write a function like so: ntimes n f = foldr (.) id (replicate n f) But in Scala, I'm not sure how I might write it. I tried: def nTimes(n: Int, f: => Any) = { val l = List.fill(n)(f) l.foldRight(identity[Function]){ (x, y