Ubuntu insights, Programming in groovy, java, et als!

Monday, October 20, 2014

Simple way to implement find(select) and collect block closures in java

Languages like ruby, groovy, smalltalk have language constructs for blocks and closures. Java 8 now has feature to evaluate lambda expressions. But here is one simple and straight way to implement the same:

In groovy :

[1, 2, 3, 10, 20, 30].findAll { num -> num > 5 }
=> yields a new list [10, 20, 30]

[ 1, 2, 3, 4 ].collect{ num -> num * num}
=> yields a new list [1, 4, 9, 16]

The above code for those more familiar with the smalltalk syntax will be:

#(1 2 3 10 20 30) select: [ :num | num > 5 ]
==> displays [10, 20, 30]

#( 1 2 3 4 ) collect: [:i | i * i ].
==> displays [1, 4, 9, 16]

In java, the same could be implemented with a set of abstract classes as a collection utility that mandate implementation of method which returns the boolean condition to add or transform the element under iteration :

Find.java
abstract class Find extends ArrayList{ 
    /** let the default constructor worry about iteration **/
    public Find(List list){    
        Iterator it = list.iterator();
        while(it.hasNext()){
            Object element = it.next();        
            if(all(element))           
                this.add(element);         
        }
    }
     
    /** This defines the boolean condition for find/select **/
    public abstract Boolean all(Object obj);
     
}

FindDemo.java
// find all elements in the list that are greater than 5
List list = Arrays.asList(1, 2, 3, 10, 20, 30);
selected = new Find(list) {
    public Boolean all(Object num) {               
        return (int) num > 5;
    }
};
// => yields a new list [10, 20, 30]

Collect.java
abstract class Collect extends ArrayList{  
    /** let the default constructor worry about iteration **/
    public Collect(List list){
        Iterator it = list.iterator();
        while(it.hasNext()){
            Object element = it.next();
            Object transformed = transform(element);
            this.add(transformed);
        }
    }
    /** This defines the transformation logic for collect **/
    public abstract Object transform(Object obj);
}

CollectDemo.java
// collect the squares of all elements in the list
List list = Arrays.asList(1, 2, 3, 4);
collected = new Collect(list) {    
    public Object transform(Object num) {
            return (int) num * (int) num;
    }
};
// => yields a new list [1, 4, 9, 16]

Saturday, October 11, 2014

Gauss Circle Problem

Can do better without two loops :-|


latticePoints = 0
radius = 3.2
mod = int(radius) + 1

def isInCircle(x, y):
    return x*x + y*y <= radius*radius

def calc(x, y):
    global latticePoints
    if x == 0 and y == 0:
        latticePoints = latticePoints + 1
        print((x, y))
    elif x == 0 and y != 0:
        latticePoints = latticePoints + 2
        print((x, y),(x, -y))      
    elif x != 0 and y == 0:
        latticePoints = latticePoints + 2
        print((x, y),(-x, y))      
    else :
        latticePoints = latticePoints + 4
        print((x, y), (-x, -y), (x, -y), (-x, y))          

for i in range(mod) :
    for j in range(mod) :
        if(isInCircle(i,j)):
            calc(i,j)
 
print(latticePoints)