scalaz

Reader Monad for Dependency Injection: multiple dependencies, nested calls

时光毁灭记忆、已成空白 提交于 2019-11-29 18:43:37
When asked about Dependency Injection in Scala, quite a lot of answers point to the using the Reader Monad, either the one from Scalaz or just rolling your own. There are a number of very clear articles describing the basics of the approach (e.g. Runar's talk , Jason's blog ), but I didn't manage to find a more complete example, and I fail to see the advantages of that approach over e.g. a more traditional "manual" DI (see the guide I wrote ). Most probably I'm missing some important point, hence the question. Just as an example, let's imagine we have these classes: trait Datastore { def

What's the relation of fold on Option, Either etc and fold on Traversable?

会有一股神秘感。 提交于 2019-11-29 14:05:49
问题 Scalaz provides a method named fold for various ADTs such as Boolean , Option[_] , Validation[_, _] , Either[_, _] etc. This method basically takes functions corresponding to all possible cases for that given ADT. In other words, a pattern match shown below: x match { case Case1(a, b, c) => f(a, b, c) case Case2(a, b) => g(a, b) . . case CaseN => z } is equivalent to: x.fold(f, g, ..., z) Some examples: scala> (9 == 8).fold("foo", "bar") res0: java.lang.String = bar scala> 5.some.fold(2 *, 2)

Basic Scalaz State question

半腔热情 提交于 2019-11-29 13:10:18
问题 How do I use State to mimic the behaviour of List.zipWithIndex ? What I have come up with so far (which doesn't work) is: def numberSA[A](list : List[A]) : State[Int, List[(A, Int)]] = list match { case x :: xs => (init[Int] <* modify((_:Int) + 1)) map { s : Int => (x -> s) :: (numberSA(xs) ! s) } case Nil => state( (i : Int) => i -> nil[(A, Int)] ) } This is based very loosely on the state example. As I said, it does not work: scala> res4 res5: List[java.lang.String] = List(one, two, three)

Applicative vs. monadic combinators and the free monad in Scalaz

南笙酒味 提交于 2019-11-29 12:42:15
问题 A couple of weeks ago Dragisa Krsmanovic asked a question here about how to use the free monad in Scalaz 7 to avoid stack overflows in this situation (I've adapted his code a bit): import scalaz._, Scalaz._ def setS(i: Int): State[List[Int], Unit] = modify(i :: _) val s = (1 to 100000).foldLeft(state[List[Int], Unit](())) { case (st, i) => st.flatMap(_ => setS(i)) } s(Nil) I thought that just lifting a trampoline into StateT should work: import Free.Trampoline val s = (1 to 100000).foldLeft

How to use IO with Scalaz7 Iteratees without overflowing the stack?

倾然丶 夕夏残阳落幕 提交于 2019-11-29 12:34:08
问题 Consider this code (taken from here and modified to use bytes rather than lines of characters). import java.io.{ File, InputStream, BufferedInputStream, FileInputStream } import scalaz._, Scalaz._, effect._, iteratee.{ Iteratee => I, _ } import std.list._ object IterateeIOExample { type ErrorOr[+A] = EitherT[IO, Throwable, A] def openStream(f: File) = IO(new BufferedInputStream(new FileInputStream(f))) def readByte(s: InputStream) = IO(Some(s.read()).filter(_ != -1)) def closeStream(s:

scalaz List[StateT].sequence - could not find implicit value for parameter n: scalaz.Applicative

那年仲夏 提交于 2019-11-29 11:55:45
问题 I'm trying to figure out how to use StateT to combine two State state transformers based on a comment on my Scalaz state monad examples answer. It seems I'm very close but I got an issue when trying to apply sequence . import scalaz._ import Scalaz._ import java.util.Random val die = state[Random, Int](r => (r, r.nextInt(6) + 1)) val twoDice = for (d1 <- die; d2 <- die) yield (d1, d2) def freqSum(dice: (Int, Int)) = state[Map[Int,Int], Int]{ freq => val s = dice._1 + dice._2 val tuple = s ->

Managing imports in Scalaz7

放肆的年华 提交于 2019-11-29 06:12:44
I am using scalaz7 in a project and sometimes I run into issues with imports. The simplest way get started is import scalaz._ import Scalaz._ but sometimes this can lead to conflicts. What I have been doing until now the following slightly painful process: work out a minimal example that needs the same imports as my actual code copy that example in a separate project compile it with the option -Xprint:typer to find out how the code looks after implicit resolution import the needed implicits in the original project. Although this works, I would like to streamline it. I see that scalaz7 has much

Clojure's 'let' equivalent in Scala

孤街醉人 提交于 2019-11-29 05:41:59
问题 Often I face following situation: suppose I have these three functions def firstFn: Int = ... def secondFn(b: Int): Long = ... def thirdFn(x: Int, y: Long, z: Long): Long = ... and I also have calculate function. My first approach can look like this: def calculate(a: Long) = thirdFn(firstFn, secondFn(firstFn), secondFn(firstFn) + a) It looks beautiful and without any curly brackets - just one expression. But it's not optimal, so I end up with this code: def calculate(a: Long) = { val first =

Writing type class instances for nested classes in Scala

痞子三分冷 提交于 2019-11-29 04:08:16
In this recent Stack Overflow question , the author wanted to change a list of parsers of some type into a parser that returns lists of that type. We can imagine doing this with Scalaz's sequence for applicative functors: import scala.util.parsing.combinator._ import scalaz._ import Scalaz._ object parser extends RegexParsers { val parsers = List(1, 2, 3).map(repN(_, """\d+""".r)) def apply(s: String) = parseAll(parsers.sequence, s) } Here we take a list of three parsers that return lists of integers and turn it into a parser that returns lists of lists of integers. Unfortunately Scalaz doesn

Shapeless: generic lens parameterized by case class or field

可紊 提交于 2019-11-29 04:04:48
Based on: import shapeless._ case class Content(field: Int) lens[Content] >> 'field I am trying to make a lens-creating method, something along: def makeLens[T <: Product](s: Symbol) = lens[T] >> s But it seems non-obvious. Is it possible to do? If not, the end result I'm trying to achieve is a generic method for updating nested Maps with case-class contents, e.g.: import scalaz._ import Scalaz._ import PLens._ import shapeless._ import shapeless.contrib.scalaz._ def nestedMapLens[R, T <: Product](outerKey: String, innerKey: Int, f: Symbol) = ~((lens[T] >> f).asScalaz) compose mapVPLens