Functional Programming and Lambda Expressions Flashcards
What are lambda expressions?
Expressions that use behaviour/methods as parameters.
How can lambdas be declared?
anonymously (numbers.forEach(n -> System.out.println(n));) or explicitly (Consumer<Integer> printInts = i -> System.out.println(i); numbers.forEach(printInts);)</Integer>
How do anonymously declared lambdas look like?
parameter -> expression ||
parameter -> {block} ||
(parameter1, parameter2) -> expression ||
(parameter1, parameter2) -> { block }
What are similar to lambda expressions and why?
Static method references, because they allow class methods to be passed as parameters.(numbers.forEach(Printer::printInts); or Printer p = new Printer();
numbers.forEach(p::printInts);)
Why is FunProg used in Java?
- centered around chaining functions
- helps with lazy evaluation and parallelization
What are some use cases of FunProg in Java?
- distributed computing
- data processing
What are the main elements of functional programming in Java?
Functional programming in java takes advantage of the Stream API to allow sequential or parallel chaining of functions, which are evaluated lazily. Also, lambda expressions or methods references are often used to pass operations as parameters.
(Pure functions - they have no sides effects, immutability - do not modify input, chaining functions, recursion- actions are broadcast throughout the chain)