Scala Stack /:() method with example Last Updated : 02 Nov, 2019 Comments Improve Suggest changes Like Article Like Report 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: SCALA // 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) } } Output: 22 10 Example #2: SCALA // 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) } } Output: 71 Comment More infoAdvertise with us Next Article Scala Stack /:() method with example S Shivam_k Follow Improve Article Tags : Scala Scala-Method scala-collection Similar Reads Scala Stack :() method with example 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 right to left. Method Definition - def :\[B](z: B)(op: (A, B) ? B): B Returns - the result of inserting op between cons 1 min read Scala Stack +:() method with example In Scala, scala.collection.mutable implements Stack data structure. The +: method is similar to ++ method in Stack which gives a copy of the stack with an element prepended. Note that the ending operators are right associative. Method Definition - def +:(elem: A) Returns - A new stack consisting of 1 min read Scala Stack :+() method with example In Scala, scala.collection.mutable implements Stack data structure. The :+ method is used to create a copy of this stack with an element appended. Method Definition - def :+(elem: A) Returns - a new stack consisting of all elements of this stack followed by elem. Example #1: SCALA // Scala program o 1 min read Scala Stack map() method with example In Scala Stack class, the map() method is utilized to build a new stack by applying a function to all elements of the given stack. Method Definition: def map[B](f: (A) => B): Stack[B] Return Type: It returns a new stack containing all the elements after applying the given function. Example #1: Sc 2 min read Scala Set -() method with example The -() method is utilized to creates a new set with a given element removed from the set. Method Definition: def -(elem: A): Set[A] Return Type: It returns a new set with a given element removed from the set. Example #1: Scala // Scala program of -() // method // Creating object object GfG { // Mai 1 min read Scala Stack find() method with example In Scala Stack class, the find() method is utilized to return an element that satisfies a given predicate in the stack. Method Definition: def find(p: (A) => Boolean): Option[A] Return Type: It returns the first element that satisfies a given predicate if present or else it returns None. Example 2 min read Scala Stack init() method with example In Scala Stack class, the init() method is utilized to return a new stack that consists of all the elements except the last one. Method Definition: def init: Stack[A] Return Type: It returns a new stack that consists of all the elements except the last one. Example #1: Scala // Scala program of init 2 min read Scala Stack apply() method with example In Scala Stack class, the apply() method is utilized to return an element at a given position in the stack. The top of the stack corresponds to the element at 0th position and so on. Method Definition: def apply(idx: Int): A Return Type: It returns an element at a given position in the stack. Exampl 1 min read Scala Map take() method with example The take() method is utilized to select the first 'n' elements of the map. Method Definition: def take(n: Int): Map[A, B] Return Type: It returns the first 'n' elements of the map. Example #1: Scala // Scala program of take() // method // Creating object object GfG { // Main method def main(args:Arr 1 min read Scala List take() method with example The take() method belongs to the value member of the class List. It is utilized to take the first n elements from the list. Method Definition: deftake(n: Int): List[A] Where, n is the number of elements to be taken from the list. Return Type:It returns a list containing only the first n elements fro 1 min read Like