8

我正在尝试编写一个枚举器,用于从java.io.BufferedReader使用Scalaz 7 的 iteratee 库中逐行读取文件,该库目前只为java.io.Reader.

我遇到的问题与我使用过的所有其他迭代库(例如Play 2.0John Millikin 的enumeratorStepHaskell)作为其类型的构造函数之一和 Scalaz 7都存在错误状态有关。没有。

我目前的实现

这是我目前拥有的。首先是一些导入和IO包装器:

import java.io.{ BufferedReader, File, FileReader }
import scalaz._, Scalaz._, effect.IO, iteratee.{ Iteratee => I, _ }

def openFile(f: File) = IO(new BufferedReader(new FileReader(f)))
def readLine(r: BufferedReader) = IO(Option(r.readLine))
def closeReader(r: BufferedReader) = IO(r.close())

还有一个类型别名来清理一下:

type ErrorOr[A] = Either[Throwable, A]

现在是一个tryIO助手,模仿(松散地,可能是错误地)在enumerator

def tryIO[A, B](action: IO[B]) = I.iterateeT[A, IO, ErrorOr[B]](
  action.catchLeft.map(
    r => I.sdone(r, r.fold(_ => I.eofInput, _ => I.emptyInput))
  )
)

自身的枚举器BufferedReader

def enumBuffered(r: => BufferedReader) = new EnumeratorT[ErrorOr[String], IO] {
  lazy val reader = r
  def apply[A] = (s: StepT[ErrorOr[String], IO, A]) => s.mapCont(k =>
    tryIO(readLine(reader)) flatMap {
      case Right(None)       => s.pointI
      case Right(Some(line)) => k(I.elInput(Right(line))) >>== apply[A]
      case Left(e)           => k(I.elInput(Left(e)))
    }
  )
}

最后是一个负责打开和关闭阅读器的枚举器:

def enumFile(f: File) = new EnumeratorT[ErrorOr[String], IO] {
  def apply[A] = (s: StepT[ErrorOr[String], IO, A]) => s.mapCont(k =>
    tryIO(openFile(f)) flatMap {
      case Right(reader) => I.iterateeT(
        enumBuffered(reader).apply(s).value.ensuring(closeReader(reader))
      )
      case Left(e) => k(I.elInput(Left(e)))
    }
  )
}

现在假设我想将文件中包含至少 25 个'0'字符的所有行收集到一个列表中。我可以写:

val action: IO[ErrorOr[List[String]]] = (
  I.consume[ErrorOr[String], IO, List] %=
  I.filter(_.fold(_ => true, _.count(_ == '0') >= 25)) &=
  enumFile(new File("big.txt"))
).run.map(_.sequence)

在许多方面,这似乎工作得很好:我可以开始这个动作unsafePerformIO,它会在几分钟内通过数千万行和千兆字节的数据,在恒定的内存中并且不会破坏堆栈,然后关闭阅读器完成后。如果我给它一个不存在的文件的名称,它会尽职尽责地将包含在 a 中的异常返回给我Left,并且enumBuffered如果它在读取时遇到异常,至少似乎表现得适当。

潜在问题

不过,我对我的实现有些担心——尤其是tryIO. 例如,假设我尝试编写一些迭代:

val it = for {
  _ <- tryIO[Unit, Unit](IO(println("a")))
  _ <- tryIO[Unit, Unit](IO(throw new Exception("!")))
  r <- tryIO[Unit, Unit](IO(println("b")))
} yield r

如果我运行它,我会得到以下信息:

scala> it.run.unsafePerformIO()
a
b
res11: ErrorOr[Unit] = Right(())

如果我在 GHCi 中尝试同样的事情enumerator,结果更像我所期望的:

...> run $ tryIO (putStrLn "a") >> tryIO (error "!") >> tryIO (putStrLn "b")
a
Left !

我只是看不到在 iteratee 库本身没有错误状态的情况下获得这种行为的方法。

我的问题

我并不声称自己是迭代器方面的专家,但我在一些项目中使用了各种 Haskell 实现,感觉我或多或少地理解了基本概念,并且曾经和 Oleg 喝过咖啡。不过,我在这里不知所措。这是在没有错误状态的情况下处理异常的合理方法吗?有没有一种方法可以实现tryIO更像enumerator版本?由于我的实现行为不同,是否有某种定时炸弹在等着我?

