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:
- Chapter 6 of Eloquent JavaScript
- Mostly Adequate Guide to FP - online book
- Fun Fun Function's video series on FP in JS
- A NetTut's Paid Video Course on FP in JS
- JavaScript Allonge - online book
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:
- Imperative Code
- Imperative Code With Functions
- Object Oriented - One Class
- Object Oriented - Small Classes
- Functional Programming - Basic
- Functional Programming - Pure Functions
- Functional Programming - Maximally Pure But Slow
- Functional Programming - With Mori
- Functional Programming - Currying
- Functional Programming - Composition and Point-Free
- Functional Programming - Final Result