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).
In [2]:
0.until(3).foreach { println }
In [3]:
(0 until 3).foreach{println}
In [4]:
0.until(9).by(2).foreach { println }
In [5]:
(0 until 9 by 2).foreach { println }
In [15]:
(9 until 0 by -2).foreach { println }
to¶
to
is inclusive on both ends.
In [7]:
0.to(3).foreach{
println
}
In [12]:
(0 to 3).foreach{
println
}
In [11]:
0.to(9).by(2).foreach { println }
In [13]:
(0 to 9 by 2).foreach { println }
In [14]:
3.to(0).by(-1).foreach{ println }
In [ ]: