Folds:
The operations here are reduceRight, reduceLeft, /: or foldLeft, :\ or foldRight. This methods apply binary operation to the successive elements of the Traversable collection.
Example :
Output:
-20
Here, foldLeft(0) is the desired operation where zero is the 'initial value'.
This method applies binary operation between successive elements of the given Traversable collection, moving from left to right and starting with the initial value.
Example :
Output:
4
Here, foldRight will perform binary operation between successive elements of the collection, moving from left to right and ending with initial value.
Example :
Output:
-4
Here, reduceLeft performs the binary operation between the successive elements of the collection like foldLeft but considering first element of the given collection as initial value.
Example :
Output:
4
Here, reduceRight will perform binary operation like foldRight but considering last element of the collection as initial value.
Specific folds:
The operations here are min, max, product, and sum. These operations operate on distinct kind of collections of Traversable.
Example :
Output:
24
Here, sum will return sum of all the elements in the collection given.
Example :
Output:
1200
Here, product will return product of all the elements in the collection given.
Example :
Output:
27
Here, max will return largest element of all the elements in the collection given.
Example :
Output:
15
Here, min will return smallest element of all the elements in the collection given.
String operations :
The operations here are addString, mkString, stringPrefix. This operations are utilized to provide another methods for converting a given collection of Traversables to a String.
Example :
Output:
7 < 8 < 9 < 10 < 11
21 > 19 > 17 > 15
Here, mkString("<") is the desired operation where, "<" is the separator.
This operation is utilized to return the given collection of elements separated by the given separator.
Example :
Output:
The numbers are: 7, 8, 9, 10, 11
Here, addString(B, ", ") is the desired operation where, "B" is a String builder and ", " is a separator.
This operation Adds the given collection of the elements in the StringBuilder separated by the stated separator.
Example :
Output:
Set
Here, stringPrefix will return the name of the collection used.
Copying operations:
The two Copying operations are copyToArray and copyToBuffer. These operations are utilized to Copy the elements of the Traversable Collection to an Array or to a buffer.
View operations:
The operations here are view, view(from, to). These operations are utilized to produce a view over the collection of Traversable given.