Functional Programming: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 79: Line 79:


{{Internal|Monads#Overview|Monads}}
{{Internal|Monads#Overview|Monads}}
=Organizatorium=
* Distributed systems need to separate state from functionality, but object-orientation combines state with behavior.

Revision as of 19:55, 17 November 2020

External

Internal

Overview

The functional programming cornerstones are the ability to pass functions as arguments and no shared mutable data. Functional programming is essentially different than imperative programming, where a program is described in terms of a sequence of statements that mutate state.

Java 8 introduced lambda expressions, which allow behavior parameterization and functional programming.

Closures and recursion are at the base of the functional programming paradigm.

Object-Oriented Programming vs Functional Programming

Object-Oriented Programming vs Functional Programming

Behavior Parameterization

Behavior parameterization is a software development pattern that facilitates handling frequent requirement changes. It essentially means taking a block of code and making it available to API calls, which will execute it internally, or will pass it, in turn, to other API layers. The block of code can be called later by other parts of the program - its execution is deferred.

Behavior parameterization is similar to the strategy pattern. In this context, "behavior" and "strategy" can be used interchangeably.

Prior to Java 8, behavior parameterization was possible with anonymous classes. In Java 8, functions can be explicitly passed to the API, as parameters, and returned as results.

Function

From an object-oriented programming language perspective, a function is a block of code that receives a list of arguments and that optionally returns a result, and possibly throws an exception. A function is different from a method in that the function is not associated with any particular class.

If a function is associative and stateless, it can be used to implement inherently parallelizable reduction operations.

Pure Function

A piece of behavior that is perfectly descried solely by the way it transform arguments to results, behaving as a mathematical function and having no side effects. A pure function cannot make nested calls, like performing a database query, it must only rely on its arguments. Also known as side-effect-free function or stateless function.

Also see Parallelism.

Stateless Function

https://docs.oracle.com/javase/10/docs/api/java/util/stream/package-summary.html#Statelessness

Operations like Streams API map() and filter() take each element from the input stream and produce zero or one result in the output stream. These operations are stateless: they do not have an internal state.

Stateful Function

Operations like Streams API reduce(), sum() or max() must have an internal state to accumulate the result.

Stateful Bounded

Examples of stateful bounded Streams API operations: reduction operations, such as reduce(), max(), sum(). Some filtering operations are also stateful bounded: skip().

Stateful Unbounded

Examples of stateful unbounded Streams API operations: distinct(), sorting operations.

Associative Function

https://docs.oracle.com/javase/10/docs/api/java/util/stream/package-summary.html#Associativity

Non-Interfering Function

https://docs.oracle.com/javase/10/docs/api/java/util/stream/package-summary.html#NonInterference

Identity Value

For a function, the identity value is the value for which the following is true, for all x:

f(i, x) = x

Closures

Closures

Monads

Monads

Organizatorium

  • Distributed systems need to separate state from functionality, but object-orientation combines state with behavior.