迭代器上的Scala图不产生副作用 [英] Scala map on iterator does not produce side effects

查看:118
本文介绍了迭代器上的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图不产生副作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