Closures , Chaining and Operators in Scala



Introduction
Have a look at the Scala code illustrated below. This code uses certain interesting features in Scala language. Try to figure out the features on your own and then read a brief description given at the end of this post. 


Program
package com.rpise

class Stack {
  class Node (val value : Int , val next : Node);
  var head : Node = null;
  def < ( value : Int ) :Stack= { head = new Node(value,head); this }
  def > (op: (Int)=>Unit):Stack = { 
         println("Value Popped -->" + ((v:Int)=>{op(v);v})(
                                        if(head == null) -1
                                         else {
                                           val t = head.value
                                           head = head.next
                                           t
                                         }
                                       ));       
         this 
  }
  def ~ () : Stack = { 
        println("Printing Stack...");
        var temp:Node = head; 
        while(temp != null){println(temp.value);temp=temp.next};
        this;
  }
}

object Program {
    def main(args: Array[String]): Unit = {
    val s1 = new Stack();
    s1<10<20<30<40<50<60<70~;
    var x=0;var y=0;
    val X = (v:Int)=>x=v
    val Y = (v:Int)=>y=v
    ((((s1>X>Y~)>(x=_)>(y=_)~)>{x=_}>{y=_}~)<6<7~)<1<2<3~;
    println("x= "+x+" y= "+y);
  }

}

Output

Printing Stack...
70
60
50
40
30
20
10
Printing Stack...
50
40
30
20
10
first value popped 70
second value popped 60
Printing Stack...
30
20
10
third value popped 50
fourth value popped 40
Printing Stack...
10
fifth value popped 30
sixth value popped 20
Printing Stack...
7
6
10
Printing Stack...
3
2
1
7
6
10





Introduction
1) Using operators for naming methods.

This feature in Scala may look like operator overloading to C++ programmers. However, in Scala technically what looks like operators are actually methods. Methods in Scala can have names like "+" ">" etc. I have used
  • "<"  for push operation
  • ">" for pop operation 
  • "~" for print operation
2) Operations are chained. 

Every operation is returning "this" handle. This results in returning of the object on which the operation was performed. So instead of writing
                      s1 < 10
                      s1 < 20
we can write 
                      s1 < 10 < 20
3)  Poped Values are captured in the closure.
"pop" operation accepts a lamdba and stored the poped value in the captured variables from main. viz x and y. 


Program
package com.rpise

class Stack {
  class Node (val value : Int , val next : Node);
  var head : Node = null;
  def < ( value : Int ) :Stack= { head = new Node(value,head); this }
  def > (op: (Int)=>Unit):Stack = { 
         println("Value Popped -->" + ((v:Int)=>{op(v);v})(
                                        if(head == null) -1
                                         else {
                                           val t = head.value
                                           head = head.next
                                           t
                                         }
                                       ));       
         this 
  }
  def ~ () : Stack = { 
        println("Printing Stack...");
        var temp:Node = head; 
        while(temp != null){println(temp.value);temp=temp.next};
        this;
  }
}

object Program {
    def main(args: Array[String]): Unit = {
    val s1 = new Stack();
    s1<10 font="">
    var x=0;var y=0;
    val X = (v:Int)=>x=v
    val Y = (v:Int)=>y=v
    ((((s1>X>Y~)>(x=_)>(y=_)~)>{x=_}>{y=_}~)<6 font="">
    println("x= "+x+" y= "+y);
  }

}

Output

Printing Stack...
70
60
50
40
30
20
10
Value Popped -->70
Value Popped -->60
Printing Stack...
50
40
30
20
10
Value Popped -->50
Value Popped -->40
Printing Stack...
30
20
10
Value Popped -->30
Value Popped -->20
Printing Stack...
10
Printing Stack...
7
6
10
Printing Stack...
3
2
1
7
6
10
x= 30 y= 20


copyright ©Rajesh Patkar, All rights reserved.

Comments

Unknown said…
Nice one.......!!!!

Great language....!

Popular posts from this blog

Harikavach ( Level 3 )

Potter Metaphor.

Video Courses Available on Web.