functional programming - Lambda Expression

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangbingfengf98/article/details/86547878
  1. Lambda expressions produce functions, not classes. On the Java Virtual Machine (JVM), everything is a class, so there are various manipulations performed behind the scenes that make lambdas look like functions—but as a programmer, we can happily pretend they are “just functions.”
  2. The lambda syntax is as spare as possible, precisely to make lambdas easy to write and use.

For examples:

one lambda expression - Strategize.java;

another example:

// functional/LambdaExpressions.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

interface Description {
  String brief();
}

interface Body {
  String detailed(String head);
}

interface Multi {
  String twoArg(String head, Double d);
}

public class LambdaExpressions {

  static Body bod = h -> h + " No Parens!"; // [1] syntax // the arguments

  static Body bod2 = (h) -> h + " More details"; // [2] syntax // followed by the ->, which you might choose to read as "produces."

  static Description desc = () -> "Short info"; // [3] syntax // everything after the -> is the method body.

  static Multi mult = (h, n) -> h + n; // [4]  syntax // for more than one argument, place them in a parenthesized argument list.

  static Description moreLines =
      () -> { // [5] syntax // if we do need multiple lines in your lambda expression, you must put those lines inside curly braces.
        System.out.println("moreLines()");
        return "from moreLines()";
      };

  public static void main(String[] args) {
    System.out.println(bod.detailed("Oh!"));
    System.out.println(bod2.detailed("Hi!"));
    System.out.println(desc.brief());
    System.out.println(mult.twoArg("Pi! ", 3.14159));
    System.out.println(moreLines.brief());
  }
}
/* Output:
Oh! No Parens!
Hi! More details
Short info
Pi! 3.14159
moreLines()
from moreLines()
*/

Recursion Lambda Expression:

Integer factorial

// functional/IntCall.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

interface IntCall {
  int call(int arg);
}
// functional/RecursiveFactorial.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

public class RecursiveFactorial {
  static IntCall fact;

  public static void main(String[] args) {
    fact = n -> n == 0 ? 1 : n * fact.call(n - 1);
    for (int i = 0; i <= 10; i++) {
      System.out.println(fact.call(i));
    }
  }
}
/* Output:
1
1
2
6
24
120
720
5040
40320
362880
3628800
*/

Fibonacci sequence

// functional/RecursiveFibonacci.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

public class RecursiveFibonacci {
  IntCall fib;

  RecursiveFibonacci() {
    fib = n -> n == 0 ? 0 : n == 1 ? 1 : fib.call(n - 1) + fib.call(n - 2);
  }

  int fibonacci(int n) {
    return fib.call(n);
  }

  public static void main(String[] args) {
    RecursiveFibonacci rf = new RecursiveFibonacci();
    for (int i = 0; i <= 10; i++) {
      System.out.println(rf.fibonacci(i));
    }
  }
}
/* Output:
0
1
1
2
3
5
8
13
21
34
55
*/

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/functional/LambdaExpressions.java

3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/functional/IntCall.java

4. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/functional/RecursiveFactorial.java

5. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/functional/RecursiveFibonacci.java 

猜你喜欢

转载自blog.csdn.net/wangbingfengf98/article/details/86547878