4

2 回答 2

6

在这里编辑是真正的解决方案。我离开了原来的帖子,因为我认为它值得看到这种模式。适用于 Klesli 的方法适用于 IterateeT

import java.io.{ BufferedReader, File, FileReader }
import scalaz._, Scalaz._, effect._, iteratee.{ Iteratee => I, _ }

object IterateeIOExample {
  type ErrorOr[+A] = EitherT[IO, Throwable, A]

  def openFile(f: File) = IO(new BufferedReader(new FileReader(f)))
  def readLine(r: BufferedReader) = IO(Option(r.readLine))
  def closeReader(r: BufferedReader) = IO(r.close())

  def tryIO[A, B](action: IO[B]) = I.iterateeT[A, ErrorOr, B] {
    EitherT.fromEither(action.catchLeft).map(r => I.sdone(r, I.emptyInput))
  }

  def enumBuffered(r: => BufferedReader) = new EnumeratorT[String, ErrorOr] {
    lazy val reader = r
    def apply[A] = (s: StepT[String, ErrorOr, A]) => s.mapCont(k =>
      tryIO(readLine(reader)) flatMap {
        case None => s.pointI
        case Some(line) => k(I.elInput(line)) >>== apply[A]
      })
  }

  def enumFile(f: File) = new EnumeratorT[String, ErrorOr] {
    def apply[A] = (s: StepT[String, ErrorOr, A]) => 
      tryIO(openFile(f)).flatMap(reader => I.iterateeT[String, ErrorOr, A](
        EitherT(
          enumBuffered(reader).apply(s).value.run.ensuring(closeReader(reader)))))
  }

  def main(args: Array[String]) {
    val action = (
      I.consume[String, ErrorOr, List] %=
      I.filter(a => a.count(_ == '0') >= 25) &=
      enumFile(new File(args(0)))).run.run

    println(action.unsafePerformIO().map(_.size))
  }
}

===== 原帖 =====

我觉得你需要一个 EitherT。如果没有 EitherT,您最终只会得到 3 个左或右。使用 EitherT 它将对左进行适当的处​​理。

我想你真正想要的是

type ErrorOr[+A] = EitherT[IO, Throwable, A] 
I.iterateeT[A, ErrorOr, B]

以下代码模拟了您当前的组合方式。因为 IterateeT 没有左右的概念,所以当你编写它时,你最终只会得到一堆 IO/Id。

scala> Kleisli((a:Int) => 4.right[String].point[Id])
res11: scalaz.Kleisli[scalaz.Scalaz.Id,Int,scalaz.\/[String,Int]] = scalaz.KleisliFunctions$$anon$18@73e771ca

scala> Kleisli((a:Int) => "aa".left[Int].point[Id])
res12: scalaz.Kleisli[scalaz.Scalaz.Id,Int,scalaz.\/[String,Int]] = scalaz.KleisliFunctions$$anon$18@be41b41

scala> for { a <- res11; b <- res12 } yield (a,b)
res15: scalaz.Kleisli[scalaz.Scalaz.Id,Int,(scalaz.\/[String,Int], scalaz.\/[String,Int])] = scalaz.KleisliFunctions$$anon$18@42fd1445

scala> res15.run(1)
res16: (scalaz.\/[String,Int], scalaz.\/[String,Int]) = (\/-(4),-\/(aa))

在下面的代码中,我们没有使用 Id,而是使用了 EitherT。由于 EitherT 具有与 Either 相同的绑定行为,因此我们最终得到了我们想要的。

scala>  type ErrorOr[+A] = EitherT[Id, String, A]
defined type alias ErrorOr

scala> Kleisli[ErrorOr, Int, Int]((a:Int) => EitherT(4.right[String].point[Id]))
res22: scalaz.Kleisli[ErrorOr,Int,Int] = scalaz.KleisliFunctions$$anon$18@58b547a0

scala> Kleisli[ErrorOr, Int, Int]((a:Int) => EitherT("aa".left[Int].point[Id]))
res24: scalaz.Kleisli[ErrorOr,Int,Int] = scalaz.KleisliFunctions$$anon$18@342f2ceb

scala> for { a <- res22; b <- res24 } yield 2
res25: scalaz.Kleisli[ErrorOr,Int,Int] = scalaz.KleisliFunctions$$anon$18@204eab31

