Functions

Introduction

This website is built on the SvelteKit framework. Svelte modules look a lot like ordinary HTML pages, with standard HTML tags and some additional Svelte features.

The modules in this presentation are small and isolated from other modules, making them easy to understand and maintain. It's often good coding practice to prevent bugs by avoiding mutation, side effects, and global variable, and to impose type constraints. But when modules are small and isolated, these precautions unnecessarily add lines of code and make things more complex and complicated than they need to be.

Functions that manipulate values other than those provided as arguments (cause side effects) are dangerous where there are many lines of code, especially when written and maintained by teams of developers. Maintainers of such code can accidentally introduce hard-to-find bugs. But with small, isolated modules, that is not a concern. The full power and flexibility of JavaScript can shine.

Ersatz Monads

"Monad" is rigorously defined in Category Theory, but not in this presentation. Here, a monad is a closure that returns a function that facilitates the composition of values, including anything from asychronous functions to primitive values. Sometimes, like Haskell monads, they shield a series of computations from possible interactions with other code. Usually, they are a convenient means of composing functions.

Some Thoughts about Functional Programming

"Functional programming is an approach to programming based on function calls as the primary programming construct." - Greg Michaelson on Page 3 of An Introduction to Functional Programming Through Lambda Calculus. That's about all I mean by "functional programming", adding only that it is sometimes advantageous to rely on small, reusable functions as arguments for the specialized functions that accomplish the goals of a module.

It's such a joy, in the safety of small, isolated modules, to have the internal workings of a function rely on and mutate global variables - even variables that are not provided as arguments. When your functions don't have to be pure; for example, when you are free to define functions that return results only on every third or fourth execution, JavaScript is free to perform as flexible and powerful wonder that it is.

Trying to make JavaScript functions behave like Haskell functions can be enriching and for some, and much more entertaining than sudoku or crossword puzzles. Such endeavors, however, aren't likely to help us develop robust, easily maintainable applications.

In the modules that follow, I define functions, experiment with functions, admire the power and possibilities of functions, and generally dwell on functions. If, to you, "functional JavaScript" means "cargo-cult JavaScript" (superstitiously imitating strictly functional languages), then call me a heretic. Still, I hope you will at least consider the possibility that in some circumstances, abandoning strict adherence to the so-called "functional paradigm" can greatly improve your JavaScript code.