Shortlink

Ripple for mobile web applications

The good aspect of meeting a lot of people every week is learning a lot of things, some of which may lead to improved productivity. My recent client visit got me introduced to a Chrome add-on called Ripple.

We’re working with Sencha Touch and executing them in chrome. The guys never got a feel of working with a mobile application as they’re testing it in the chrome browser. I personally use a tool called IBBDemo2 that’s a flash application. It acts like a iPad/iPhone simulator. Due to security reasons flash is not allowed in this client machines. One of the guys’ suggested we try Ripple.

Ripple is a Blackberry emulator but it supports multiple devices and platforms as well. After installing Ripple with chrome browser, you can develop an application using Sencha or any other library and run it in Ripple Emulator.
Given below are some screenshots of the page when viewed in normal chrome window and one that is run in the Ripple Emulator.


Now that my chrome has Ripple installed, looks like it’s going to be in my favorite tools listfor sometime till I find a new and better one.

Shortlink

C# – Revisiting Lambda expressions – II

In the previous post, we saw that the Lambda expressions can be used as a shortcut to function pointers. A method that accepts a lambda expression as a parameter is equivalent to a method that accepts a function as a parameter. Let’s understand this with an example.

We’ll create a delegate that checks if the given number is an even number or not. We’ll instantiate this delegate and use it.

class LambdaTest{
        delegate bool IsEven(int num);
        static void Main(string[] args) {
            IsEven isEvenObj = num => num % 2 == 0;
            Console.WriteLine(isEvenObj(131));
       }
}

We’ve defined an IsEven delegate and instantiated it and used it. IsEven is just compiled to a class that inherits System.MultiCastDelegate. Instead of defining a delegate that accepts an integer and returns a boolean, we can use a simple built-in delegate called Func for this purpose. Let’s rewrite the code using Func<>

class LambdaTest{
        static void Main(string[] args) {
            Func<int, bool> isEven = num => num % 2 == 0;
            Console.WriteLine(isEven(131));      
        }
}

Func<> is a built-in delegate class in C#. It accepts nearly 16 generic parameters, the last parameter being the return value. In our example Func<int,bool> indicates it accepts an integer and returns a boolean. Similar to Func<> is the Action<> delegate as well. It just doesn’t return any value. The ForEach methods in collection classes accept the Action<> delegate as parameter.

class LambdaTest{
        static void Main(string[] args) {
            List<string> langs = new List<string>() { "C#", "Java","Ruby","Groovy" };
            Action<string> action = s => Console.WriteLine(s);
            langs.ForEach(action);
        }
}

Therefore we just need to understand that the lambda expressions are nothing but objects of delegate classes, with a crisp syntax.

Shortlink

C#- Revisiting Lambda Expressions

Though plenty of articles have been written about Lambda expressions in C#, still feel most of the developers continue to build .NET applications using C# 1.0. I had an opportunity to discuss lambda expressions with a group of developers.

We created a simple list of string values and wanted to print the strings that end with letter ‘y’. We discussed various ways to implement this. We started off with the traditional approach of using for-each loop and moved on to use lambda expressions. Given below is the code that we wrote.

//Approach - I
 List<string> langs = new List<string>() { "Java","C#","Ruby","Groovy","Scala"};
 foreach (string lang in langs)
 {
      if(lang.EndsWith("y"))
          Console.WriteLine(lang);
 }
//Approach - II
bool FindLanguages(string lang)
{
   return lang.EndsWith("y");
}
List<string> langs = new List<string>() { "Java","C#","Ruby","Groovy","Scala"};
foreach (string lang in langs)
{
     if (FindLanguages(lang))
         Console.WriteLine(lang);
}
//Approach - III
bool FindLanguages(string lang)
{
   return lang.EndsWith("y");
}
List<string> langs = new List<string>() { "Java","C#","Ruby","Groovy","Scala"};
var result = langs.FindAll(FindLanguages);
foreach (var item in result)
{
    Console.WriteLine(item);
}
//Approach - IV
List<string> langs = new List<string>() { "Java","C#","Ruby","Groovy","Scala"};
Predicate<string> FindLanguages = lang => lang.EndsWith("y");
var result = langs.FindAll(FindLanguages);
foreach (var item in result)
{
    Console.WriteLine(item);
}
//Approach - V
List<string> langs = new List<string>() { "Java","C#","Ruby","Groovy","Scala"};
var result = langs.Where(lang=>lang.EndsWith("y"));
foreach (var item in result)
{
    Console.WriteLine(item);
}

The last two approaches (IV and V) show the use of lambda expressions.

Shortlink

Groovy-Closure with variable number of arguments

Last week somebody tossed up a question of using variable number of arguments with Groovy. We’re implementing a method called ‘add‘ in the String class using meta-programming. The add method should just append any number of string arguments passed to it.

Given below is the simple code that takes in variable number of arguments.

String.metaClass.add = {String... args ->
	args.inject(delegate) {result,item->"${result}${item}"}
}

println "java".add(" is"," old")
println "Groovy ".add(" is ","agile ","and ","cool")

The add method uses the ellipsis mechanism of passing variable number of arguments. The ‘args’ argument is a list that uses the inject method to append the string values passed it.

Shortlink

Groovy/Java ArrayList code comparison

Given below is the code to create a simple ArrayList in Java and it’s equivalent in Groovy.

//Code in Java
ArrayList<String> countries = new ArrayList<String>();
countries.add("India");
countries.add("France");
countries.add("Spain");
System.out.println(countries.getClass().getName());//Prints java.util.ArrayList
for(int i=0;i<countries.size();i++){
	System.out.println(countries.get(i));
}

The Groovy equivalent of the same.

 
//Code in Groovy
countries = []
countries << "India"
countries << "Spain"
countries << "France"
println countries.class.name //Prints java.util.ArrayList
countries.each {println it}