Related Articles

Related Articles

Scala Stack /:() method with example
  • Last Updated : 02 Nov, 2019

In Scala, scala.collection.mutable implements Stack data structure. The /: method applies a binary operator to a start value and all elements of this traversable or iterator, going left to right.

Method Definition – def /:[B](z: B)(op: (B, A) ? B): B

Returns – the result of inserting op between consecutive elements of this traversable or iterator.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of mutable stack /:() 
// method 
  
// Import Stack 
import scala.collection.mutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
          
        val st1 = Stack(1, 2, 3
          
          
        // Applying /:() method 
        val result1 = (16 /: st1)(_+_)
        val result2 = (16 /: st1)(_-_)
              
        // Display output 
        print(result1)
        print("\n")
        print(result2)
          
    

chevron_right


Output:

22
10

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of mutable stack /:() method 
  
// Import Stack 
import scala.collection.mutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
        // Creating a stack 
        val q2 = List(11, 12, 13, 14, 15
          
        // Applying /:() method 
        val result = (6 /: q2)(_+_)
              
        // Display output 
        print(result) 
          
    

chevron_right


Output:

71



My Personal Notes arrow_drop_up
Recommended Articles
Page :