Chapter 10 - Functional Programming Flashcards
What is called functional programming?
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
What is declarative programming?
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.
The main principles of Functional Programming?
Immutability , functional purity and first-class functions
Explain functional purity?
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.
What is immutability?
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:
First class functions?
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.
What is functional Interface?
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.
Single Abstract Method (SAM) Rule
A functional interface must have exactly one abstract method. It can have other default or static methods, but only one abstract method.
Lambda expression in Java
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
Explain BiFunction
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;
Pass Function as argument
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.
Returning function
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.
Closure
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.
Higher order functions in Java
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.
Function interface
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.