scalaz

What's the difference between a lens and a partial lens?

我是研究僧i 提交于 2019-12-03 12:04:32
问题 A "lens" and a "partial lens" seem rather similar in name and in concept. How do they differ? In what circumstances do I need to use one or the other? Tagging Scala and Haskell, but I'd welcome explanations related to any functional language that has a lens library. 回答1: To describe partial lenses—which I will henceforth call, according to the Haskell lens nomenclature, prisms (excepting that they're not! See the comment by Ørjan)—I'd like to begin by taking a different look at lenses

Iteratees in Scala that use lazy evaluation or fusion?

爱⌒轻易说出口 提交于 2019-12-03 10:35:38
I have heard that iteratees are lazy, but how lazy exactly are they? Alternatively, can iteratees be fused with a postprocessing function, so that an intermediate data structure does not have to be built? Can I in my iteratee for example build a 1 million item Stream[Option[String]] from a java.io.BufferedReader , and then subsequently filter out the None s, in a compositional way, without requiring the entire Stream to be held in memory? And at the same time guarantee that I don't blow the stack? Or something like that - it doesn't have to use a Stream . I'm currently using Scalaz 6 but if

Where is `sequence` in Scalaz7

余生颓废 提交于 2019-12-03 10:01:46
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? 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 => ga) scalaz.syntax.TraverseOps provides a version that gets pimped onto List , since List has a Traverse

Sequencing both Scalaz WriterT and Either with for-yield

混江龙づ霸主 提交于 2019-12-03 08:37:11
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): WT[Unit] = WriterT.put[F, Z, Unit] (f.point (()))(msg :: Nil)) def foo (): WT[Int] = for { _: Unit <-

Cartesian product traverse in scalaz

断了今生、忘了曾经 提交于 2019-12-03 08:20:43
In Eric Torreborre's blogpost on the paper Essence of the Iterator Pattern , he describes how the cartesian product of a traverse is also a traverse. Can anyone show me an example of this using the scalaz library as I can't figure it out. Let's say the problem is that, for a List[Int] I want to provide both of: The Int sum of the elements in the list A List[String] the elements of which are created by appending the "Z" to the String representation of the Int s My understanding is that I can do this using traverse but in such a way as to only actually traverse my structure once, unlike this

Example of State and Free monad in Scalaz

折月煮酒 提交于 2019-12-03 07:46:22
Can somebody give an example how to use ScalaZ Free monad ? For example, if I have a simple State function and want to apply it 10,000 times, I'd get StackOverflowError: def setS(i: Int) :State[List[Int], Unit] = State { l => ( i::l, () ) } val state = (1 to 10000).foldLeft( put(Nil :List[Int]) ) { case (st, i) => st.flatMap(_ => setS(i)) } state(Nil) As I understand, Free monad can help avoid this. How would I re-write this piece of code using Free monad to not cause stack overflow ? Travis Brown As I say in a comment above, lifting the State computations into StateT[Free.Trampoline, S, A]

Map and reduce/fold over HList of scalaz.Validation

独自空忆成欢 提交于 2019-12-03 07:30:40
I started out with something like this: def nonEmpty[A] = (msg: String) => (a: Option[A]) => a.toSuccess(msg) val postal: Option[String] = request.param("postal") val country: Option[String] = request.param("country") val params = (postal |> nonEmpty[String]("no postal" )).toValidationNel |@| (country |> nonEmpty[String]("no country")).toValidationNel params { (postal, country) => ... } Now I thought it would be nice to reduce the boilerplate for better readability and for not having to explain to more junior team members what .toValidateNel and |@| mean. The first thought was List but then

Error handling monads in Scala? Try vs Validation

半腔热情 提交于 2019-12-03 06:53:29
问题 scalaz.Validation is said to be more powerful than the Try monad, because it can accumulate errors. Are there any occasions where you might choose Try over scalaz.Validation or scalaz.\/ ? 回答1: The most significant argument in favor of Try is that it's in the standard library. It's also used in the standard library—for example the callbacks you register with Future 's onComplete must be functions from Try . It may be used more extensively in the standard library in the future. The fact that

Threading extra state through a parser in Scala

一世执手 提交于 2019-12-03 05:40:27
问题 I'll give you the tl;dr up front I'm trying to use the state monad transformer in Scalaz 7 to thread extra state through a parser, and I'm having trouble doing anything useful without writing a lot of t m a -> t m b versions of m a -> m b methods. An example parsing problem Suppose I have a string containing nested parentheses with digits inside them: val input = "((617)((0)(32)))" I also have a stream of fresh variable names (characters, in this case): val names = Stream('a' to 'z': _*) I

Scala functional programming gymnastics

蓝咒 提交于 2019-12-03 05:15:48
问题 I am trying to do the following in as little code as possible and as functionally as possible: def restrict(floor : Option[Double], cap : Option[Double], amt : Double) : Double Obviously the following works: = (floor -> cap) match { case (None, None) => amt case (Some(f), None) => f max amt case (None, Some(c)) => c min amt case (Some(f), Some(c)) => (f max amt) min c } I was really hoping for something more elegant and will accept use of the Scalaz library! You can assume that the following