Functional Programming By Example

While chatting in a CodeNewbie Slack channel I realized that the question of what functional programming is in relation to object oriented programming is a question that comes up a lot and is an area of great confusion. For this reason, I wanted to write an article to hopefully help clear up this confusion, even if just for a little.

What I came up with was 11 different versions of the same program. The program reads in a file containing some text, breaks the text into individual words, counts them up, and displays the top 10 words that were used in the text.

I don't think this will work as your first introduction to functional programming. It is probably used best as a pairing to another introductory article or lecture. Here are a few to choose from:

I have heavily commented each of these source files, and I think the best way to read them is to open them all up in individual browser tabs, and then start reading from 1. At times a source file may make reference to another source file, at which point you are welcome to jump to the refering one, and you can certainly bounce around as much as you'd like to. Without further ado, here they are:

  1. Imperative Code
  2. Imperative Code With Functions
  3. Object Oriented - One Class
  4. Object Oriented - Small Classes
  5. Functional Programming - Basic
  6. Functional Programming - Pure Functions
  7. Functional Programming - Maximally Pure But Slow
  8. Functional Programming - With Mori
  9. Functional Programming - Currying
  10. Functional Programming - Composition and Point-Free
  11. Functional Programming - Final Result
blog comments powered by Disqus