scalaz

Scalaz: how to compose a map lens with a value lens?

本小妞迷上赌 提交于 2019-12-04 06:21:17
There's an example of a Scalaz map lens here : Dan Burton calls it containsKey , and it's inspired by the Edward Kmett talk. There is also something called mapVPLens in Scalaz 7 which is useful for modifying values in a map. My question is: if I have a lens for modifying type V , and a lens for a Map[K,V] , how can I compose them? I've been searching for a while for a good simple example, but there's still a dearth of examples in Scalaz. I'm interested in both Scalaz 6 and Scalaz 7 solutions. If the lens you're trying to compose with the map lens is a partial lens, you can just use compose :

“private[syntax]” in Scala

自闭症网瘾萝莉.ら 提交于 2019-12-04 05:32:06
问题 What is this "private[syntax]" language feature? /** Wraps a value `self` and provides methods related to `Show` */ final class ShowOps[F] private[syntax](val self: F)(implicit val F: Show[F]) extends Ops[F] { //// final def show: Cord = F.show(self) final def shows: String = F.shows(self) final def print: Unit = Console.print(shows) final def println: Unit = Console.println(shows) //// } ^ Location: scalaz-series-7.3.x/core/src/main/scala/scalaz/syntax/ShowSyntax.scala 回答1: private[packageX]

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

青春壹個敷衍的年華 提交于 2019-12-04 04:21:38
问题 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

How to avoid stack overflow when using scalaz's free monad?

拜拜、爱过 提交于 2019-12-04 03:36:28
I had previously thought that part of the goal of the implementation was to avoid this very problem, so maybe I'm doing something obviously dumb? Here is some code: // Stack overflow import scalaz._ sealed trait Command[T] case class Wait(ms: Long) extends Command[Unit] case object Evaluator extends (Command ~> Id.Id) { override def apply[T](cmd: Command[T]) = cmd match { case Wait(t) => Thread.sleep(t) } } object Api { def sleep(ms: Long): Free.FreeC[Command, Unit] = Free.liftFC(Wait(ms)) } val sleep: Free.FreeC[Command, Unit] = Api.sleep(1).flatMap { _ => sleep } Free.runFC(sleep)(Evaluator)

Summing a List of Options with Applicative Functors

偶尔善良 提交于 2019-12-04 02:39:05
I have a List[Option[Int]] and want to sum over it using applicative functors. From [1] I understand that it should be something like the following import scalaz._ import Scalaz._ List(1,2,3).map(some(_)).foldLeft(some(0))({ case (acc,value) => (acc <|*|> value){_+_} }) however I am simply not able to figure out the correct way to write this. I would be glad if somebody could help me with this. Thank you very much [1] How to combine Option values in Scala? Edit Thanks for all the great answers. If there is any None in the list, I want it to return None. I am trying to replace Null/Exception

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

蓝咒 提交于 2019-12-03 16:15:16
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._ type Va[A] = Validation[String, A] // only works on pairs of Va[_] def validate[Ret, In1, In2](params:

Convert a Seq[String] to a case class in a typesafe way

情到浓时终转凉″ 提交于 2019-12-03 14:17:59
I have written a parser which transforms a String to a Seq[String] following some rules. This will be used in a library. I am trying to transform this Seq[String] to a case class. The case class would be provided by the user (so there is no way to guess what it will be). I have thought to shapeless library because it seems to implement the good features and it seems mature, but I have no idea to how to proceed. I have found this question with an interesting answer but I don't find how to transform it for my needs. Indeed, in the answer there is only one type to parse (String), and the library

Scalaz Kleisli usage benefits

最后都变了- 提交于 2019-12-03 14:05:47
In scalaz Kleisli[M[_], A, B] is a wrapper of A => M[B] , which allows composition of such functions. For instance, if M[_] is monad I can compose Kleisli[M, A, B] and Kleisli[M, B, C] with >=> to get Kleisli[M, A, C] . In a nutshell, Kleisli provides fancy andThens depending on M . Is it correct ? Are there other benefits of using Kleisli ? Travis Brown Here are two benefits as examples—I'm sure you could come up with others. First, it can be useful to abstract over different arrows, such as Kleisli[M, ?, ?] and ? => ? . For example, I can write a generic function that will apply an

Scala Option object inside another Option object

ε祈祈猫儿з 提交于 2019-12-03 14:02:01
I have a model, which has some Option fields, which contain another Option fields. For example: case class First(second: Option[Second], name: Option[String]) case class Second(third: Option[Third], title: Option[String]) case class Third(numberOfSmth: Option[Int]) I'm receiving this data from external JSON's and sometimes this data may contain null's, that was the reason of such model design. So the question is: what is the best way to get a deepest field? First.get.second.get.third.get.numberOfSmth.get Above method looks really ugly and it may cause exception if one of the objects will be

Scala - compose function n times

核能气质少年 提交于 2019-12-03 12:32:20
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) => y.andThen(x) } } but the types are all wrong. Is there a simple way to achieve this? Ideally