6.1-Writting Simple Lambdas Flashcards

1
Q

📝️What is Functional programming?

A

It’s a way to write code in a declarative way.

  • > So you specify what you want to do, rather than dealing with the state of objects.
  • > You focus more on expressions than loops
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

📝️How do we write in a Functional Programming style in Java?

A

By using Lambda Expressions …

aka Lambdas, Clousures

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

📝️What is a Lambda Expression?

A

A lambda expression is a concise way to implement the Single Abstract Method of a Functional Interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

📝️Why Lambdas? (4)

A
  1. Enable Functional Programming (Declarative instead of Imperative)
  2. Write a more comprehensible code
    - > This is more Readable and Concise)
  3. Easier to use API’s & Libraries
  4. Enables Parallel Programming
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

📝️How to write Lambdas (Syntax)?

A

param-list -> statement(s);

param-list ::

  • > 4 single-parameter (WITHOUT type) parentheses are optional.
  • > parameter-type is optional when using parameter *** Type is infered by context (Functional Interfaces SAM)

-> Arror-operator

statement(s) ::

  • > For single (return) statement curly braces are optional
  • > If return keyword present or multiple statements curly braces are mandatory.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

📝️ What are the predifined Functional Interfaces? (4)

A

Predicate -> boolean test(T t)
Function -> R apply(T t)
Consumer -> void accept(T t)
Supplier -> T get()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

📝️ How to implement a Functional Interface?

A
  • ** An interface can NOT be instantiated
    1. A concrete class that overrides the SAM method
    2. An Anonymous Class that overrides the SAM method
    3. A Lambda Expression
    4. Method Reference (A method with the same signature that is already implemented)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When a Lambda Expression can be used?

A

When implementing the SAM of a Functional Interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does a Lambda Expression infere the parameter types?

A

Because of the Context, a Lambda Expression implements a SAM from a Functional Interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly