I’ve decided to start this thread in order to share knowledge on the programming paradigm referred to as Functional Programming.
For many Functional Programming is something they’ve either never heard about or only recently heard about.
So you’d probably be surprised to know that Functional Programming one of the oldest paradigms; far older than Object Oriented Programming (OOP), in actual fact it’s roots stem from Lamba Calculus in the 1930s, to the first real functional enabled language,
Lisp developed in the late 1950s.
Here is an inline sine formula: , followed by a bit of normal text
code highlight and more normal text.
Functional Programming, like OOP is simply just style of programming; one which in this case:
- Models methods in similar style to mathematical functions
- Avoids hidden inputs, side effects, and mutable state i.e. referentially transparent.
- Employs techniques like mapping, pipelining, recursion, currying and the use of higher order functions.
- No paradigm or style is ever mutually exclusive. Meaning it’s safe to mix.
- No paradigm enforces a All-or-Nothing approach. Meaning they mix well together.
- Incremental integration is not only possible; it’s an great way to start.
FP Adoption in Languages
Many languages that weren’t previously consider functional, have been in the last decade or so enhanced to support a functional programming, for example:
- C# (Since Linq, released in 2007)
- Java (Since Java 8, released in 2014)
- PHP (Since PHP 4, released in 2008)
- Python (Since Python 2, released in 2000)
Of the above four languages Java was probably the most assertive in enforcing a single paradigm: OOP. However since Java 8, under the banner
Project Lambda, significant strides have been made to undo that, and a relatively clear roadmap lies ahead for Java’s functional future.
Java like many other languages is transforming into a multi paradigm friendly language: one which enables us to search for the perfect balance ostensively by
separating logic from action, or stated differently
separating the value and object layers.
Here’s just a few of the changes planned for Java 9 / 10:
- Optional as a proto-value type. They’re testing the idea of either migrating many of the stdlib reference types to value types, and to introduce new value type constructs that
Codes like a class, works like an int!
- Generics over value types (primitives)
- Revamp of Java’s type system, specifically to support small immutable, identityless value types.
You can read more about the Java roadmap here:
What I hope to share
Many of the functional programming articles that you’ll find on the Internet tend to focus on the abstract parts of functional techniques and type theory.
What I’m going to try to do different, is that I hope to better explain the underpinnings of these techniques, by showing you practical examples of both imperative, and “unfunctional” code that people write every day, and then demonstrate how we go about translating this into a more a functional style.
During this process I also hope to convince you that many of the approaches underpinning FP aren’t completely new; and that many of the techniques are probably already employed in a somewhat similar fashion in your own code.
- The first article will focus on what constitutes functional style code; with the end goal to explain why it matters, or why we need a distinguish between value and object types.
- The second article will focus on accumulator loops (e.g. sum of values) using the
Fold (higher-order function), also called
aggregate, and ultimately try to explain why this technique was developed and why it’s useful.
- The third article will focus on transformative loops (e.g. adjusting values) using the
Map (higher-order function), also called
select, with the same end goal: to explain why this technique exists and why it’s useful.
Multi Language Examples:
In an attempt to make these articles more useful We’ll try to present all code examples in each of the following languages:
Future articles could cover aspects like:
- Partial Application
- More higher order functions
…and hopefully by the end of this, you’ll have a better understanding of how to take advantage of FP in your code; and if I’m at all successfull you should also have a better (if not simplistic) understanding of what constitutes: Functors, Applicatives and Monads.