Thursday, March 21, 2013

lambda expression



  • Anonymous function

In computer programming, an anonymous function (also function constant, function literal, or lambda function) is a function (or a subroutine) defined, and possibly called, without being bound to an identifier.
Anonymous functions are convenient to pass as an argument to a higher-order function and are ubiquitous in languages with first-class functions such as Haskell.
Anonymous functions are a form of nested function, in that they allow access to the variable in the scope of the containing function (non-local variables).
Unlike named nested functions, they cannot be recursive without the assistance of a fixpoint operator (also known as an anonymous fixpoint or anonymous recursion).
http://en.wikipedia.org/wiki/Anonymous_function



  • Lambda calculus 

Lambda calculus  is a formal system in mathematical logic and computer science for expressing computation by way of variable binding and substitution
http://en.wikipedia.org/wiki/Lambda_calculus



  • What is a lambda expression?

Lambda is an operator used to denote anonymous functions
For more information on Lambda expressions in Java 8 check out the JSR-335
My First Java 8 Lambda
It’s getting late, I have downloaded the Java 8 JDK preview, written three pages of fluff on Lambda expressions but have yet to write one. So here goes, you can download Java 8 from Oracle at http://jdk8.java.net/lambda/.
http://diarmuidmoloney.wordpress.com/2011/12/10/my-first-java-8-lambda-expression/



  • Java 8 - Closures, Lambda Expressions Demystified 

http://frankhinkel.blogspot.com/2012/11/java-8-closures-lambda-expressions.html

2 comments:

  1. Simplest lambda expression could be

    ()-> system.out.println("expression");

    () is argument . Here we are passing no argument . So if there is no argument () is the way to define empty argument

    system.out.println("expression") is expression . Expression is nothing but the body of the lambda expression. Here arguments can be used in various body statements

    For example

    (a,b)->a+b;

    (a,b) is argument here . a+b is expression.


    lambda expression are also called anonymous methods. Lambda expressions follow scala type of syntax .

    Why do we need these?

    These expression add functional edge to the language. Let's try to understand that with an example

    Before that let me make an statement -

    An interface having only one method is called functional interface.

    You might have written anonymous class using Runnable ,ActionListener interface . For example :



    Public class Engine {

    public static void main(String args[]){

    new Thread(new Runnable(){
    public void run()
    {
    int count=10
    while (count>0){

    System.out.println(count);
    COUNT--;
    }
    }).start();

    }

    in above code we are using Runnable interface an anonymous class.

    How to replace anonymous class code with lambda expression ?

    Read further here

    http://efectivejava.blogspot.in/2013/08/lambda-expression-in-java-8.html

    ReplyDelete