Functional Language Aspects
There’s been an interesting influx of functional programming aspects into mainstream object-oriented languages such as C# and Java lately. Of course languages such as Groovy and Ruby have been featuring these language elements for years. Let’s look at a few of these features:
Closures
Closures are parameterized anonymous functions which are bound to variables and evaluated in a dynamic context. This construct is also referred to as lambda functions (e.g. C#, Ruby, Lisp). .NET introduced Lambda expressions with .NET 3.5, Java will most likely introduce them with JDK 1.7. I find them very useful, but a little hard to debug sometimes. Some examples:
Java
{int=>int} addOne = {int x => x+1};
C#
Func<int,int> addOne = d => d + 1; // or var addOne = d => d +1;
Type Inference
Let’s look at this C# example:
var x = 5; object y = 6; x = "foobar"; // leads to error as x's type is int y = "foobar"; // works just fine
The type of x is inferred upon assignment (note that C# still employs static typing, however).
Java 7 will introduce type inference as well, even though it seems to be mostly constructor type inference to simplify syntax:
Map<String, List<String>> anagrams = new HashMap<String, List<String>>();
// can be written as
Map<String, List<String>> anagrams = new HashMap<>();
Fun Links
C# is a functional language: http://sneezy.cs.nott.ac.uk/fun/nov-06/FunPm.ppt
JDK 1.7 Features http://tech.puredanger.com/java7/
Type Inference http://en.wikipedia.org/wiki/Type_inference
Closures http://en.wikipedia.org/wiki/Closures
Comments
Leave a Reply
You must be logged in to post a comment.






