Part 1: The Introduction. A Fool's guide to writing Functional JS


You’re a developer going about your day writing loops and pushing and deleting stuff from arrays and what not. And then some day you think

maybe I should test my code…
"Shouldn’t be too hard", you say.

And then you get started, you learn the basics of unit testing and even write some tests. But as soon as your code gets a bit more complex, it starts to seem like almost impossible to test. But, you are determined, you’re not gonna give up that easy; you start creating and destroying objects after every test. Maybe your chosen test framework even offers hooks for those.

But Imagine a world where you could test almost every piece of functionality in your code, without jumping through those hoops.

Well, that’s not what I’m gonna teach you today .

What we will try to discuss, however, are some of the basics of a paradigm called Functional Programming (or FP for short). What you can eventually get by following the principles of FP is what we previously discussed and more. Namely, easily testable code, minimal setup in testing, cool developer creds, happiness* .

P.S. I can’t promise that last one, what I can promise is good DX (Developer Experience) which may/may not correlate to happiness.
Right, Lets start!

What is FP?

I’m not gonna get into some complex mathematical/theoretical explanation of what FP is. I’ll try to explain FP in very simple terms…

FP is a paradigm(a way of thinking about problems) where we solve problems in terms of functions and state that does not change(immutable state).

Foreword

A word of warning, FP (and almost all literature about FP) is filled with jargon such as "immutable", "functor", "monad", etc. Don’t get discouraged, once you get familiar with them, you’ll start to see why people use them (hint: to express ideas in short).

Unlike most traditional FP tutorials, I won’t be showing any code in any purely functional language like Haskell or OCaml. While those are great language, I’m not great at them and I think JavaScript is good enough as a language to explore at least the basics of FP

Core Principles (of FP)

Almost every religion comes with some core tenants that you must follow. While FP is not a religion, it does come with some rules that must be obeyed at all times (otherwise the FP gods will be very unpleased and you will suffer as a developer). But, this being me and not some FP guru, I might miss some and am most definitely paraphrasing all of them.

  1. NEVER* mutate state
  2. Don’t cause side effects
  3. If you do need have side effects, isolate and limit them to a small number of functions
  4. Every piece of code that does something should be a function
  5. Oh, and did I mention? NEVER MUTATE STATE
Don’t worry about the jargon for now, we will discuss those terms shortly.

State (and mutation? ‍♀️)

Lets begin by examining some objects in real life. Do you remember those digital watches (CASIO et al) from back in the day that lit up when you pressed a button on the side?
Image: Casio W-86 / W86 / W-86-1VQES "Illuminator" digital watch. Multicherry / CC BY-SA


Lets examine that mechanism. The "backlight" on those watches is a very simple green-ish LED soldered besides the digital display, powered by the on-board battery and sometimes a tiny little solar panel. If we think of that backlight and button mechanism as a simple program. We can observe a few things,

  • The backlight is initially OFF
  • Once you press the button something happens
  • Now the backlight is ON
In this very crude explanation of a backlight, ON and OFF are states. In code, you probably represent that as a variable.

"But what about this mutation thing", you ask.

Well, in imperative/OO code, you are probably used to changing the value of variables. This is what’s known as a mutation (i.e. change). Functional Programming strongly discourages mutating state; Instead you create distinct, new states.

Say you have a list of two numbers (or array or whatever) and now you want to add one more. If your list structure were mutable, you might append another number two it. In immutable land, however, you create a new list containing the items from your old list and one more.

The cool thing is you don’t even need an inherently immutable list structure, you can just use JavaScript’s good old Array type. To make your life easier, TC39 people even threw in Array.prototype.concat.

// Mutable code
let itemList = [1, 2];
mutList.push(3);


// Immutable code
let twoItemList = [1, 2];
let threeItemList = twoItemList.concat(3);
Immutability might seem a little counter-intuitive at first but it is one of the things that will give you the promised testability.

