Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala视图的Groovy等价物_Scala_Groovy - Fatal编程技术网

Scala视图的Groovy等价物

Scala视图的Groovy等价物,scala,groovy,Scala,Groovy,我正在尝试将此Scala代码转换为Groovy: val r = BigInt(2).pow(1000).toString.view.map(_.asDigit).sum groovy的“视图”等价于什么?Java8带来了一个迭代器(基本上是一个迭代器),您可以将其减少为: groovy:000> 2G.pow(1000).toString().chars().reduce(0){ a,b -> a+b-48 } ===> 1366 或更接近您的Scala代码(映射、求和)

我正在尝试将此Scala代码转换为Groovy:

val r = BigInt(2).pow(1000).toString.view.map(_.asDigit).sum
groovy的“视图”等价于什么?

Java8带来了一个迭代器(基本上是一个迭代器),您可以将其减少为:

groovy:000> 2G.pow(1000).toString().chars().reduce(0){ a,b -> a+b-48 }
===> 1366
或更接近您的Scala代码(映射、求和):

魔法48是“0”的ascii码

一般的回答是:groovy中的许多函数方法都倾向于实现结果,但是Java8流在groovy中同样易于使用

def result = new BigInteger(2).pow(1000).toString().toCharArray().toList().sum { it - 48 }


groovy中似乎没有等价的
视图
。我想知道
“abc.iterator()
是否懒惰?@tim_yates否,str.iterator()创建了一个数组列表
toCharArray
:“将这个字符串转换为一个新的字符数组。”;使用
collect
的“更具可读性”版本已经被否决。
def result = new BigInteger(2).pow(1000).toString().toCharArray().toList().sum { it - 48 }
def result1 = 2g.pow(1000).toString().toCharArray().toList().sum { it - 48 }