作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是阶梯书中的一个例子:
object Example1 {
def lazyMap[T, U](coll: Iterable[T], f: T => U) = {
new Iterable[U] {
def iterator = coll.iterator.map(f)
}
}
val v = lazyMap[Int, Int](Vector(1, 2, 3, 4), x => {
println("Run!")
x * 2
})
}
控制台结果:
Run!
Run!
Run!
Run!
v: Iterable[Int] = (2, 4, 6, 8)
怎么这么懒?
最佳答案
它调用 map 函数的原因是因为您在调用 lazyMap 上的 toString
函数的 Scala 控制台中运行。如果您通过在代码末尾添加 ""
确保不返回该值,它将不会映射:
scala> def lazyMap[T, U](coll: Iterable[T], f: T => U) = {
new Iterable[U] {
def iterator = coll.iterator.map(f)
}
}
lazyMap: [T, U](coll: Iterable[T], f: T => U)Iterable[U]
scala> lazyMap[Int, Int](Vector(1, 2, 3, 4), x => {
println("Run!")
x * 2
}); ""
res8: String = ""
scala>
关于scala - lazyMap其实不懒惰?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33187567/
我最喜欢的 apache commons-collections 之一是 LazyMap,它会在执行 map.get(newKey) 时使用 Transformer 即时实例化值;//不会返回 nul
我正在使用 Grails 2.4 和 jersey request builder 插件。 我得到以下错误 def resRequestList = RESPONSE FROM REST REQUES
已解决:感谢 below answer来自 S.Richmond。我需要取消设置 groovy.json.internal.LazyMap 类型的 all 存储 map ,这意味着使变量 envSer
我有一段代码调用 google geocode API 并返回 Json 结果,像这样 def response = new JsonSlurper().parse (url.toURL())
我正在尝试创建 Jenkins 管道,它在不同阶段对我的第三方管道进行 http 调用,并根据响应进入下一阶段。下面是我的 Jenkins 文件 /* groovylint-disable Comp
我是一名优秀的程序员,十分优秀!