Think about it, itemList might have 2 items at one point of your code and 3 at some other… Your test could be expecting it to have two items but it has three, now you’re wondering where in your program life cycle it changed. Maybe you forgot to clear the junk from a previous test? Maybe you changed it on a different method… You have to sit there and figure that out.

On the other hand, twoItemList always has these two items and never changes, so if you are getting a test failed because your function returned 3 items instead of 2, you know what happened. You returned the wrong list ​.​

Side Effects (and pure functions)

Relax, we are not gonna discuss pharmaceuticals.
One of the core tenants of FP is to try and avoid side-effects.

"But, what on earth is a side effect?", you ask.

Glad you asked. A side effect is anything you do that does not involve your own state. Lets get back to this in a bit. But first, functions

Functions

"But I already know everything I need to know about functions", you say.

Do you?

In FP a function (or rather a pure function) is a piece of code that may/may not take something as input (arguments) and returns something and does nothing more. It does not:

  • Perform some I/O task
  • Change the state of anything outside its scope
  • Return different things for the same input
So, getting back to out Side Effects discussion, a side effect is any and all of the above discussed despicable things except the last one.

Or in other words, if a function performs some I/O task instead of or in addition to returning an output, or it changes the global state somehow; it is said to have side effects and is referred to as an impure function*

A function can also be impure if its output is non-deterministic. That is, gives different output for the exact same input.

That’s all for today

I feel like the above topics are a good enough indication of how things work in FP. We’ll get into more details about how to do certain things in the next article.

Till then, Peace ✌️

