基本 Scalaz 状态问题

发布于 2024-10-09 10:11:49 字数 2511 浏览 7 评论 0原文

如何使用 State 来模仿 List.zipWithIndex 的行为?到目前为止我想出的(不起作用)是:

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)] )
}

这是非常松散地基于 状态示例。正如我所说,它不起作用:

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))

我可以通过更改 case 语句的一行来使其工作:

case x :: xs => (init[Int]) map { s : Int => (x -> s) :: (numberSA(xs) ! (s + 1)) }

但这感觉不对。有人可以帮忙吗?

编辑 - 更多的尝试让我做到了这一点

def numberSA[A](list : List[A]) : State[Int, List[(A, Int)]] = {
  def single(a : A) : State[Int, List[(A, Int)]] = (init[Int] <* modify((_ : Int) + 1)) map { s : Int => List(a -> s) }
  list match {
    case Nil     => state( (_ : Int) -> nil[(A, Int)] )
    case x :: xs => (single(x) <**> numberSA(xs)) { _ ::: _ }
  }
}

它可以改进吗?它可以推广到 List 以外的容器吗(如果是的话,需要什么类型类?)

编辑 2 - 我现在已经推广了它,尽管有点笨拙

def index[M[_], A](ma : M[A])
      (implicit pure : Pure[M], empty : Empty[M], semigroup : Semigroup[M[(A, Int)]], foldable : Foldable[M]) 
      : State[Int, M[(A, Int)]] = {
  def single(a : A) : State[Int, M[(A, Int)]] = (init[Int] <* modify((_ : Int) + 1)) map { s : Int => pure.pure(a -> s) }
  foldable.foldLeft(ma, state( (_ : Int) -> empty.empty[(A, Int)] ), { (s : State[Int, M[(A, Int)]],a : A) => (s <**> single(a)) { (x,y) => semigroup.append(x,y)}  } )
}

或者非常相似:

def index[M[_] : Pure : Empty : Plus : Foldable, A](ma : M[A]) 
     : State[Int, M[(A, Int)]] = {
  import Predef.{implicitly => ??}
  def single(a : A) : State[Int, M[(A, Int)]] = (init[Int] <* modify((_ : Int) + 1)) map { s : Int => ??[Pure[M]].pure(a -> s) }
  ??[Foldable[M]].foldLeft(ma, state( (_ : Int) -> ??[Empty[M]].empty[(A, Int)] ), { (s : State[Int, M[(A, Int)]],a : A) => (s <**> single(a)) { (x,y) => ??[Plus[M]].plus(x,y)}  } )
}

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 to work by changing a line of the case statement:

case x :: xs => (init[Int]) map { s : Int => (x -> s) :: (numberSA(xs) ! (s + 1)) }

But this just feels wrong. Can anyone help?

EDIT - more playing around has got me to this

def numberSA[A](list : List[A]) : State[Int, List[(A, Int)]] = {
  def single(a : A) : State[Int, List[(A, Int)]] = (init[Int] <* modify((_ : Int) + 1)) map { s : Int => List(a -> s) }
  list match {
    case Nil     => state( (_ : Int) -> nil[(A, Int)] )
    case x :: xs => (single(x) <**> numberSA(xs)) { _ ::: _ }
  }
}

Can it be improved? Can it be generalized to containers other than List (and, if so, what typeclasses are needed?)

EDIT 2 - I have now generalized it, albeit a bit clunkily

def index[M[_], A](ma : M[A])
      (implicit pure : Pure[M], empty : Empty[M], semigroup : Semigroup[M[(A, Int)]], foldable : Foldable[M]) 
      : State[Int, M[(A, Int)]] = {
  def single(a : A) : State[Int, M[(A, Int)]] = (init[Int] <* modify((_ : Int) + 1)) map { s : Int => pure.pure(a -> s) }
  foldable.foldLeft(ma, state( (_ : Int) -> empty.empty[(A, Int)] ), { (s : State[Int, M[(A, Int)]],a : A) => (s <**> single(a)) { (x,y) => semigroup.append(x,y)}  } )
}

Or the very similar:

def index[M[_] : Pure : Empty : Plus : Foldable, A](ma : M[A]) 
     : State[Int, M[(A, Int)]] = {
  import Predef.{implicitly => ??}
  def single(a : A) : State[Int, M[(A, Int)]] = (init[Int] <* modify((_ : Int) + 1)) map { s : Int => ??[Pure[M]].pure(a -> s) }
  ??[Foldable[M]].foldLeft(ma, state( (_ : Int) -> ??[Empty[M]].empty[(A, Int)] ), { (s : State[Int, M[(A, Int)]],a : A) => (s <**> single(a)) { (x,y) => ??[Plus[M]].plus(x,y)}  } )
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

︶葆Ⅱㄣ 2024-10-16 10:11:49
def index[M[_]:Traverse, A](m: M[A]) =
  m.traverse[({type λ[x] = State[Int,x]})#λ, (A, Int)](a =>
    state(i => (i + 1, (a, i)))) ! 0

甚至...

def index[M[_]:Traverse, A](m: M[A]) =
  m.traverse[({type λ[x] = State[Int,x]})#λ, (A, Int)](a =>
    (Lens.self[Int] += 1) map ((a, _)) ! -1

参见迭代器模式的本质 有关使用 State 进行遍历的更多信息。

def index[M[_]:Traverse, A](m: M[A]) =
  m.traverse[({type λ[x] = State[Int,x]})#λ, (A, Int)](a =>
    state(i => (i + 1, (a, i)))) ! 0

Or even...

def index[M[_]:Traverse, A](m: M[A]) =
  m.traverse[({type λ[x] = State[Int,x]})#λ, (A, Int)](a =>
    (Lens.self[Int] += 1) map ((a, _)) ! -1

See The Essence of the Iterator Pattern for more on traversing with State.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文