迭代器上的Scala图不产生副作用

编程入门 行业动态 更新时间:2024-10-19 16:39:10
本文介绍了迭代器上的Scala图不产生副作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为什么这样,

scala> List(1,2,3,4).iterator.map((x: Int) => println(x))

不打印出

1 2 3 4

List(1,2,3,4).map((x: Int) => println(x)) List(1,2,3,4).foreach((x: Int) => println(x)) List(1,2,3,4).iterator.foreach((x: Int) => println(x))

所有做?

换句话说,为什么是地图在一个将T型映射到Unit并具有副作用的迭代器上,不能显示这些副作用?

In other words, why is it that a map on a iterator that maps type T to Unit and has side effects unable to show those side effects?

编辑:

另外为什么下面的lazyMap的调用实际上会计算新的迭代器(提供完整的新的迭代器)从开始到结束,如果迭代器是懒惰的

Also why does the following invocation of lazyMap actually computes the new iterator (provide the complete new iterator) from beginning to end if iterator is lazy?

def lazyMap[T, U](coll: Iterable[T], f: T => U) = new Iterable[U] { def iterator = coll.iterator map f } scala> lazyMap(List(1,2,3,4), (x: Int) => x + 1) res4: java.lang.Object with Iterable[Int] = (2, 3, 4, 5)

推荐答案

因为迭代器上的映射是懒惰的,你需要一些严格性:

Cause map on iterator is lazy and you need some strictness:

scala> List(1,2,3,4).iterator.map((x: Int) => println(x)) res0: Iterator[Unit] = non-empty iterator // nothing actually happened yet, just remember to do this printing things scala> res0.toList 1 2 3 4 res1: List[Unit] = List((), (), (), ())

当您在迭代器上进行foreach时,很明显您正在做副作用,所以懒惰将是不希望的。我不会这样说,关于地图。

When you doing foreach on iterator it is quite obvious that you're doing side effects, so lazyness will be undesired. I wouldn't said so about map.

UPD

编辑:这种行为的原因是存在toString语句结果的隐式调用,这反过来限制了迭代器 - 自己尝试这个代码:

As for your edit: the reason for such behaviour, is that there is implicit call of toString for statement result which in turn stricts the iterator -- try this code on your own:

scala> { lazyMap(List(1,2,3,4), {(x: Int) => println(x); x + 1}); 1 }

,你会看到这个函数 f 从不称为

and you'll see that function f is never called

更多推荐

迭代器上的Scala图不产生副作用

本文发布于:2023-11-14 00:31:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585725.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:副作用   器上   迭代   Scala

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!