Posts

Showing posts from August, 2012

Google's Dart - Properties

Introduction Dart makes accessing properties easy. The following example illustrates four properties x,y,r and theta. Writing the keywords get and set make regular methods property methods. As of know Dart doesnot have keywords like private, public , protected. You can make a variable private by writing underscore in front of the variable's name. In our example _x and _y are private variables. These variables are not private to the class but to the library in which the class is declared. Even a subclass cannot access these variables if it is in a different library.  To position a class or a function in a library you use library directive. In our example we have used it as follows.

#library("geometry");

To import a library you have to write

#import("somelibrary");

Dart also supports Scala like constructor.

Point(this._x,this._y);

Program #library("geometry");
void main() {
  print("Welcome to Dart's Properties");
  var v = new Point(12.0,5.0)…

Optional Type annotations in Dart Language.

Introduction One very interesting feature of Google's dart language is it's optional type annotations. 
Mandatory type annotation Java language provides us with intrinsic types and gives us the ability to define our own types ( User Defined Types). It is mandatory to mention type when creating a variable. In Java language, types annotations are not Optional...they are Mandatory. Mandatory type annotations buy us benefits in static analysis and tooling, but they also bring their own set of constraints and verbosity to code.
No type Annotations On the other hand in Javascript there are intrinsic types and really no first class notion for User Defined types similar to that in Java.  Javascript  developers resort to idioms and patterns to emulate type systems. Absence of annotations bring brevity and a lot of freedom but lose several benefits gained from static analysis and tooling.
Optional type Annotations Dart makes type annotations optional. For smaller scripts you can skip type anno…

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 Iterable[T]{  type G;  def visit(p:G):Visitable[T] = {foreach(pattern(_,p));this}  def pattern…

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 : Node = null;   def < ( value : T ) :Stack[T]= { h = new Node(value,h); this }   def > (op: (Option[T])=>Un…

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 Itera…

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:No…