DataStructures in Scala

I have started populating the DataStructure Space in the Scala section.
The first entrant is a simple Stack class implemented using some interesting features of Scala Language.

Java , C++ and C# programmers can analyze the following Scala code, written for implementing a simple bounded stack. Try to figure out interesting Scala language properties. 


Simple Stack


package datastructuresinscalastack

object Main {
  def main(args: Array[String]): Unit = {
    println("Welcome to Simple Stack");
    var s1 = new Stack(10)
    val v = (1::2::3::s1).!.pop
    println("Popped Value is "+ v)
    s1 !
  }
}
class Stack(v:Int){
  var stk = new Array[Int](v)
  var sp = v
  def ::(v:Int) = { if(sp!=0){sp-=1;stk(sp)= v} else println("Overflow") ; this}
  def pop = if(sp!=v) {sp = sp + 1; stk(sp-1)} else {println("underflow"); -1}       
  def ! = { println("Printing Stack"); for( i <- sp to stk.length-1) println(stk(i)) ; this }
}


Output

Welcome to Simple Stack
Printing Stack
1
2
3
Popped Value is 1
Printing Stack
2
3



Copyright ©Rajesh Patkar, All rights reserved.

Comments

Popular posts from this blog

Harikavach ( Level 3 )

Potter Metaphor.

Grapes a Metaphor