Comments¶
- An array in Java is not an
Iterable
(due to design reasons). There is no super class of array andIterable
either in Java. If you want a method to support both array andIterable
as the parameter, you need to overload it (for both array andIterable
.) It is the same situation in Kotlin as Kotlin is mostly Java.
Collections in Kotlin
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).
Array in Scala
Construct an element with initial values.
Iterator in Kotlin
Tips and Traps¶
- Notice that Iterable.map
returns a List instead of an iterator.
Since almost all collections in Kotlin eventually inherit from
Iterable
, themap
method of all collections (includingHashMap
) in Kotlin returns List.