Posts

Showing posts with the label scala

Abstract member types, Parameterized types and Pattern Matching.

Introduction Make your Iterable stack object visitable using Trait. This implementation varies from the implementation in   previous post  . This solution uses features like Abstract member type, Type Parameterization & Pattern Matching in Scala. This example illustrates how Abstract types and parameterized types can be used in tandem. PointVisitor encapsulates the details of Specialized Point Structures.   Program package com.rajeshpatkar object Program {   def main(args: Array[String]): Unit = {     val p1 = Point2D(1,2)     val p2 = Point3D(5,6,7)     val p3 = PointPolar(1,34)     val s1 = new PStack();     s1 < p1 < p2 < p3 ~ ;     s1.visit(_*3).visit(_*5).visit(_-2);   } } abstract class Point case class Point2D(val x:Int , val y:Int) extends Point case class Point3D(val x:Int,val y:Int,val z:Int) extends Point case class PointPolar(val r:Int ,val theta:Int) extends Point trait Visitable[T] extends It

Visitor Pattern UseCase in Scala using Pattern Matching and Traits

Introduction Make your Iterable stack object  visitable  using  Trait . I am using the Stack Implementation of the  previous post . Scala code presented here illustrates several features of Scala language. Generics ( Parameterized types), Pattern Matching and Traits.  Program package com.rajeshpatkar object Program {   def main(args: Array[String]): Unit = {     val p1 = Point2D(1,2)     val p2 = Point3D(5,6,7)     val p3 = PointPolar(1,34)     (Stack[Point]() < p1 < p2 < p3 ~).visit(_*2).visit(_*33).visit(_-2);   } } abstract class Point case class Point2D(val x:Int , val y:Int) extends Point case class Point3D(val x:Int,val y:Int,val z:Int) extends Point case class PointPolar(val r:Int ,val theta:Int) extends Point trait Visitable[T]{   def visit(p:(Int)=>Int):T } case class Stack[T] extends Iterable[T] with Visitable[Stack[T]]{   class Node (val value : T , val next : Node);   var h

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)                    

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