设为首页 加入收藏

TOP

Scalaz(23)- 泛函数据结构: Zipper-游标定位(三)
2017-10-10 12:13:24 】 浏览:9692
Tags:Scalaz 数据结构 Zipper- 游标 定位
gt;: A](y: AA): Zipper[AA] = zipper(focus #:: lefts, y, rights) /** * An alias for `deleteRight` */ def delete: Option[Zipper[A]] = deleteRight /** * Deletes the element at focus and moves the focus to the left. If there is no element on the left, * focus is moved to the right. */ def deleteLeft: Option[Zipper[A]] = lefts match { case l #:: ls => Some(zipper(ls, l, rights)) case Stream.Empty => rights match { case r #:: rs => Some(zipper(Stream.empty, r, rs)) case Stream.Empty => None } } /** * Deletes the element at focus and moves the focus to the left. If there is no element on the left, * focus is moved to the right. */ def deleteLeftOr[AA >: A](z: => Zipper[AA]): Zipper[AA] = deleteLeft getOrElse z /** * Deletes the element at focus and moves the focus to the right. If there is no element on the right, * focus is moved to the left. */ def deleteRight: Option[Zipper[A]] = rights match { case r #:: rs => Some(zipper(lefts, r, rs)) case Stream.Empty => lefts match { case l #:: ls => Some(zipper(ls, l, Stream.empty)) case Stream.Empty => None } } /** * Deletes the element at focus and moves the focus to the right. If there is no element on the right, * focus is moved to the left. */ def deleteRightOr[AA >: A](z: => Zipper[AA]): Zipper[AA] = deleteRight getOrElse z /** * Deletes all elements except the focused element. */ def deleteOthers: Zipper[A] = zipper(Stream.Empty, focus, Stream.Empty) ... /** * Update the focus in this zipper. */ def update[AA >: A](focus: AA) = { this.copy(this.lefts, focus, this.rights) } /** * Apply f to the focus and update with the result. */ def modify[AA >: A](f: A => AA) = this.update(f(this.focus)) ...

insert,modify,delete也很齐备。值得注意的是多数Zipper的移动函数和操作函数都返回Option[Zipper[A]]类型,如此我们可以用flatMap把这些动作都连接起来。换句话说就是我们可以用for-comprehension在Option的context内?实现行令编程(imperative programming)。我们可以通过一些例子来示范Zipper用法:

 1 val zv = for {  2     z <- List(2,8,1,5,4,11).toZipper  3     s1 <- z.next  4     s2 <- s1.modify{_ + 2}.some  5   } yield s2                                      //> zv : Option[scalaz.Zipper[Int]] = Some(Zipper(<lefts>, 10, <rights>))
 6   
 7   zv.get.show          //> res8: scalaz.Cord = Zipper(Stream(2), 10, Stream(1,5,4,11))
 8   zv.get.toList        //> res9: List[Int] = List(2, 10, 1, 5, 4, 11)
 9 ... 10 val zv = for { 11     z <- List(2,8,1,5,4,11).toZipper 12     s1 <- z.next 13     s2 <- s1.modify{_ + 2}.some 14     s3 <- s2.move(1) 15     s4 <- s3.delete 16   } yield s4                                      //> zv : Option[scalaz.Zipper[Int]] = Some(Zipper(<lefts>, 5, <rights>))
17   
18   zv.get.show       //> res8: scalaz.Cord = Zipper(Stream(10,2), 5, Stream(4,11))
19   zv.get.toList     //> res9: List[Int] = List(2, 10, 5, 4, 11)
20 ... 21 val zv = for { 22     z <- List(2,8,1,5,4,11).toZipper 23     s1 <- z.next 24     s2 <- s1.modify{_ + 2}.some 25     s3 <- s2.move(1) 26     s4 <- s3.delete 27     s5 <- s4.findZ {_ === 11} 28     s6 <- if (s5.focus === 12) s5.delete else s2.insert(12).some 29   } yield s6                                      //> zv : Option[scalaz.Zipper[Int]] = Some(Zipper(<lefts>, 12, <rights>))
30   
31   zv.get.show        //> res8: scalaz.Cord = Zipper(Stream(10,2), 12, Stream(1,5,4,11))
32   zv.get.toList      //> res9: List[Int] = List(2, 10, 12, 1, 5, 4, 11)
33 ... 34 val zv = for { 35     z <- List(2,8,1,5,4,11).toZipper 36     s1 <- z.next 37     s2 <- s1.modify{_ + 2}.some 38     s3 <- s2.move(1) 39     s4 <- s3.delete 40     s5 <- s4.findZ {_ === 11} 41     s6 <- if (s5.focus === 12) s5.delete else s2.insert(12).some 42     s7 <- s6.end.delete 43     s8 <- s7.start.some 44   } yield s8                                      //>
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Scalaz(22)- 泛函编程思维: C.. 下一篇\x 开头编码的数据解码成中文

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目