Displaying date and time in localized date pattern in Liferay (JSF)

2013/01/20 § Leave a comment

Different countries have different date and time formats, in USA it is represented as dd-mm-yyyy, in Sweden dates are official written in the format yyyy-mm-dd (you can read more about it in the following link http://en.wikipedia.org/wiki/Category:Date_and_time_representation_by_country). When developing a web application it can be common requirement that we want to display date and time in users localized pattern so that users won’t be confused. So here is the small trick how you can read users localized date pattern in JSF application in portlet.

Commonly in the JSF we can read the users local by calling following method

Locale browserLocale = FacesContext.getCurrentInstance().getViewRoot().getLocale();

but this also requires mapping of all the locales you want to support in faces-context.xml, if you know what locales you need to support then this will do the work, otherwise here is a small trick that will read the locale of user in Liferay and using that we can get the user’s localized date pattern.

public String readUsersDatepattern() {

HttpServletRequest req = (HttpServletRequest)(FacesContext

Locale locale = req.getLocale();
 SimpleDateFormat dateFormat = (SimpleDateFormat) SimpleDateFormat
 .getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.SHORT, locale);

return dateFormat.toPattern();


and in the jsf page you can display is as

<h:outputText value="bean.timestamp">
<f:convertDateTime type="both" pattern="#{bean.datePattern()}" />

hope this comes handy


Best Resources for learning JavaScript in depth (for free)

2012/01/24 § Leave a comment

Here are some of the links available freely on web which I found very useful for understanding the JavaScript in detail.


Of-course there are other many great resources, I will update it as I come across other materials. Hope you also find it useful.

JSF how to iterate over map using ui:repeat

2011/07/07 § 3 Comments

private Map<String, ArrayList<Person>> myMap = new TreeMap<String, ArrayList<Person>>();

This is how my map look like and i want to display both key and value pair on my html page. ui:repeat doesn’t handle the Sets or Maps by default so here is the work around how we can make it work.

Reguar getter seter Person Class

public class Person {
private String name = "";

private String gender = "";
private String address = "";

public Person(String name, String gender, String address) {
this.name = name;
this.gender = gender;
this.address = address;

Make a function that returns only the keys of map

private ArrayList<String> keys = new ArrayList<String>();
private void myKeys(){
for(String key: blocks.keySet()){

Call the Map on jsf page as follows,

<ui:repeat value="#{mapBean.keys}" var="key">
<ice:outputLabel value="#{key}"/>
<ui:repeat var="item" value="#{mapBean.myMap[key]}">
<ice:outputLabel value="#{item.name}"/>
<ice:outputLabel value="#{item.adress}"/>

Lambda expressions in Python

2011/04/02 § 1 Comment

Python supports lambda expressions[1], having lambda support in a language is very nice feature, specially if you like functional style of syntax you will love it. Its not just a choice of style of programming sometimes lambda expressions helps to solve some problem quite elegantly, it helps to make code clean readable and free from unwanted functions which could have been easily replaced with lambda functions. Because of all these advantages i really love the lambda capability in python, here i will try to discuss some of the situations where lambda expressions could be helpful and explain the different style of writing lambda functions.

Though pythons lambda capability is not strong as many functional languages as it only offers lambdas which with expressions, not statements[4] but if we consider this feature carefully it is sufficient for most of the problems. Lambda functions doesn’t have name and they are called from where they are defined so they are also called anonymous functions, in python they are called by keyword lambda and their sysntatic structure looks like this lambda arguments: expression

so lets see how a simple lambda function look like

>>> func = lambda x : x*3
>>> func(3)

this is a very simple lambda function where given any value that value, it is multiplied by 3, so when 3 is passed in function func it returns 9. This demonstrate how a simple lambda function can be defined but this function might have very few places where it can find its use so lets see some other functions. One situations where lambda fist perfectly is in filters and maps, let say we have a list of numbers and we want to filter out only the odd numbers, for that we can do something like this

>>> print filter(lambda x : x%2 != 0, range(10))
[1, 3, 5, 7, 9]

here lambda provide condition to filter all unwanted numbers, similarly we can get similar use of lambda in map.

Up to now it looks simple but what if we need to give some condition on the lambda expression, our needs can’t always be that simple as shown in the examples above, say we have a lambda function that takes two values and if the first value is greater than second it does subtract first from second otherwise returns zero, we can express such case as follows

>>> lambda_check = lambda x, y : x - y if (x > y) else 0
>>> lambda_check(17, 7)

# or alternatively it can be written as

>>> lambda_check = lambda x, y : (x > y and x - y ) or 0
>>> lambda_check(17, 7)

the syntax looks self explanatory, the alternative syntax looks bit odd but it depends on and or execution strategy. If-else condition works quite well, now lets  look at more complex condition, let say we have 2 values if first value is bigger, second is subtracted from first, if second is bigger first is subtracted from second and if both are equal they are added if all fails returns zero. To express this condition we sould take the alternative suntax above and it can be expressed as follows

lambda_check = lambda x, y : (x > y and x - y) or \
(y > x and  y - x) or \
(x == y and  x + y) or 0
>>> lambda_check(17, 7)

This syntax looks bit complex but if we look carefully we just make the use of and and or to create this expression. We can use same techniques for different datatypes, conditions or even you can call functions.

if you want to read more about lambda expressions in python you can follow the links below.

[1] http://docs.python.org/reference/expressions.html#lambda
[2] http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/
[3] http://diveintopython.org/power_of_introspection/lambda_functions.html
[4] http://lambda-the-ultimate.org/node/1480

Google translate, accuracy and languages that will never make it to google translate

2011/01/30 § Leave a comment

First of all I must say am  a big fan of Google translate and I use it almost everyday, being in the foreign land where i can’t understand the local language, life would have been much harder if there had not been Google translate. And i suppose there the thousands of people with similar situation. Apart from that for the thousands of articles published in different languages, if your friends tweet or Facebook in diff languages or if you are in strange place or store you don’t understand any thing for all these problems there is single stop Google translate. Yes Google translate is really great service with support for 52 languages you can lower your pressure if you are visiting new pace you don’t understand local language or if want to get gist of the article published in another language.

What about the accuracy of the service, can it be used for the professional services, can it ever be in the state that we can use it for translation of  text books or poems ?  probably not. Before telling any thing about the service we should know how it works. Google uses really intelligent technology instead of manually giving all the  the translation rule of the languages to the system Google feeds enormous amount of human translated text to the system so system analyses these huge amount of translated texts and try to get significant patterns for translation, once system find these meaningful patterns on these human translated texts later it uses same patterns to translate the text.

So basically, accuracy of the system depends on the amount of human translated text provided to the system. More the translation pattern system can recognise more accurate the system gets. What if the language doesn’t have enough translated text, in that case Google translation system can’t learn enough translation rules/patterns that is required for the accuracy of the translation as a result you will get very poor translations. And the worst if some languages have very few translated texts then you may never see that language in the Google translate.

What’s the solution if we want to make translation system for less used languages, Solution is to manually define the translation rules of the language to the system. We can build a system where translation rules of the languages can be define manually. I think the framework like Grammatical Framework are very useful tools for building rule based systems. Given enough research and time we may even get the system more accurate than Google translate but as of the current stare of Machine translation techniques it very hard to get the same accuracy of translation as humans do. This is because unless the machine translation can pick up on the cultural undertones and subtleties at play in language we can’t get the same level of richness in the translation and i think these things are still very far from the current state of Machine Translation technology.

More at:
How Google Translate Works
Google Translate and the Struggle for Accurate Machine Translations
Can Google break the computer language barrier?

Useful Links for Primitive Recursive functions

2011/01/14 § Leave a comment

The primitive recursive functions are defined using primitive recursion and composition as central operations, they are defined inductively. We define some base functions and some operations to build up new functions from old ones. There are lots of resources available online here are some of the links i find useful, hope you find interesting too.

  1. http://www.logicmatters.net/resources/pdfs/gwt/GWT05.pdf
    http://www.logicmatters.net is really wonderful site and there are series of notes on the title “Godel Without (Too Many) Tears” http://www.logicmatters.net/resources/pdfs/gwt/, i really like this site and PRF (Primitive Recursive function) tutorial is great.
  2. http://www.eecs.qmul.ac.uk/~bellin/rec08.pdf
  3. http://www.cs.mcgill.ca/~prakash/Courses/comp330/Notes/prim_rec_notes.pdf – lecture note from McGill University
  4. https://sites.google.com/site/modelsofcomputation/Home/litterature/prf.pdf – Lecture notes by Bengt Nordström
  5. http://www.cs.cmu.edu/~cdm/pdf/PrimRec-6up.pdf – Lecture slide CMU

Lambda Calculus

2010/11/14 § Leave a comment

I found λ-calculus very much interesting and important language in aspect of studying the theoretical properties of the programming languages and as it also forms the basic building blocks of the functional languages, so here i want to present a basic introduction to lambda calculus and some of its basic operations. Please don’t forget to check the links sections below if you want to study it in details, there are lots of good tutorials available in web have tried to index few of them.

Why lambda calculus ?
The λ-calculus can be called as the smallest universal programming language of the world. Good thing about lambda calculus it that it just consists of a single transformation rule (variable substitution) and a single function definition scheme and still we can represent very complex structure and operations with it. Lambda calculus was introduced by Alonzo Church in the 1930 as part of an investigation into the foundations of mathematics and part of which was found to be relevant to the computation so we use it to study theory of programming languages. Lambda calculus can be seen as equivalent to Turing machine but the main difference is that λ-calculus mainly depends on the transformation rules and does not care about the actual machine implementing it on the other hand Turing machine heavily depends on it so it makes λ-calculus more suitable for the study of programming properties without worrying about the hardware implementing it

lambda expressions are composed of
Variables, Abstraction and applications which can be represented as
e ::= x | ?x.e | (e1 e2)
or recursively we can describe as follows as follows:
<expression> ::= <name> | <function> | <application>
<function>    ::= ? <name>.<expression>
<application> ::= <expression><expression>
The only keywords used in the language are ? and the dot. In order to avoid cluttering expressions with parenthesis, we adopt the convention that function application associates from the left i.e f x y means (f(x))(y) and lambda abstraction extends far to the right as possible. For example ?x.xy means ?x.(xy) rather than (\x.x)y.

lambda expressions are composed of
variables, Abstraction and applications which can be represented as

e ::= x | λx.e | (e1 e2)

or recursively we can describe as follows as follows:

<expression>  ::= <name> | <function> | <application>
<function>    ::=  λ <name>.<expression>
<application> ::= <expression><expression>

The only keywords used in the language are λ and the dot. In order to avoid cluttering expressions with parenthesis, we adopt the convention that function application associates from the left i.e f x y means (f(x))(y) and lambda abstraction extends far to the right as possible. For example λx.xy means λx.(xy) rather than (λx.x)y.

An example of a function can be something like λx.x, which is a identity function. The name after the λ is the identifier of the argument of this function. The expression after the point (in this case a single x) is called the “body” of the definition. Functions can be applied to expressions. An example of an application is (λx.x)y This is the identity function applied to y.

The names of the arguments in function definitions do not carry any meaning by themselves. They are just “place holders”.
Therefore (λx.x)  (λy.y)  (λz.z)

Free and Bound variables
If we have functions like λx.y x then we say that x is bound and y is free variable, one important thing to remember is that a variable binds to its nearest lambda, so if we have expression like (λx.y x) x the outer occurrence of x is free because there is no occurrence of λ to bind that variable.

Understanding substation is little-bit difficult at first as we don’t have any name for our functions and we can perform different kinds of reduction to evaluate our expression.Please go through the Wikipedia [http://en.wikipedia.org/wiki/Lambda_calculus#Reduction] or Chapter 2 of this tutorial [ftp://ftp.cs.kun.nl/pub/CompMath.Found/lambda.pdf]

Programming with Lambda calculus
Natural Numbers
By using some base type which can’t be reduced further than that say ‘0’ or [] , and a successor function we can define complete set of natural numbers in church encoding, here is how we can define church numerals

0 := λfz.z

1 := λfz.f z

2 := λfz.f (f z)

3 := λfz.f (f (f z))


n := λfz. f^n z

similarly successor function can be defined as

Succ = λnfz.f (n f z)

Addition of two numbers m and n can be defined as the m-th composition of f composed with the n-th composition of f, or it can also be defined using the successor function we define above, since adding m to n can be seen as 1 m times to n, so

Plus ::= λnfz.f (n f z)
Plus ::= λmn.m Succ n

similarly multiplication can be defined as

Mult := λmnf.m (n f)
Mult := λmn.m (PLUS n) 0

Predecessor function is bit tricky, as we are dealing with the natural numbers here we can define pred. as if n = 0, then pred 0 = 0, else pred n = n – 1

Pred := λn f x . n (λg h . h (g  f)) (λu . x) (λu . u)

Predecessor can also be defined using pair function,
let Pair(x, y) be a pair, fst return the first element and snd returns the second element of pair, and let Si = λp. pair (snd p) (succ (snd p)) , its a function generates from the pair (n-1, n) or alternatively we can say is a shift and increment function, given a pair (m,n) the function ss maps it to (n, n+1) so given this function we can define Pred as

Pred := λn.fst (n Si  (Pair 0 0)).

which applies function Si n times to (Pair 0 0) and selects the first member of the pair and this approach gives prd of zero as zero.

Boolean and Conditionals
True and False is defined in church numerals as follows

True := λ x y . x

Frue := λ x y . y

The definition of true and false can simply be seen as The true function takes two arguments and returns the first one, and the false function returns the second of two arguments. Given those definitions we can easily define the conditional expressions as follows.

Given those definitions we can easily define the conditional expressions as follows

if true then e1 else e2 = true e1 e2
= (\x y .x ) e1 e2
= e1


if false then e1 else e2 = false e1 e2
= (\x y .y ) e1 e2
= e2

once we have cnditional we can define all other logical operations as

not p = if p then false else true

p and q = if p then q else false

p or q  = if p then true else q


Defining recursion in lambda calculus is bit tricky, Recursion is the definition of a function using the function itself but as all the lambda expressions are name less so we can define the recursion as we are used to define it in the programming languages that supports recursion so we use the help of  fixed point combinators,

Y = λg.(λx.g (x x)) (λx.g (x x))

When any function is applied to the Fixed point combiner it simply calls a function and recreates it self

so if we apply g to Y it simply expands to g (Y g) i.e. Y g = g (Y g)

Please go through the Section 4 Recursion [www.inf.fu-berlin.de/lehre/WS03/alpi/lambda.pdf] of this paper or page two of this tutorial also has one nice example of how Fixed point is applied to recursive function in lambda calculus.

Similarly we can define other complex structures like tree, list etc using the pair.

So lambda calculus is very simple yet powerful and is a Turing-Complete and the lambda-calculus along of the “halting-problem” is unsolvable these properties make it as a perfect system for the theoretical study of programming languages.

Here are some of the resources to lambda calculus that can be freely available in web

[1] http://en.wikipedia.org/wiki/Lambda_calculus
[2] John Harrrison – Lambda calculus: [http://www.cl.cam.ac.uk/teaching/Lectures/funprog-jrh-1996/chapter2.ps.gz]
[3] John Harrrison – Lambda calculus as a programming language: [http://www.cl.cam.ac.uk/teaching/Lectures/funprog-jrh-1996/chapter3.ps.gz]
[4] For solving your lambda expressions and checking reductions http://ellemose.dina.kvl.dk/~sestoft/lamreduce/lamframes.html
[5] A Tutorial Introduction to the Lambda Calculus – Ra´l Rojas [www.inf.fu-berlin.de/lehre/WS03/alpi/lambda.pdf]
[6] Church Numerals – http://www.cs.rice.edu/~javaplt/311/Readings/supplemental.pdf
[7] Introduction to Lambda Calculus – [ftp://ftp.cs.kun.nl/pub/CompMath.Found/lambda.pdf]
[8] Peter Selinger – Lecture Notes on the Lambda Calculus [http://www.mathstat.dal.ca/~selinger/papers/lambdanotes.pdf]