Skip to content
Related Articles

Related Articles

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 :




    // Scala program for operations
    // of Folds
      
    // Creating object 
    object Folds
    {
      
        // Main method
        def main(args: Array[String]) 
        {
      
            // Creating List of numbers 
            val x = List(8, 6, 4, 2)
      
            // Applying method of
            // Folds
            // val y = x.foldLeft(0)(_ - _) or
            val y = (0 /: x)(_ - _)
      
            // Evaluates the Expression 
            // given below and displays it
            // ((((0 - 8) - 6) - 4) - 2) 
            println(y)
      
        }
    }
    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 :




    // Scala program for operations
    // of Folds
      
    // Creating object 
    object Folds
    {
      
        // Main method
        def main(args: Array[String]) 
        {
      
            // Creating List of numbers 
            val x = List(8, 6, 4, 2)
      
            // Applying method of
            // Folds
            // val y = x.foldRight(0)(_ - _) or
            val y = (x :\ 0)(_ - _)
      
            // Evaluates the Expression 
            // given below and displays it
            // (8 - (6 - (4 - (2 - 0))))
            println(y)
        }
    }
    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 :




    // Scala program for operations
    // of Folds
      
    // Creating object 
    object Folds
    {
      
        // Main method
        def main(args: Array[String]) 
        {
      
            // Creating List of numbers 
            val x = List(8, 6, 4, 2)
      
            // Applying method of folds
            val y = x.reduceLeft(_ - _)
      
            // Evaluates the Expression 
            // given below and displays it
            // (((8 - 6) - 4) -2)
            println(y)
        }
    }
    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 :




    // Scala program for operations
    // of Folds
      
    // Creating object 
    object Folds
    {
      
        // Main method
        def main(args: Array[String]) 
        {
      
            // Creating List of numbers 
            val x = List(8, 6, 4, 2)
      
            // Applying method of folds
            val y = x.reduceRight(_ - _)
      
            // Evaluates the Expression 
            // given below and displays it
            // (8 - (6 - (4 - 2)))
            println(y)
        }
    }
    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 :




    // Scala program for operations
    // of Specific folds
      
    // Creating object 
    object Specificfold
    {
      
        // Main method
        def main(args: Array[String]) 
        {
      
            // Creating List of numbers 
            val x = List(3, 4, 7, 10)
      
            // Applying method of 
            // Specific folds
            val y = x.sum
      
            // Displays sum of all the
            // elements in the list 
            println(y)
        }
    }
    Output:
    24
    

    Here, sum will return sum of all the elements in the collection given.
    Example :




    // Scala program for operations
    // of Specific folds
      
    // Creating object 
    object Specificfold
    {
      
        // Main method
        def main(args: Array[String]) 
        {
      
            // Creating List of numbers 
            val x = List(2, 5, 10, 12)
      
            //Applying method of 
            //Specific folds
            val y = x.product
      
            //Displays product of all 
            //the elements in the list 
            println(y)
        }
    }
    Output:



    1200
    

    Here, product will return product of all the elements in the collection given.
    Example :




    // Scala program for operations
    // of Specific folds
      
    // Creating object 
    object Specificfold
    {
      
        // Main method
        def main(args: Array[String]) 
        {
      
            // Creating List of numbers 
            val x = List(21, 15, 27, 22)
      
            // Applying method of 
            // Specific folds
            val y = x.max
      
            // Displays largest element
            // of the list
            println(y)
        }
    }
    Output:
    27
    

    Here, max will return largest element of all the elements in the collection given.
    Example :




    // Scala program for operations
    // of Specific folds
      
    // Creating object 
    object Specificfold
    {
      
        // Main method
        def main(args: Array[String]) 
        {
      
            // Creating List of numbers 
            val x = List(21, 15, 27, 22)
      
            // Applying method of 
            // Specific folds
            val y = x.min
      
            // Displays smallest element
            // of the list
            println(y)
        }
    }
    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 :




    // Scala program for operations
    // of Strings
      
    // Creating object 
    object Strings
    {
      
        // Main method
        def main(args: Array[String]) 
        {
      
            // Creating List of numbers 
            val x = List(7, 8, 9, 10, 11)
            val v = List(21, 19, 17, 15)
      
            // Applying Strings operations
            val y = x.mkString(" < ")
            val w = v.mkString(" > ")
      
            // Displays all the elements
            // separated by (<)
            println(y)
            println("\n")
      
            // Displays all the elements
            // separated by (>)
            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 :




    // Scala program for operations
    // of Strings
      
    // Creating object 
    object Strings
    {
      
        // Main method
        def main(args: Array[String]) 
        {
      
            // Creating List of numbers 
            val x = List(7, 8, 9, 10, 11)
      
            // Creating StringBuilder
            val B = new StringBuilder("The numbers are: ")
          
            // Applying Strings operation
            val y = x.addString(B, ", ")
      
            // Displays all the elements
            // of the list in the String 
            // builder separated by (,)
            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 :




    // Scala program for operations
    // of Strings
      
    // Creating object 
    object Strings
    {
      
        // Main method
        def main(args: Array[String]) 
        {
      
            // Creating a Set of numbers 
            val x = Set(7, 8, 9, 10, 11)
          
            // Applying Strings operation
            val y = x.stringPrefix
      
            // Displays the name of the 
            // collection used
            println(y)
        }
    }
    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.
My Personal Notes arrow_drop_up
Recommended Articles
Page :