Fold vs Reduce¶
fold
takes an initial value,
and the first invocation of the lambda you pass to it
will receive that initial value
and the first element of the collection as parameters.
reduce
doesn't take an initial value,
but instead starts with the first element of the collection as the accumulator (called sum in the following example).
Equivalence of the Scala Collection Scan Method¶
There is no equivalence of the Scala collection scan method. You have to implement it yourself using zip.
In [5]:
0.until(3).forEach { println(it) }
Out[5]:
In [7]:
(0 until 3).forEach{println(it)}
Out[7]:
In [19]:
0.until(9).step(2).forEach { println(it) }
Out[19]:
In [20]:
(0 until 9 step 2).forEach { println(it) }
Out[20]:
In [3]:
0.rangeTo(3).forEach{
println(it)
}
Out[3]:
In [9]:
(0..3).forEach{
println(it)
}
Out[9]:
In [17]:
0.rangeTo(9).step(2).forEach { println(it) }
Out[17]:
In [16]:
(0..9 step 2).forEach { println(it) }
Out[16]:
downTo¶
downTo
is inclusive on both ends.
In [4]:
3.downTo(0).forEach{ println(it) }
Out[4]:
In [ ]: