한가지는 '지퍼'를 사용하는 것입니다 (응용 프로그램에 따라 예를 들어, 당신이 등 ... 종종 지점을 업데이트), 아래를 참조하십시오. 'moveTo'메소드 또는 이와 유사한 메소드를 추가 할 수 있습니다. 불변 목록을 다루는 경우 잠재적으로 업데이트 수가 줄어 듭니다. 나는 위의 간단한 해결책이 아마 작은 목록을위한 최선이라고 생각한다.
편집 : 당신은 당신이 등 서로 가까운 요소를 쫓는 알고 있다면 아마 불통 될 수
case class Zipper[A](focus: A, left: List[A], right: List[A]) {
def fromZipper: List[A] = left ::: List(focus) ::: right
/** Will throw NoSuchElementException if try to move beyond start. */
/** directions are forward and backward across the list. */
def moveForward: Zipper[A] = Zipper(right.head,left :+ focus,right.tail)
def moveBackward: Zipper[A] = Zipper(left.last,left.init,focus :: right)
/** Update the focus element. */
def update(a: A): Zipper[A] = Zipper(a,left,right)
}
def apply[A](left: List[A], focus: A, right: List[A]): Zipper[A]
= Zipper(focus,left,right)
def apply[A](xs: List[A]): Zipper[A]
= Zipper(xs.head,List.empty[A],xs.tail)
여기서 핵심 비트, 당신이 언급하고있다리스트는 불변, 당신은 할 수 있도록 항목을 교환하십시오. – Sean