You%27re%20a%20developer%20going%20about%20your%20day%20writing%20loops%20and%20pushing%20and%20deleting%20stuff%20from%20arrays%20and%20what%20not.%20And%20then%20some%20day%20you%20think%20%uD83E%uDD14%0A%0A%3E%20maybe%20I%20should%20test%20my%20code...%0A%0A%22Shouldn%27t%20be%20too%20hard%22%2C%20you%20say.%0A%0AAnd%20then%20you%20get%20started%2C%20you%20learn%20the%20basics%20of%20unit%20testing%20and%20even%20write%20some%20tests.%20But%20as%20soon%20as%20your%20code%20gets%20a%20bit%20more%20complex%2C%20it%20starts%20to%20seem%20like%20almost%20impossible%20to%20test.%20But%2C%20you%20are%20determined%2C%20you%27re%20not%20gonna%20give%20up%20that%20easy%3B%20you%20start%20creating%20and%20destroying%20objects%20after%20every%20test.%20Maybe%20your%20chosen%20test%20framework%20even%20offers%20hooks%20for%20those.%0A%0ABut%20Imagine%20a%20world%20where%20you%20could%20test%20almost%20every%20piece%20of%20functionality%20in%20your%20code%2C%20without%20jumping%20through%20those%20hoops.%20%0A%0AWell%2C%20that%27s%20not%20what%20I%27m%20gonna%20teach%20you%20today%20%uD83D%uDE05.%0A%0AWhat%20we%20will%20try%20to%20discuss%2C%20however%2C%20are%20some%20of%20the%20basics%20of%20a%20paradigm%20called%20Functional%20Programming%20%28or%20FP%20for%20short%29.%20What%20you%20can%20eventually%20get%20by%20following%20the%20principles%20of%20FP%20is%20what%20we%20previously%20discussed%20and%20more.%20Namely%2C%20easily%20testable%20code%2C%20minimal%20setup%20in%20testing%2C%20cool%20developer%20creds%2C%20happiness*%20%uD83D%uDE05.%0A%0A%3E%20P.S.%20I%20can%27t%20promise%20that%20last%20one%2C%20what%20I%20can%20promise%20is%20good%20DX%20%28Developer%20Experience%29%20which%20may/may%20not%20correlate%20to%20happiness.%0A%0A**Right%2C%20Lets%20start%21**%0A%0A%23%23%20What%20is%20FP%3F%0A%0AI%27m%20not%20gonna%20get%20into%20some%20complex%20mathematical/theoretical%20explanation%20of%20what%20FP%20is.%20I%27ll%20try%20to%20explain%20FP%20in%20very%20simple%20terms...%0A%0AFP%20is%20a%20paradigm%28a%20way%20of%20thinking%20about%20problems%29%20where%20we%20solve%20problems%20in%20terms%20of%20functions%3Csup%3E%uD83D%uDE09%3C/sup%3E%20and%20state%20that%20does%20not%20change%28immutable%20state%29.%0A%0A%23%23%23%20*Foreword*%0A%0A%3E%20A%20word%20of%20warning%2C%20FP%20%28and%20almost%20all%20literature%20about%20FP%29%20is%20filled%20with%20jargon%20such%20as%20%22immutable%22%2C%20%22functor%22%2C%20%22monad%22%2C%20etc.%20Don%27t%20get%20discouraged%2C%20once%20you%20get%20familiar%20with%20them%2C%20you%27ll%20start%20to%20see%20why%20people%20use%20them%20%28hint%3A%20to%20express%20ideas%20in%20short%29.%0A%0A%3E%20Unlike%20most%20traditional%20FP%20tutorials%2C%20I%20won%27t%20be%20showing%20any%20code%20in%20any%20purely%20functional%20language%20like%20Haskell%20or%20OCaml.%20While%20those%20are%20great%20language%2C%20I%27m%20not%20great%20at%20them%20and%20I%20think%20JavaScript%20is%20good%20enough%20as%20a%20language%20to%20explore%20at%20least%20the%20basics%20of%20FP%20%uD83D%uDE03%0A%0A%23%23%20Core%20Principles%20%28of%20FP%29%0A%0AAlmost%20every%20religion%20comes%20with%20some%20core%20tenants%20that%20you%20**must**%20follow.%20While%20FP%20is%20not%20a%20religion%2C%20it%20does%20come%20with%20some%20rules%20that%20must%20be%20obeyed%20at%20all%20times%20%28otherwise%20the%20FP%20gods%20will%20be%20very%20unpleased%20and%20you%20will%20suffer%20as%20a%20developer%29.%20But%2C%20this%20being%20me%20and%20not%20some%20FP%20guru%2C%20I%20might%20miss%20some%20and%20am%20most%20definitely%20paraphrasing%20all%20of%20them.%0A%0A1.%20**NEVER***%20mutate%20state%0A2.%20Don%27t%20cause%20side%20effects%0A3.%20If%20you%20do%20need%20have%20side%20effects%2C%20isolate%20and%20limit%20them%20to%20a%20small%20number%20of%20functions%0A4.%20Every%20piece%20of%20code%20that%20*does%20something*%20should%20be%20a%20function%0A5.%20Oh%2C%20and%20did%20I%20mention%3F%20**NEVER%20MUTATE%20STATE**%0A%0ADon%27t%20worry%20about%20the%20jargon%20for%20now%2C%20we%20will%20discuss%20those%20terms%20shortly.%0A%0A%23%23%20State%20%28and%20mutation%3F%20%uD83E%uDD37%u200D%u2640%uFE0F%29%0A%0ALets%20begin%20by%20examining%20some%20objects%20in%20real%20life.%20Do%20you%20remember%20those%20digital%20watches%20%28CASIO%20et%20al%29%20from%20back%20in%20the%20day%20that%20lit%20up%20when%20you%20pressed%20a%20button%20on%20the%20side%3F%0A%0A%7C%20%21%5BCasio%20W-86%20/%20W86%20/%20W-86-1VQES%20%22Illuminator%22%20digital%20watch%5D%28https%3A//upload.wikimedia.org/wikipedia/commons/thumb/8/89/Casio_W-86_digital_watch_electroluminescent_backlight_%2528ii%2529.jpg/480px-Casio_W-86_digital_watch_electroluminescent_backlight_%2528ii%2529.jpg%29%20%7C%0A%7C%20%3A----------------------------------------------------------%3A%20%7C%0A%7C%20**Image**%3A%20*Casio%20W-86%20/%20W86%20/%20W-86-1VQES%20%22Illuminator%22%20digital%20watch*.%20%5BMulticherry%5D%28https%3A//commons.wikimedia.org/wiki/File%3ACasio_W-86_digital_watch_electroluminescent_backlight_%28ii%29.jpg%20%22via%20Wikimedia%20Commons%22%29%20/%20%5BCC%20BY-SA%5D%28https%3A//creativecommons.org/licenses/by-sa/4.0%29%20%7C%0A%0ALets%20examine%20that%20mechanism.%20The%20%22backlight%22%20on%20those%20watches%20is%20a%20very%20simple%20green-ish%20LED%20soldered%20besides%20the%20digital%20display%2C%20powered%20by%20the%20on-board%20battery%20and%20sometimes%20a%20tiny%20little%20solar%20panel.%20If%20we%20think%20of%20that%20backlight%20and%20button%20mechanism%20as%20a%20simple%20program.%20We%20can%20observe%20a%20few%20things%2C%0A%0A*%20The%20backlight%20is%20initially%20**OFF**%0A*%20Once%20you%20press%20the%20button%20*something%20happens*%0A*%20Now%20the%20backlight%20is%20**ON**%0A%0AIn%20this%20very%20crude%20explanation%20of%20a%20backlight%2C%20ON%20and%20OFF%20are%20states.%20In%20code%2C%20you%20probably%20represent%20that%20as%20a%20variable.%0A%0A%22But%20what%20about%20this%20mutation%20thing%22%2C%20you%20ask.%0A%0AWell%2C%20in%20imperative/OO%20code%2C%20you%20are%20probably%20used%20to%20*changing*%20the%20value%20of%20variables.%20This%20is%20what%27s%20known%20as%20a%20mutation%20%28i.e.%20change%29.%20Functional%20Programming%20**strongly%20discourages**%20mutating%20state%3B%20Instead%20you%20create%20distinct%2C%20new%20states.%0A%0ASay%20you%20have%20a%20list%20of%20two%20numbers%20%28or%20array%20or%20whatever%29%20and%20now%20you%20want%20to%20add%20one%20more.%20If%20your%20list%20structure%20were%20mutable%2C%20you%20might%20append%20another%20number%20two%20it.%20In%20immutable%20land%2C%20however%2C%20you%20create%20a%20new%20list%20containing%20the%20items%20from%20your%20old%20list%20and%20one%20more.%0A%0AThe%20cool%20thing%20is%20you%20don%27t%20even%20need%20an%20inherently%20immutable%20list%20structure%2C%20you%20can%20just%20use%20JavaScript%27s%20good%20old%20Array%20type.%20To%20make%20your%20life%20easier%2C%20TC39%20people%20even%20threw%20in%20**Array.prototype.concat**.%0A%0A%60%60%60javascript%0A//%20Mutable%20code%0Alet%20itemList%20%3D%20%5B1%2C%202%5D%3B%0AmutList.push%283%29%3B%0A%0A//%20Immutable%20code%0Alet%20twoItemList%20%3D%20%5B1%2C%202%5D%3B%0Alet%20threeItemList%20%3D%20twoItemList.concat%283%29%3B%0A%60%60%60%0A%0A%0A%0AImmutability%20might%20seem%20a%20little%20counter-intuitive%20at%20first%20but%20it%20is%20one%20of%20the%20things%20that%20will%20give%20you%20the%20promised%20testability.%20%0A%0AThink%20about%20it%2C%20*itemList*%20might%20have%202%20items%20at%20one%20point%20of%20your%20code%20and%203%20at%20some%20other...%20Your%20test%20could%20be%20expecting%20it%20to%20have%20two%20items%20but%20it%20has%20three%2C%20now%20you%27re%20wondering%20where%20in%20your%20program%20life%20cycle%20it%20changed.%20Maybe%20you%20forgot%20to%20clear%20the%20junk%20from%20a%20previous%20test%3F%20Maybe%20you%20changed%20it%20on%20a%20different%20method...%20You%20have%20to%20sit%20there%20and%20figure%20that%20out.%0A%0AOn%20the%20other%20hand%2C%20*twoItemList*%20always%20has%20these%20two%20items%20and%20never%20changes%2C%20so%20if%20you%20are%20getting%20a%20test%20failed%20because%20your%20function%20returned%203%20items%20instead%20of%202%2C%20you%20know%20what%20happened.%20You%20returned%20the%20wrong%20list%20%u200B%uD83D%uDE07.%u200B%20%0A%0A%23%23%20Side%20Effects%20%28and%20pure%20functions%29%0A%0A%3E%20%20Relax%2C%20we%20are%20not%20gonna%20discuss%20pharmaceuticals.%0A%0AOne%20of%20the%20core%20tenants%20of%20FP%20is%20to%20try%20and%20avoid%20side-effects.%0A%0A%22But%2C%20what%20on%20earth%20is%20a%20side%20effect%3F%22%2C%20you%20ask.%0A%0AGlad%20you%20asked.%20A%20side%20effect%20is%20anything%20you%20do%20that%20does%20not%20involve%20your%20own%20state.%20Lets%20get%20back%20to%20this%20in%20a%20bit.%20But%20first%2C%20functions%0A%0A%23%23%23%20Functions%0A%0A%22But%20I%20already%20know%20everything%20I%20need%20to%20know%20about%20functions%22%2C%20you%20say.%0A%0ADo%20you%3F%0A%0AIn%20FP%20a%20function%20%28or%20rather%20a%20pure%20function%29%20is%20a%20piece%20of%20code%20that%20may/may%20not%20take%20something%20as%20input%20%28arguments%29%20and%20returns%20something%20and%20does%20nothing%20more.%20It%20does%20not%3A%0A%0A*%20Perform%20some%20I/O%20task%0A*%20Change%20the%20state%20of%20anything%20outside%20its%20scope%0A*%20Return%20different%20things%20for%20the%20same%20input%0A%0ASo%2C%20getting%20back%20to%20out%20Side%20Effects%20discussion%2C%20a%20side%20effect%20is%20any%20and%20all%20of%20the%20above%20discussed%20despicable%20things%20except%20the%20last%20one.%0A%0AOr%20in%20other%20words%2C%20if%20a%20function%20performs%20some%20I/O%20task%20instead%20of%20or%20in%20addition%20to%20returning%20an%20output%2C%20or%20it%20changes%20the%20global%20state%20somehow%3B%20it%20is%20said%20to%20have%20side%20effects%20and%20is%20referred%20to%20as%20an%20impure%20function*%0A%0A%3E%20A%20function%20can%20also%20be%20impure%20if%20its%20output%20is%20non-deterministic.%20That%20is%2C%20gives%20different%20output%20for%20the%20exact%20same%20input.%0A%0A%0A%0A%23%23%20That%27s%20all%20for%20today%0A%0AI%20feel%20like%20the%20above%20topics%20are%20a%20good%20enough%20indication%20of%20how%20things%20work%20in%20FP.%20We%27ll%20get%20into%20more%20details%20about%20how%20to%20do%20certain%20things%20in%20the%20next%20article.%0A%0A**Till%20then%2C%20Peace%20%u270C%uFE0F**%0A%0A%21%5BDeuces%5D%28https%3A//media.giphy.com/media/NG6dWJC9wFX2/giphy.gif%29%0A%0A


comments powered by Disqus