Make your Object Iterable in Scala using Trait



Introduction
Make you object Iterable by mixing you class with Iterable Trait. The current post uses example of Stack Object designed in the previous post. I will illustrate two variations to this approach. 


Program

package com.rajeshpatkar

class Stack extends Iterable[Int]{
  class Node (val value : Int , val next : Node);
  var h : Node = null;
  def < ( value : Int ) :Stack= { h = new Node(value,h); this }
  def > (op: (Int)=>Unit):Stack = { 
         var value = if(h == null) -1
                     else {
                            val t = h.value
                            h = h.next
                            t
                     }  
         op(value)           
         println("Value Popped -->" + value)     
         this 
  }
  def ~ () : Stack = { 
                       println("Printing Stack...")
                       foreach(println)
                       this;
  }
  override def iterator():Iterator[Int] = new SIterator();
  class SIterator extends Iterator[Int]{
                   var data = List[Int]();
                   var temp = h;
                   while(temp != null){
                     data = temp.value::data;
                     temp=temp.next;
                   }
                 override def hasNext():Boolean = 
                          !data.isEmpty;
                 override def next():Int= {
                         var v = data.head
                         data = data.tail
                         v
                 }
                   
  }
}


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 font="">
    println("x= "+x+" y= "+y);
    s1.foreach(println);
    println("Printing filtered numbers..");
    s1.filter(_>3).foreach(println);
    }

}


Output

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


Brief Pointers to features Used
Type which has to be made Iterable must be mixed with Trait Iterable. In our case class Stack must be mixed with Iterable[Int].

     "Stack extends Iterable[Int]"

      You need to override iterator method. 

      "override def iterator():Iterator[Int] = new SIterator()"
   
Type SIterator, extends Iterator and overrides hasNext and next methods. These methods will be used by other utility methods in Iterable traits. 


Program

package com.rajeshpatkar

class Stack extends Iterable[Int]{
  class Node (val value : Int , val next : Node);
  var h : Node = null;
  def < ( value : Int ) :Stack= { h = new Node(value,h); this }
  def > (op: (Int)=>Unit):Stack = { 
         var value = if(h == null) -1
                     else {
                            val t = h.value
                            h = h.next
                            t
                     }  
         op(value)           
         println("Value Popped -->" + value)     
         this 
  }
  def ~ () : Stack = { 
                       println("Printing Stack...")
                       foreach(println)
                       this;
  }
  override def iterator():Iterator[Int] = (
          ()=>{
                 var data = List[Int]()
                 var temp = h;
                 while(temp != null){
                    data = temp.value::data
                    temp=temp.next
                 }
                 data
               })().iterator;  
  }



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 font="">
    println("x= "+x+" y= "+y);
    s1.foreach(println);
    println("Printing filtered numbers..");
    s1.filter(_>3).foreach(println);
    }
}


Output

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


copyright ©Rajesh Patkar, All rights reserved.

Comments

Popular posts from this blog

Potter Metaphor.

Chrome

Harikavach ( Level 3 )