How to FP

Pure functionsMore FP concepts

Home»Top-down FP

FP is easy, but it can seem difficult at first.

Functional programming (FP) is a programming paradigm, meaning that it is a way of thinking about software construction based on some fundamental, defining principles. Other examples of programming paradigms include object-oriented programming (OOP) and procedural programming.

Why Functional Programming?

Functional code tends to be more concise, more predictable, and easier to test than imperative or object-oriented codebut if you're unfamiliar with it and the common patterns associated with it, then functional code can also seem a lot more dense.

Functional Programming revolves around the idea that a program is made of a set of functions and that they follow certain rules. Forget classes and inheritance: the patterns that you will encounter are quite different here.

FP is the process of building software by:

  • composing pure functions and,
  • avoiding shared state, mutable data, and side-effects.

Let's learn more about these concepts...

FP from the top down

Appendix

Which languages follow which paradigms? (For the curious...)

You might be very surprised. Purely FP, OOP, or procedural langauges are fairly rare. Many langagues begin with a procedural base and then add FP and/or OOP features. Among these are some very popular langauges commonly considered object-oriented.

C++, C#, Java, and JavaScript are all procedural languages with features of object-oriented languages and functional programming languages. Java in particular has been moving quickly in a functional direction, perhaps spurred on by Scala, which is a multi-paradigm competitor to Java.

In JavaScript (ECMAScript), the move toward a strongly functional paradigm has been accelerating annually for several years.

BASIC, C, COBOL, and Fortran are examples of procedural languages still in use.

Object-oriented languages as most of us know them should really be called "class-oriented", as they make a distinction between classes and instances. Some languages are purely OO, for example Ruby, in which everything is an object. Scala has classes and inheritance, but also strong functional features.

JavaScript is actually an object-oriented prototype-based language, which means it lacks the distinction between classes and instances, although the recent (and controversial) addition of "class" syntax hides this a bit. But JavaScript has reasonably strong functional capabilities, and they get stronger with each new version.

Popular object-oriented class-based languages include C++, C#, Java (and Groovy), Objective-C, Perl 5, PHP 5, Python, Ruby, and Scala.

Popular functional programming languages include Erlang and Elixir, F#, Groovy, JavaScript, Java (since version 8), Kotlin, Lisp, Perl 6, PHP, Python, Ruby, Rust, and Scala. Pure functional programming languages include Elm, Haskell, and PureScript.

As you can see, most of the popular languages are now multi-paradigm. Ruby, for example, is a "pure" object-oriented class-based language (everything is an object), but Ruby can still be used in a very functional style (and this style is often strongly recommended).

There are many ways to classify programming languages. If you're interested, check out this list of programming languages by type. Note how many languages appear under many different types.

FP vs. OOP

Resources

Sources

Errors, bugs, suggestions, questions? Contact Prateek Sharma.