4 Lambdas Flashcards

1
Q

What is a lambda expression

A

Functional programming uses lambda expressions to write code. A lambda expression
is a block of code that gets passed around. You can think of a lambda expression as an anonymous method. It has parameters and a body just like full-fl edged methods do, but it doesn’t have a name like a real method.

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

What is functional programming?

A

In Java 8, the language added the ability to write code using another style. Functional programming
is a way of writing code more declaratively. You specify what you want to do rather than dealing with the state of objects. You focus more on expressions than loops.
Functional programming uses lambda expressions to write code.

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

What are the special rules for writing lambdas?

A

1-The parentheses can only be omitted if there is a single parameter and its type is not explicitly stated.
2-Braces can be omitted when we only have a single statement.
3-Return & semicolon can be omitted when braces are not used.
4- Return and semicolon ARE REQUIRED when two or more statements.
5- Use {} to create blocks of code elsewhere.

  • Remember that the parentheses
    are only optional when there is one parameter and it doesn’t have a type declared.

Shortcut: a -> a.canHop()
Full syntax: (Animal a) -> { return a.canHop(); }

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

What is a “marker” interface?

A

An interface with no methods.

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

What is a “functional” interface?

A

An interface with one, and only one, abstract method. Even if an interface contains multiple default/and/or static methods, still functional if it has only one abstract method.

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

What is a a predicate?

A
It is a functional interface
- (single abstract method)
-the abstract method is called Test
-the abstract method Test returns a boolean
boolean test (T t)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Which is the correct arrow for lambda?

  • >
  • ->
A

The single arrow

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