Functional Programming in Java

Functional Programming brings new and interesting paradigms to code development. Several of those languages (Scala, Clojure) run on top of a Java VM, but Java itself is NOT as functional programming language..., how is that possible, there must be a way!
One of the aspects of the functional programming languages is this possibility to use a pointer on a function as a parameter. Here is a possible approach, even easier to access since the diamond notation has been implemented in the recent versions of the Java language.
We are going to use a simple example as an illustration. We will choose the function to execute, based on the value of a parameter. What we want to do here is to greet someone in the language passed as a parameter.
Let us consider a first Java interface defined as follow:
package functions;

public interface FunctionOneArgReturnVoid<SomeType>
{
  public void execute(SomeType a);
}
And let us write the following implementation:
package functions;

public class ImplementationSampleOne
{
  FunctionOneArgReturnVoid<String> funcEnglish = 
                     new FunctionOneArgReturnVoid<String>()
   {
     public void execute(String s)
     {
       System.out.println("Hello " + s + "!");
     }
   };
  FunctionOneArgReturnVoid<String> funcFrench  = 
                     new FunctionOneArgReturnVoid<String>()
   {
     public void execute(String s)
     {
       System.out.println("Bonjour " + s + " !");
     }
   };

  public FunctionOneArgReturnVoid<String> getFunction(String lng)
  {   
    if ("FR".equalsIgnoreCase(lng))
      return funcFrench;
    else
      return funcEnglish;
  }

  public void execute(FunctionOneArgReturnVoid<String> func, String arg)
  {
    func.execute(arg); 
  }

  public static void main(String[] args)
  {
    String lng = "EN";
    if (args.length > 0)
      lng = args[0];

    ImplementationSampleOne impl = new ImplementationSampleOne();
    impl.execute(impl.getFunction(lng), "Joe Shmow");
  }
}
See the two implementations of the FunctionOneArgReturnVoid interface, and the way they are invoked in the main method.
The execution would look like this:
Prompt> java functions.ImplementationSampleOne "FR"
Bonjour Joe Shmow !

Prompt> java functions.ImplementationSampleOne "EN"
Hello Joe Shmow!
The return type can be also described by an interface:
package functions;

public interface FunctionTwoArgsReturnType<SomeTypeOne, 
                                           SomeTypeTwo, 
                                           ReturnType>
{
  public ReturnType execute(SomeTypeOne a, SomeTypeTwo b);
}
The interface implementation would look like this:
  FunctionTwoArgsReturnType<String, String, String> funcEnglish = 
                 new FunctionTwoArgsReturnType<String, String, String>()
   {
     public String execute(String a, String b)
     {
       return "Hello " + a + ", from " + b + "!";
     }
   };
  FunctionTwoArgsReturnType<String, String, String> funcFrench  = 
                 new FunctionTwoArgsReturnType<String, String, String>()
   {
     public String execute(String a, String b)
     {
       String s = "Bonjour " + a + ", de la part ";
       if (b.toUpperCase().toCharArray()[0] == 'A' ||
           b.toUpperCase().toCharArray()[0] == 'E' ||
           b.toUpperCase().toCharArray()[0] == 'I' ||
           b.toUpperCase().toCharArray()[0] == 'O' ||
           b.toUpperCase().toCharArray()[0] == 'U' ||
           b.toUpperCase().toCharArray()[0] == 'Y')
         s += ("d'" + b);
       else
         s += ("de " + b);
       s += " !";
       return s;
     }
   };
The main like that:
  public static void main(String[] args)
  {
    String lng = "EN";
    if (args.length > 0)
      lng = args[0];

    ImplementationSampleTwo impl = new ImplementationSampleTwo();
    String greeting = impl.execute(impl.getFunction(lng), "Joe Shmow", "Olivier");
    System.out.println(greeting);
    greeting = impl.execute(impl.getFunction(lng), "Joe Shmow", "Mr X");
    System.out.println(greeting);
  }
This possibility of using pointers on functions does not make Java a functional programming language, but this is a first step towards it.
More to come...
Comments:

Does this code will run on prior JDK7 ?

Posted by guest on December 16, 2011 at 11:20 PM PST #

All this code needs to run is the diamond notation and wildcards support, which is in Java since 1.5, I think.

Posted by olediouris on January 05, 2012 at 07:48 AM PST #

This will work in jdk 1.7 version

Posted by guest on March 11, 2012 at 11:14 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About


This is the blog for the Oracle FMW Architects team fondly known as the A-Team. The A-Team is the central, technical, outbound team as part of the FMW Development organization working with Oracle's largest and most important customers. We support Oracle Sales, Consulting and Support when deep technical and architectural help is needed from Oracle Development.
Primarily this blog is tailored for SOA issues (BPEL, OSB, BPM, Adapters, CEP, B2B, JCAP)that are encountered by our team. Expect real solutions to customer problems, encountered during customer engagements.
We will highlight best practices, workarounds, architectural discussions, and discuss topics that are relevant in the SOA technical space today.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today