Welcome to my blog. Here are my most recent blog posts:
  • Functors

    April 7, 2013

    1. A contructor or apply method which for any type A, we can construct a type in the new category T[A]
    2. map method which given a transformed type T[A] and a function that transforms A=>B we can construct T[B]

    Using these 2 properties we can tranform a type T[A] to a T[B] using the map method with the following steps:

    1. use the A=>B function to transform the A type inside the T[A] into a B
    2. use the contructor(1st property) to create a T[B] using the B from step 1.

    The result is that we can have a transormation from T[A]=>T[B] using the above properties.


  • Eight Puzzle solution in scala

    February 22, 2013

    This post is about solving an interesting game as a search problem in Scala and seeing how different heuristics can help find optimal solutions much faster than than regular breath first algorithm. Similar game search problems for example the water pouring problem and the Bloxorz were part of Martin's lectures and homework in the Streams (lazy evaluated lists) part of the course. The problem came from the coursera course in Artificial Intelligence Planning.

    The problem is the Eight Puzzle Problem described more precisely here.


  • Type classes

    February 5, 2013

    Type classes were originally developed in Haskell as a disciplined alternative to ad-hoc polymorphism. Type classes have been shown to provide a type-safe solution to important challenges in software engineering and programming languages such as, for example, retroactive extension of programs. They are also recognized as a good mechanism for concept-based generic programming ...

    As it turns out type classes are a good way to

    1. extend functionality of our types without having to change original code
    2. to write generic code based on concepts

    Sounds nice, but lets see what we have to do to get it.


  • Partial Functions

    January 28, 2013

    Partial functions are functions that are not defined for all possible input.

    The main distinction between PartialFunction and scala.Function1 is that the user of a PartialFunction may choose to do something different with input that is declared to be outside its domain.

    So you can pretty much use a PartialFunction anywhere you would use a Function1 since it is a proper subtype, plus some extras.


  • Functions and function compositions

    January 27, 2013

    Scala has first-class functions. Not only can you define functions and call
    them, but you can write down functions as unnamed literals and then pass
    them around as values.

    And as such we can do the following:

    So we have first class functions, and what we did is good already, but what else can we do?

    Well in scala functions inherit from Function1...22 traits depending on the number of arguments they have. The Function1 trait provides some extra functionality we can use to get some more juice out of scala functions.


  • Scala 2.10 string interpolation

    January 23, 2013

    A new feature in Scala 2.10 is string interpolation. The idea is that anytime the compiler finds a literal of the form id"some string", it transforms it to a method call on an instance of StringContext. Scala 2.10 defines 3 methods for this use: s, f, raw. You can see basic use of each bellow:

    Nice thing about this feature is that you can very easily create your own functions for similar use, either to support some other string format like json, xml etc or to create your own functions. For example the following.


  • Optio-null

    January 17, 2013

    One of the first classes you will encounter in Scala is Option class which has 2 subclasses Some and None. 

    Represents optional values. Instances of Option are either an instance of Scala. Some or the object None

    Basic use case of declaring and using pattern matching on options, notice the difference between Some(null) and Option(null):

    Lets try toList on option values.

    Since None is like the "empty list", you can use them to compose expressions over Options without having to worry about None, in the same way you dont have to worry about handling the empty list.


  • Intro to type variance

    January 16, 2013

    Type variance is a design decision we have to make when we create a class with a type parameter, also known as a generic type. We have 3 options listed bellow:

    1. Non Variant type

    So this is the definition:

    At this point our GenericType has no type variance, which means we cannot substitute NonVariantType[String] with a NonVariantType[Any] or the other way around. This is demonstrated bellow:

    2. Covariant Type

    If we expect our generic type to work with subtypes of our type parameter in the same way as the type we instantiate it with, then we define our type as a covariant type:

    So with our new definition we repeat our example from before using our covariant type:

    3. Contravariant Type


  • Scala 2.10 and Akka 2.1.0

    January 12, 2013

    First I tried to modify the akka example from typesafe to work with the new scala and akka versions, you can find the modyfied project here (with eclipse plugin).

    Changes I made were to fix compilation due to classes moved from Akka into scala concurent (SIP-14) and some changes so all 8 cores of my system will be working for a little more.

    It was nice to see that all of them were on full capacity during the run, if you try it with 4 workers you can see that about 1/2 of the cpu capacity is not used.

    Also I find usefull to use eclipse with-source=true in sbt to download the sources, makes browsing the code in eclipse a brease.

     


  • Futures & Promises 2.10

    January 9, 2013

    Callbacks, the abiliby to reference a code fragment and pass it as argument to to another function is very popular and its used throught the application layers. I can be blocking or non blocking(used for IO, event processing). In scala async callbacks are done using futures and futures were implemented in many of the frameworks build ontop of scala.

    In scala 2.10 there is a new implementation of Futures and adds the implentation of Promises. 

    The goals of this new implemention are:

    1. To have a single implenation of this functionality (not one per framework).

    2. To make this implementaion efficient and composable.

    3. To take advantage of scala's power to make futures a more powerfull tool.


  • Logical Expression parser

    August 13, 2012

    Recently we I had a requirement to enhance the rule engine for our system to use a logical expression formulae to calculate result of several rules associated with a validation.

    Until that point all rules had to succeed so it was a straight forward AND between all rules, instead of trying to create a complecated user interface we decided to go with a formulae expression. This of course means that we have to validate during setup and ofcourse evaluate the result based on individual rule result at runtime.


  • Scala CASE classes

    July 26, 2012

    When you create a case class in scala say like

    case class Something (id: Int)

    you get the following from the compiler:

    • the apply method so you can write Something(4) instead of new Something(4) when you are creating your objects
    • the compiler adds the val since all the fields are immutable
    • you get a copy function to create modified copies of your object like so:   Something(6).copy(something = 7) and by using named parameters you can change only the value that changes.

    Benefits of case classes:


  • From CSV to objects with Scala's pattern matching

    March 7, 2012

    One or the primary reasons I liked scala so much is pattern matching. In combination with Case classes its an extremely powerful tool.

    Any software project includes some sort of migration. Most common way that data comes in is text files with some delimiters. So lets say we pick up a file that contains lines like the bellow:

    https://gist.github.com/1992324

    Here text fields are surrounded by ~ and all columns separated by ^

    So how do you import this into Scala?

    The idea is to create a small regular expression for each of the fields you expect to get. For example a string regular expression for the strings:

    val text = """~([^~]*)~"""


  • Lift snippet subpackages

    February 22, 2012

    After a while lift project becomes big enough and you will probably want to break your files into subpackages here's what you need to do:

    1. Add your default snippet package for example if your base snippet package is my.project.snippet then in your boot file add the line: LiftRules.addToPackages("my.project") 
    2. Create subpackages under your snippet package and put your scala code in there.
    3. In your html5 templates point to your snippets by using subpackage/SnippetName like the following: <div data-lift="subpackage/SnippetName"></div>

  • Viewing/Editing H2 database via web interface

    February 14, 2012

    I was just going through the messages in the lift web maillist and one of the posts was about the H2 dabase and how to view and edit the tables. I always wanted to have a look in there, but never bothered enough to figure it out. Thanks to Diego Medina for serving it on a plate!

    Find where you h2 jar file is and then run the following command:

    java -cp /path/to/h2/lib/h2-1.3.160.jar org.h2.tools.Server

    This should start the server and it open up the browser for you:

    Sellect generic h2 server from the first drop down then:

    jdbc:h2:/path/to/db/startingdb;AUTO_SERVER=TRUE