Chapter 10 - Functional Programming Flashcards

1
Q

What is called functional programming?

A

Functional Programming is kind of programming technique which consists different kind of aspects:
All functions must return a value.
» A function can return another function as its return value and can accept
other functions as arguments.
» A function can be assigned to a variable. Any reference to the variable invokes
the function.
» Side effects outside of a function are not allowed. Thus, a function can’t
modify a variable outside of its local scope.
» In fact, no variables in a pure functional program can be modified after they’re
defined and given an initial value. In other words, all variables are immutable.
A purely functional programming language doesn’t have assignment
statements.
» Iteration is done via recursion rather than looping

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

What is declarative programming?

A

Declarative Programming: Declarative programming focuses on expressing the desired outcome or result without specifying the detailed steps to achieve it. The program describes “what” needs to be done rather than “how” to do it. Declarative programming is typically used to define rules, relationships, patterns, and constraints.

With functional programming, you declare what operations to perform on data, but you don’t specify how to do it in a step-by-step manner.

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

The main principles of Functional Programming?

A

Immutability , functional purity and first-class functions

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

Explain functional purity?

A

Functional purity, also known as referential transparency, is a fundamental concept in functional programming that emphasizes writing functions that consistently produce the same output for a given set of inputs and have no side effects. In other words, a pure function is deterministic, stateless, and has no observable impact on the program or the outside world beyond producing a return value.

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

What is immutability?

A

Immutability is a fundamental concept in functional programming that emphasizes the creation of objects or data structures whose state cannot be modified after they are created. In the context of Java, immutability means that an object’s state (its fields or properties) cannot be changed once the object is instantiated. This concept has several important implications and benefits in the context of functional programming:

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

First class functions?

A

The “first-class functions” principle in programming refers to the ability of functions to be treated as first-class citizens, just like any other value, such as integers, strings, or objects. In a language that supports first-class functions, functions can be passed as arguments to other functions, returned as values from functions, and assigned to variables. This principle is fundamental to functional programming and is one of its defining characteristics.

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

What is functional Interface?

A

functional interface is an interface that has one, and only one, abstract
method. The abstract method in the functional interface can be called a function. Any interface that has more than one abstract method or doesn’t have any
abstract methods does not qualify as a functional interface.

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

Single Abstract Method (SAM) Rule

A

A functional interface must have exactly one abstract method. It can have other default or static methods, but only one abstract method.

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

Lambda expression in Java

A

Lambda expressions in Java provide a concise way to define anonymous methods, allowing you to implement functional interfaces quickly and with less code.
Anonymous— We say anonymous because it doesn’t have an explicit name like a method would
normally have: less to write and think about!
Function— We say function because a lambda isn’t associated with a particular class like a method is.
But like a method, a lambda has a list of parameters, a body, a return type, and a possible list of
exceptions that can be thrown.
Passed around— A lambda expression can be passed as argument to a method or stored in a
variable.
Concise— You don’t need to write a lot of boilerplate like you do for anonymous classes

we dont need another class to implement the interface

The syntax for a lambda expression is as follows:

(parameters) -> expression or block of code

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

Explain BiFunction

A

BiFunction is a functional interface in Java that represents a function that accepts two arguments and produces a result.

BiFunction<String, String, String> concatenateFunction = (s1, s2) -> s1 + s2;

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

Pass Function as argument

A

pass functions (or methods) as arguments to other functions, just like you would pass any other data type. This allows for more flexible and dynamic behavior in your code. This concept is commonly associated with functional programming and is supported in Java through the use of functional interfaces, lambda expressions, and method references.

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

Returning function

A

In Java, you can return a function from a method by using functional interfaces, lambda expressions, or method references. This allows you to create higher-order functions, which are functions that either take other functions as parameters or return functions.

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

Closure

A

In the context of functions that return functions, a closure allows the returned function to retain access to the variables of the outer function, even after the outer function has finished executing. This is extremely useful in scenarios where you want to create specialized functions based on some parameters, and these specialized functions need to “remember” those parameters or the context in which they were created.

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

Higher order functions in Java

A

Java, higher-order functions are functions that can accept other functions as parameters and/or return functions as results. However, Java does not support higher-order functions in the same way that some functional programming languages do, like JavaScript or Haskell. Nevertheless, you can achieve similar behavior using interfaces, functional interfaces, and lambda expressions.

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

Function interface

A

In Java, the Function interface is a part of the java.util.function package, introduced in Java 8 as a part of the functional programming enhancements. It’s a functional interface, which means it has a single abstract method and is often used to represent a function that takes an argument of one type and produces a result of another type.

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

Method Reference

A

Method references allow you to shorthand lambda expressions when the lambda body is a direct invocation of a method.

Function<Integer, Integer> doubler = x -> x * 2;
// Equivalent method reference
Function<Integer, Integer> doubler = MyMath::doubleValue;

17
Q

Consumer interface

A

In Java, the Consumer interface is a functional interface that represents an operation that takes a single input and performs some action without returning any result. It is often used for side effects, like printing to the console or updating a data structure. The Consumer interface has a method called accept().
https://github.com/NavrozashviliDamiane/ConsumerInterface_example_Java

18
Q

Supplier Interface

A

Supplier interface is a functional interface that represents a supplier of results. It has no input arguments and is used to provide a result when invoked. The Supplier interface has a method called get().

Imagine you have a vending machine. The vending machine is like a Supplier. It doesn’t need anything from you, but when you push a button (call a method), it gives you the item you asked for.

for example: Scenario: Generating Unique User IDs

https://github.com/NavrozashviliDamiane/SupplierInterface_Java

19
Q

Predicate interface

A

Predicate interface is a functional interface that represents a predicate: a boolean-valued function of one argument. It’s often used to evaluate a condition on the input and return a boolean result, indicating whether the condition is true or false. It has a method test()

https://github.com/NavrozashviliDamiane/PredicateJava_Example

20
Q

challenge of changing requirements

A

software engineering arises from the inherent nature of software development where the needs, preferences, and expectations of users or stakeholders can evolve over time. This dynamism necessitates frequent adjustments or enhancements to the software to align with the changing requirements.

Ex: The weather app displays the current temperature in Celsius for a user’s location.
The user now wants the option to view the temperature in both Celsius and Fahrenheit.

21
Q

Behavior Parameterization

A

passing a behavior, often in the form of a function or method, as a parameter to another function or method. This allows the behavior of the function or method to be customized or parameterized based on the specific behavior provided.

22
Q

Anonymous class

A

An anonymous class is useful when you need a quick, one-off implementation for an interface or an extension of a class without creating a separate named subclass.
A named class has a name and can be reused in multiple places, while an anonymous class is defined and used at a specific location.

They allow you to declare and
instantiate a class at the same time.

23
Q

UnaryOperator

A

The UnaryOperator interface in Java is a functional interface that extends the Function interface. It represents a function that takes a single argument of a specific type and returns a result of the same type. In other words, it is a function that operates on a single operand and produces a result of the same type as the operand.

24
Q

BinaryOperator

A

BinaryOperator interface in Java is another functional interface that extends the BiFunction interface. It represents a function that takes two arguments of the same type and produces a result of the same type. In simpler terms, it’s a function that operates on two operands and produces a result of the same type.

25
Q

BiPredicate

A

BiPredicate interface in Java is a functional interface that represents a predicate (a boolean-valued function) that takes two arguments and returns a boolean. It’s like a condition or a test that evaluates two inputs and produces a true or false result.

26
Q
A