Creating an Enumeratee from a stateful algorithm

≯℡__Kan透↙ 提交于 2019-12-06 13:38:38
lmm

The only thing you need to implement is the applyOn method:

def applyOn[A](inner: Iteratee[To, A]): Iteratee[From, Iteratee[To, A]] = ...

Everything else is implemented in the trait.

When creating an iteratee, I find recursion is the most important trick; it's a continuation-like style where rather than returning anything, each step computes the thing it needs to compute and then calls into it again. So your state should become a function parameter:

def next[A](inner: Iteratee[To, A], i: Input[From], state: Symbol)
  : Iteratee[From, A] =
  i match {
    case Input.El(e) =>
      if(state == 'foo && e >= 0) {
        val nextInner = Iteratee.flatten {
          inner.feed(Input.El(e - 2)) flatMap {_.feed(Input.El(e * 3))}
        }
        val nextState = 'bar
        Cont {k => next(nextInner, k, nextState)}
      } else if(state == 'foo)
        Error("expected >=0", i)
      else if(state == 'bar)
        next(inner, i, 'foo)
      ...
    case _ =>
      //pass through Empty or Eof
      Iteratee.flatten(inner.feed(i))
  }
def applyOn[A](inner: Iteratee[To, A]): Iteratee[From, Iteratee[To, A]] =
  Cont {i => next(inner, i, 'foo)}

Notice how we return either a direct recursive call to next, or a continuation that will eventually make a (mutually) recursive call to next.

I've passed the Input around explicitly to every call, whereas a more elegant approach might handle it in the continuation (and perhaps implement applyOn directly rather than having it as a wrapper method), but hopefully this makes it clear what's going on. I'm sure there are more elegant ways to achieve the desired result (I've used scalaz iteratees but I don't know the Play API at all), but it's nice to work through the explicit solution at least once so we understand what's really going on underneath.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!