Scala Trait Traversable | Set-4
Last Updated :
15 Apr, 2019
Prerequisite :-
Scala Trait Traversable | Set-1
Scala Trait Traversable | Set-2
Scala Trait Traversable | Set-3
It is recommended to view Set-1, Set2 and Set-3 before this one.
The operations are as follows:
- 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 :
object Folds
{
def main(args : Array[String])
{
val x = List( 8 , 6 , 4 , 2 )
val y = ( 0 / : x)( _ - _ )
println(y)
}
}
|
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 :
object Folds
{
def main(args : Array[String])
{
val x = List( 8 , 6 , 4 , 2 )
val y = (x : \ 0 )( _ - _ )
println(y)
}
}
|
Here, foldRight will perform binary operation between successive elements of the collection, moving from left to right and ending with initial value.
Example :
object Folds
{
def main(args : Array[String])
{
val x = List( 8 , 6 , 4 , 2 )
val y = x.reduceLeft( _ - _ )
println(y)
}
}
|
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 :
object Folds
{
def main(args : Array[String])
{
val x = List( 8 , 6 , 4 , 2 )
val y = x.reduceRight( _ - _ )
println(y)
}
}
|
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 :
object Specificfold
{
def main(args : Array[String])
{
val x = List( 3 , 4 , 7 , 10 )
val y = x.sum
println(y)
}
}
|
Here, sum will return sum of all the elements in the collection given.
Example :
object Specificfold
{
def main(args : Array[String])
{
val x = List( 2 , 5 , 10 , 12 )
val y = x.product
println(y)
}
}
|
Here, product will return product of all the elements in the collection given.
Example :
object Specificfold
{
def main(args : Array[String])
{
val x = List( 21 , 15 , 27 , 22 )
val y = x.max
println(y)
}
}
|
Here, max will return largest element of all the elements in the collection given.
Example :
object Specificfold
{
def main(args : Array[String])
{
val x = List( 21 , 15 , 27 , 22 )
val y = x.min
println(y)
}
}
|
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 :
object Strings
{
def main(args : Array[String])
{
val x = List( 7 , 8 , 9 , 10 , 11 )
val v = List( 21 , 19 , 17 , 15 )
val y = x.mkString( " < " )
val w = v.mkString( " > " )
println(y)
println( "\n" )
println(w)
}
}
|
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 :
object Strings
{
def main(args : Array[String])
{
val x = List( 7 , 8 , 9 , 10 , 11 )
val B = new StringBuilder( "The numbers are: " )
val y = x.addString(B, ", " )
println(y)
}
}
|
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 :
object Strings
{
def main(args : Array[String])
{
val x = Set( 7 , 8 , 9 , 10 , 11 )
val y = x.stringPrefix
println(y)
}
}
|
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.
Share your thoughts in the comments
Please Login to comment...