scalaz

Customising composition of Future, Either and Writer in Scalaz

丶灬走出姿态 提交于 2019-12-01 01:06:37
This is a follow up to my previous question: Sequencing both Scalaz WriterT and Either with for-yield The following code block is an example of sequencing Future , Either and Writer using the EitherT and WriterT monad transformers; the following question is about how to subtly change the behaviour of that stack of transformers. import scalaz._, Scalaz._ class Example[F[_], L] (val logFn: (String) => L)(implicit val f: Monad[F], l: Monoid[L]) { type T = Throwable type EF[α] = EitherT[F, T, α] type WEF[α] = WriterT[EF, L, α] private def unreliableInt (i: Int): T Either Int = new java.util.Random

Handling exceptions in an iteratee library without an error state

白昼怎懂夜的黑 提交于 2019-12-01 00:27:02
问题 I'm trying to write an enumerator for reading files line by line from a java.io.BufferedReader using Scalaz 7's iteratee library, which currently only provides an (extremely slow) enumerator for java.io.Reader . The problems I'm running into are related to the fact that all of the other iteratee libraries I've used (e.g. Play 2.0's and John Millikin's enumerator for Haskell) have had an error state as one of their Step type's constructors, and Scalaz 7 doesn't. My current implementation Here

Typeclasses and inheritance in scalaz

不想你离开。 提交于 2019-11-30 20:57:08
This is my second try to define the problem, I can't get my head around it. I want to be able to define an algebraic type and define a simple typeclass over it, let's say Show . In haskell I do: data Tree a = EmptyTree | Node a deriving (Show) Now, if I type EmptyTree - haskell can show it, so it belongs to Show . Now I am trying to do the same in scala: sealed abstract class Tree[+T] case object EmptyTree extends Tree[Nothing] case class Node[T](value: T) extends Tree[T] Then I define Show around it: implicit def show[T] = Show.showA[Tree[T]] I can do println((EmptyTree : Tree[Int]).show) .

Functor Instance for Type Constructor with Two Parameters in Scala

让人想犯罪 __ 提交于 2019-11-30 14:44:47
I have a class Foo with two parameters, and I am trying to write a Functor instance for Foo with the first parameter fixed, as follows: object Scratchpad { trait Functor[F[_]] { def fmap[A, B](f: A => B): F[A] => F[B] } case class Foo[X, Y](value: Y) implicit def fooInstances[X]: Functor[Foo[X, _]] = new Functor[Foo[X, _]] { def fmap[A, B](f: A => B): Foo[X, A] => Foo[X, B] = foo => Foo[X, B](f(foo.value)) } } But the above code fails to compile, generating the following error: Error:(9, 41) Scratchpad.Foo[X, _] takes no type parameters, expected: one implicit def fooInstances[X]: Functor[Foo

Scalaz: how can I accumulate Failures or apply a function to Validations with different types?

旧时模样 提交于 2019-11-30 13:54:44
I have 19 strings that need to be validated into various types. When all validate successfully, I would like to instantiate a class that represents a row of a spreadsheet (where the columns do not all have the same type). When one or more of the strings fails to validate, I would like to have the errors accumulated in a NonEmptyList. If there were 12 or fewer items, I could use |@| or apply12. If I use a for expression, it fails fast and no accumulation happens. I could sequence the failures when the for expression fails, but that means I'm looping twice. Is there a way to use scalaz to pull

Will using Scala in a more functional way (scalaz) incur a performance/maintainability penalty?

半城伤御伤魂 提交于 2019-11-30 12:51:51
问题 I'm currently working on a small project (< 10k loc) which is mainly pure but relies on mutable optimizations mainly based on iterators and some data-structure reuse for heavy-duty calculations. I'd like to learn a bit more functional programming and want to get more type safety, by e.g. wrapping mutable computations into state transformer monads and the like. For this purpose there exists the scalaz library. Question One When abstracting my computations on a larger scale by using all the

Scala fast text file read and upload to memory

本秂侑毒 提交于 2019-11-30 12:23:43
In Scala, for reading a text file and uploading it into an array, a common approach is scala.io.Source.fromFile("file.txt").getLines.toArray Especially for very large files, is there a faster approach perhaps by reading blocks of bytes into memory first and then splitting them by new line characters ? (See Read entire file in Scala for commonly used approaches.) Many Thanks. The performance problem has nothing to do with the way the data is read. It is already buffered. Nothing happens until you actually iterate through the lines: // measures time taken by enclosed code def timed[A](block: =>

Is is possible to improve type inference for partially applied types in Scala?

≯℡__Kan透↙ 提交于 2019-11-30 09:15:18
I'm trying to improve the type inference of the traverse_ function in the code below: import scala.language.higherKinds trait Applicative[AF[_]] { def ap[A, B](a: AF[A])(f: AF[A => B]): AF[B] def pure[A](a: A): AF[A] def fmap[A, B](a: AF[A])(f: A => B): AF[B] } def traverse_[AP[_]: Applicative, A](xs: Iterable[A])(f: A => AP[Unit]): AP[Unit] = { val ap = implicitly[Applicative[AP]] (xs :\ ap.pure(())) { (x, acc) => val apFunc = ap.fmap(f(x))(a => identity[Unit] _) ap.ap(acc)(apFunc) } } implicit def optionAp = new Applicative[Option] { def ap[A, B](a: Option[A])(f: Option[A => B]): Option[B] =

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

对着背影说爱祢 提交于 2019-11-30 08:57:48
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) res1: Int = 10 scala> 5.left[String].fold(2 +, "[" +) res2: Any = 7 scala> 5.fail[String].fold(2 +, "[

Basic Scalaz State question

青春壹個敷衍的年華 提交于 2019-11-30 08:46:42
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) scala> numberSA(res4) ! 1 res6: List[(String, Int)] = List((one,1), (two,1), (three,1)) I can get it