scalaz

What is Store in scalaz

萝らか妹 提交于 2019-12-10 14:13:22
问题 I'm trying to understand Lens es in scalaz (surprisingly didn't find something similar in cats-core ) and I came across the so called Store which is a type alias: type StoreT[F[_], A, B] = IndexedStoreT[F, A, A, B] type IndexedStore[I, A, B] = IndexedStoreT[Id, I, A, B] type Store[A, B] = StoreT[Id, A, B] Where final case class IndexedStoreT[F[_], +I, A, B](run: (F[A => B], I)) The question is how to treat this type? The documentation just referes to Lens es. Can someone give an explanation

Free implementation in scalaz

我是研究僧i 提交于 2019-12-10 13:05:09
问题 The Free implementation in Haskell is: data Free f a = Pure a | Free (f (Free f a)) whereas, the implementation in Scalaz is: sealed abstract class Free[S[_], A] private case class Return[S[_], A](a: A) extends Free[S, A] private case class Suspend[S[_], A](a: S[A]) extends Free[S, A] private case class Gosub[S[_], B, C](a: Free[S, C], f: C => Free[S, B]) extends Free[S, B] why isn't the scalaz implementation similar to Haskell, like: sealed trait Free[F[_],A] case class Return[F[_],A](a: A)

Scala: question marks in type parameters

╄→гoц情女王★ 提交于 2019-12-10 12:45:22
问题 I'm trying to understand the following piece of code (from the Scalaz library): def kleisliIdApplicative[R]: Applicative[Kleisli[Id, R, ?]] = ... I'm assuming that a type of the form T[P0, ?] is a type-constructor that takes a parameter. However I'm no able to find documentation that explains the usage of question marks in type parameters. A related question is what is the difference between the question mark and an underscore? Is there a place where all this is well-documented? 回答1: The

Validating a sequence of XML elements with Writer monad

不打扰是莪最后的温柔 提交于 2019-12-10 11:18:32
问题 This is a follow-up to my old question. Suppose I need to validate an XML: <a><a1/><a2/><a3/></a> I need to make sure that the root a has children a1 , a2 , and a3 (in this order). I'd like to use a List (instead of scalaz.Validation ) and Writer monad to collect validation errors. So I define a function to validate a sequence of XML elements like this: type Validate = List[Elem] => Writer[List[String], List[Elem]] Now I can add a function to validate the label of an XML element: val label:

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

送分小仙女□ 提交于 2019-12-10 04:08:23
问题 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 ? 回答1: The disjunction you are looking for: import scalaz.{ \/, -\/ \/-, EitherT } import scalaz.syntax.ToIdOps object Testing

Scalaz 7 Iteratee to process large zip file (OutOfMemoryError)

随声附和 提交于 2019-12-09 18:20:25
问题 I'm trying to use the scalaz iteratee package to process a large zip file in constant space. I have a long-running process I need to perform on each file in the zip file. Those processes can (and should) be run in parallel. I created an EnumeratorT that inflates each ZipEntry into a File object. The signature looks like: def enumZipFile(f:File):EnumeratorT[IoExceptionOr[IO[File]], IO] I want to attach an IterateeT that will perform the long-running process on each file. I basically end up

a clean way to combine two tuples into a new larger tuple in scala?

混江龙づ霸主 提交于 2019-12-09 04:20:21
问题 Let's say I have the following tuples: scala> val t1 = Tuple2("abcd", "efg") t1: (java.lang.String, java.lang.String) = (abcd,efg) scala> val t2 = Tuple2(1234, "lmnop") t2: (Int, java.lang.String) = (1234,lmnop) scala> val t3 = Tuple3("qrs", "tuv", "wxyz") t3: (java.lang.String, java.lang.String, java.lang.String) = (qrs,tuv,wxyz) Is there a friendly way to combine them (in two steps if necessary) into a Tuple7? I'm really looking for a general answer for combining tuples of arbitrary size,

Scala fast text file read and upload to memory

让人想犯罪 __ 提交于 2019-12-08 23:40:34
问题 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. 回答1: The performance problem has nothing to do with the way the data is read. It is already buffered. Nothing happens

Flatten types after composing two defs

久未见 提交于 2019-12-08 19:57:15
问题 following is a toy example to demonstrate real life legacy methods' shape weirdness and point of the question. As you can see anotherFunc , after mapping over personList expands type to \/[Throwable,List[\/[Throwable,String]]] which isn't intended return type but effect of map ing personList . again what's illustrated below inside anotherFunc is for demo purpose(in reality there is more meaningful things happening instead of Option("fakeString") or any of that. Requirement is to map

Scala infix type aliasing for >2 type parameters?

喜夏-厌秋 提交于 2019-12-08 16:23:38
问题 I know in Scala, you can do type ===>[A, B] = Map[A, B] and then you can use infix notation to define def foo: String ===> Int which is same as saying def foo: Map[String, Int] . Is there any way to exploit this infix notation to create types with >2 arguments? For example, I want something like this: type A ~> B ~~~> C to be an alias of say Map[A, Pair[B, C]] ? Is there anyway I can write something line this: type A to B -> C as alias for (A, B, C) type? 回答1: Interestingly operator