Functional Programming: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(20 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
* https://monkey.org/~marius/funsrv.pdf
* Concepts (TODO) https://en.wikipedia.org/wiki/Functional_programming#Concepts
=Internal=
=Internal=


* [[Programming#Functional_Programming|Programming]]
* [[Programming_Languages_Concepts#Functional_Programming|Programming Languages Concepts]]


=Overview=
=Overview=
Line 10: Line 15:


Closures and recursion are at the base of the ''functional programming'' paradigm.
Closures and recursion are at the base of the ''functional programming'' paradigm.
=Object-Oriented Programming vs Functional Programming=
{{Internal|Object-Oriented Programming vs Functional Programming|Object-Oriented Programming vs Functional Programming}}


=Behavior Parameterization=
=Behavior Parameterization=
Line 22: Line 31:


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''.
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_Function|associative]] and [[#Stateless_Function|stateless]], it can be used to implement inherently parallelizable [[Java_8_Streams_API#Reduction|reduction operations]].


==Pure Function==
==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. Also known as ''side-effect-free function'' or ''stateless 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#Pure_Function|Parallelism]].
Also see [[Parallelism#Pure_Function|Parallelism]].


==Stateless Function==
==Stateless Function==
{{External|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==
==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: [[Java_8_Streams_API#Reduction|reduction operations]], such as reduce(), max(), sum(). Some filtering operations are also stateful bounded: [[Filtering_Data_with_Java_8_Streams_API#Discarding_First_n_Elements|skip()]].
===Stateful Unbounded===
Examples of stateful unbounded Streams API operations: [[Filtering_Data_with_Java_8_Streams_API#distinct.28.29|distinct()]], [[Java_8_Streams_API#Sorting_Data|sorting operations]].


==Associative Function==
==Associative Function==
{{External|https://docs.oracle.com/javase/10/docs/api/java/util/stream/package-summary.html#Associativity}}
==Non-Interfering Function==
{{External|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=


<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;">
{{Internal|Closures#Overview|Closures}}
:[[Closures]]
 
</blockquote>
=<span id='Monad'></span>Monads=
 
{{Internal|Monads#Overview|Monads}}
=Organizatorium=
 
* Distributed systems need to separate state from functionality, but object-orientation combines state with behavior.
* https://medium.com/delta-force/the-death-of-traditional-programming-9453d8ec9491

Latest revision as of 15:52, 23 September 2021

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