我正在尝试使用scalaz iteratee包来处理恒定空间中的大型zip文件.我需要对zip文件中的每个文件执行一个长时间运行的进程.这些过程可以(并且应该)并行运行.
我创建了一个EnumeratorT将每个膨胀ZipEntry成一个File对象.签名如下:
def enumZipFile(f:File):EnumeratorT[IoExceptionOr[IO[File]], IO]
Run Code Online (Sandbox Code Playgroud)
我想附加一个IterateeT将在每个文件上执行长时间运行的进程.我基本上最终得到了类似的东西:
type IOE[A] = IoExceptionOr[A]
def action(f:File):IO[List[Promise[IOE[File]]]] = (
consume[Promise[IOE[File]], IO, List] %=
map[IOE[File], Promise[IOE[File]], IO](longRunningProcess) %=
map[IOE[IO[File]], IOE[File], IO](_.unsafePerformIO) &=
enumZipFile(f)
).run
def longRunningProcess:(iof:IOE[File]):Promise[IOE[File]] =
Promise { Thread.sleep(5000); iof }
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时:
action(new File("/really/big/file.zip")).unsafePerformIO.sequence.get
Run Code Online (Sandbox Code Playgroud)
我收到一条java.lang.OutOfMemoryError: Java heap space消息.这对我来说很有意义,因为它试图在所有这些IO和Promise对象的内存中建立一个庞大的列表.
几个问题:
longRunningProcess它的副作用.Enumerator方法是错误的方法吗?我几乎没有想法,所以任何事情都会有所帮助.
谢谢!
更新#1
这是堆栈跟踪:
[error] java.lang.OutOfMemoryError: Java heap space
[error] at scalaz.Free.flatMap(Free.scala:46)
[error] at scalaz.effect.IO$$anonfun$flatMap$1.apply(IO.scala:62) …Run Code Online (Sandbox Code Playgroud) 我对Scalaz分离有所了解.这些上的左侧类型可以是来自其他库的不同类型的错误案例类.例如,一个故障情况可能是由于HTTP超时,而另一个故障情况可能表示Play中的Json解析错误.
有没有办法使用某种形式的类来声明'这4个类/特征都是这种类型的错误'而不实际让它们从一个共同的特征继承?如果确实存在,则还需要能够在理解中推断出它.
在Scalaz 7.x中从a隐式转换scala.collection.Traversable[A]为scalaz.Foldable[A]定义的位置在哪里?标准导入
import scalaz._
import Scalaz._
Run Code Online (Sandbox Code Playgroud)
不包括它.
曾经有过implicit def TraversableFoldable[]...定义scalaz/Foldable.scala,但是
相关的含义曾经被定义scalaz/Foldable.scala,但已被移入
scalaz.std.list
scalaz.std.iterable
...
Run Code Online (Sandbox Code Playgroud)
scalaz.std.traversable但是不存在.也许这是偶然遗漏?
FWIW,scalaz.std.list包含在标准导入中,因此List[A]转换为Foldable[A]. scalaz.std.iterable不在标准导入中,必须显式导入.Iterable[A]要Foldable[A]隐式转换.
val vLInts = (1 to 10).toList.right[String]
for {
i <- ListT(vLints)
_ = println(i)
} yield i
//error: no type parameters for method apply:(underlying: M[List[A]])scalaz.ListT[M,A] in object ListT exist so that it can be applied to arguments (scalaz.\/[String,List[Int]])
Run Code Online (Sandbox Code Playgroud)
这里的问题是析取\/[A, B]有2个泛型,因此不是Monad.当我做一个类型别名
type Attempt[A] = \/[String, A]
Run Code Online (Sandbox Code Playgroud)
它成功了,因为我把左侧固定,我现在有Monad.如果最外面的类型是Disjunction,如何不使用类型别名,我怎样才能让我的Monad Transformer工作?
我有以下解析器来解析包含Float和RDD的算术表达式:
import scalaz._
import Scalaz._
def term2: Parser[List[\/[Float, RDD[(Int,Array[Float])]]]] = rep(factor2)
def factor2: Parser[\/[Float, RDD[(Int,Array[Float])]]] = pathxml | num
def pathxml: Parser[ RDD[(Int,Array[Float])]] = pathIdent ^^ { s => pathToRDD(s)} //pathToRDD is a function that gets the path in string and create an RDD from the file inside that path and pathIdent parse to see whether the input string is a path or not
def num: Parser[\/[Float, RDD[(Int,Array[Float])]]] = floatingPointNumber ^^ (n => n.left[RDD[(Int,Array[Float])]].toFloat)
Run Code Online (Sandbox Code Playgroud)
收到以下错误:
[error] type mismatch;
[error] found …Run Code Online (Sandbox Code Playgroud) 在Nick Partridge关于导出scalaz的演示文稿中,基于旧版本的scalaz,他使用函数引入了验证:
def even(x: Int): Validation[NonEmptyList[String], Int] =
if (x % 2 == 0) x.success else { s"not even: $x".wrapNel.failure }
Run Code Online (Sandbox Code Playgroud)
然后他结合这个使用
even(1) <|*|> even(2)
Run Code Online (Sandbox Code Playgroud)
应用测试并返回带有失败消息的验证.使用scalaz 7我得到了
scala> even(1) <|*|> even(2)
<console>:18: error: value <|*|> is not a member of scalaz.Validation[scalaz.NonEmptyList[String],Int]
even(1) <|*|> even(2)
^
Run Code Online (Sandbox Code Playgroud)
什么是scalaz 7相当于这个组合器?
在Web应用程序中,我有一个可以以各种不同方式失败的操作,或者最终成功.
在此上下文中,成功和失败由SimpleResult的子类表示(表示HTTP响应)
我使用scalaz /上的monadic操作来编码我的算法,例如:
val result = for {
user <- fetchUser \/> Forbidden("you must be connected to perform this action")
basket <- user.basket \/> NotFound("no basket !")
...
} yield Ok(someBasketView(user, basket))
Run Code Online (Sandbox Code Playgroud)
所以这最终成为一个SimpleResult \/ SimpleResult ,我必须写这个:
result fold (identity, identity)
Run Code Online (Sandbox Code Playgroud)
从析取中提取结果,我觉得相当难看.
是否有一些抽象可以捕获这种"明显可简化的结构"?或者也许是分离不是这个问题的正确抽象?
这按预期工作:
object Planexecutor extends App {
import scalaz.concurrent.Future
import scala.concurrent.duration._
val f = Future.apply(longComputation)
val result = f.run
println(result)
}
Run Code Online (Sandbox Code Playgroud)
这不是:
object Planexecutor extends App {
import scalaz.concurrent.Future
import scala.concurrent.duration._
val f = Future.apply(longComputation).timed(1.second)
val result = f.run
println(result)
}
Run Code Online (Sandbox Code Playgroud)
在第一种情况下,应用程序正常退出,而在第二种情况下,它不会退出.但是,两个版本都会正确打印出结果值.
这是一个错误还是我不理解的东西?
我正在尝试使用Scalaz EitherT和scala.concurrent.Future.当试图在for-comprehension中使用它时:
import scalaz._
import Scalaz._
val et1:EitherT[Future, String, Int] = EitherT(Future.successful(1.right))
val et2:EitherT[Future, String, String] = EitherT(Future.successful("done".right))
val r:EitherT[Future, String, String] = for {
a <- et1
b <- et2
} yield (s"$a $b")
Run Code Online (Sandbox Code Playgroud)
我得到以下缺少的Functor和Monad实例错误:
could not find implicit value for parameter F: scalaz.Functor[scala.concurrent.Future]
b <- et2
^
could not find implicit value for parameter F: scalaz.Monad[scala.concurrent.Future]
a <- et1
Run Code Online (Sandbox Code Playgroud)
scalaz是否定义了Functor和Monad for Future的实例?如果没有提供这些实例的其他库或我是否需要编写它们?
这个“私有[语法]”语言特性是什么?
/** 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)
////
}
Run Code Online (Sandbox Code Playgroud)
^ 位置:scalaz-series-7.3.x/core/src/main/scala/scalaz/syntax/ShowSyntax.scala