scala> res25.run(2).run
res26: scalaz.Scalaz.Id[scalaz.\/[String,Int]] = -\/(aa)

您可以将 Keisli 替换为 IterateeT 并将 Id 替换为 IO 以获得您需要的内容。

于 2012-11-16T20:13:11.447 回答
4

它的方法pipes是使用Channel类型类进行类型类组合:

class Channel p where
    {-| 'idT' acts like a \'T\'ransparent proxy, passing all requests further
        upstream, and passing all responses further downstream. -}
    idT :: (Monad m) => a' -> p a' a a' a m r

    {-| Compose two proxies, satisfying all requests from downstream with
        responses from upstream. -}
    (>->) :: (Monad m)
          => (b' -> p a' a b' b m r)
          -> (c' -> p b' b c' c m r)
          -> (c' -> p a' a c' c m r)
    p1 >-> p2 = p2 <-< p1

EitherT...并从基础组合物衍生出提升的组合物。这是代理转换器原理的一个特例,在 中介绍pipes-2.4,它允许在任意扩展上提升组合。

这种提升需要定义一个EitherT专门的Proxy类型的形状Control.Proxy.Trans.Either

newtype EitherP e p a' a b' b (m :: * -> *) r
  = EitherP { runEitherP :: p a' a b' b m (Either e r) }

Proxy为了能够定义类的类型良好的实例,对形状的这种特殊化是必要的Channel。Scala 在这方面可能比 Haskell 更灵活。

然后我只是重新定义Monad实例(和其他实例)以及EitherT这种特殊类型的所有普通操作:

throw :: (Monad (p a' a b' b m)) => e -> EitherP e p a' a b' b m r
throw = EitherP . return . Left

catch
 :: (Monad (p a' a b' b m))
 => EitherP e p a' a b' b m r        -- ^ Original computation
 -> (e -> EitherP f p a' a b' b m r) -- ^ Handler
 -> EitherP f p a' a b' b m r        -- ^ Handled computation
catch m f = EitherP $ do
    e <- runEitherP m
    runEitherP $ case e of
        Left  l -> f     l
        Right r -> right r

有了这个,我就可以定义以下提升的组合实例:

-- Given that 'p' is composable, so is 'EitherP e p'
instance (Channel p) => Channel (EitherP e p) where
    idT = EitherP . idT
    p1 >-> p2 = (EitherP .) $ runEitherP . p1 >-> runEitherP . p2

要了解那里发生了什么,只需遵循以下类型:

p1 :: b' -> EitherP e p a' a b' b m r
p2 :: c' -> EitherP e p b' b c' c m r

runEitherP . p1 :: b' -> p a' a b' b m (Either e r)
runEitherP . p2 :: c' -> p b' b c' c m (Either e r)

-- Use the base composition for 'p'
runEitherP . p1 >-> runEitherP . p2
 :: c' -> p a' a c' c m (Either e r)

-- Rewrap in EitherP
(EitherP . ) $ runEitherP . p1 >-> runEitherP . p2
 :: c' -> EitherP e p a' a c' c m r

这使您可以在特定阶段内抛出和捕获错误,而不会中断其他阶段。这是我从pipes-2.4公告帖子中复制并粘贴的示例:

import Control.Monad (forever)
import Control.Monad.Trans (lift)
import Control.Proxy
import Control.Proxy.Trans.Either as E
import Safe (readMay)

promptInts :: () -> EitherP String Proxy C () () Int IO r
promptInts () = recover $ forever $ do
    str <- lift getLine
    case readMay str of
        Nothing -> E.throw "Could not parse an integer"
        Just n  -> liftP $ respond n

recover p =
    p `E.catch` (\str -> lift (putStrLn str) >> recover p)

main = runProxy $ runEitherK $ mapP printD <-< promptInts

结果如下:

>>> main
1<Enter>
1
Test<Enter>
Could not parse an integer
Apple<Enter>
Could not parse an integer
5<Enter>
5

iteratee 方法的答案是相似的。您必须采用现有的方式来编写迭代器并将其提升EitherT。无论您是使用类型类还是仅仅定义一个新的组合运算符,都取决于您。

其他一些有用的链接:

于 2012-11-17T00:19:51.593 回答