HackerNoon.com
Published in

HackerNoon.com

Functional programming in Javascript is an antipattern

And Clojure is actually easier

  • APIs for lodash, Immutable, lodash/fp, ramda, and native JS or some combination
  • mutable programming techniques when working with Javascript data structures
  • immutable programming techniques for Immutable data structures
  • immutable programming with mutable Javascript data structures when working with Redux or React
Functional programming in Javascript.
`Visualization of an antipattern.

What are the long term costs of this?

What is Clojure/Clojurescript?

What makes Clojurescript easier?

Run whatever code you want inside your editor.
  1. You can run any code in your editor with a keypress. This is exactly what it sounds like. You can type whatever code you’re trying to write in your editor, highlight it (or just put your cursor over it) and run it to see the result. You can define functions and call them with whatever arguments you want. You can do all of this while your app is running. So if you don’t know how something works, you can evaluate it in the REPL from your editor and see what’s going on.
  2. Functions work on arrays and objects. Map, reduce, filter, etc. all work identically on arrays and objects. This is by design. We shouldn’t have to think about different map functions for arrays vs. objects.
  3. Immutable data structures. Every Clojurescript data structure is immutable. As a result, you never wonder whether something is immutable or not. You also never switch programming paradigms from mutable to immutable. You’re fully in immutable-land.
  4. Basic functions are part of the language itself. Functions like map, filter, reduce, compose, and many others are part of the core language and don’t need to be imported. So you don’t end up with 4 different versions of, e.g. “map” (Array.map, lodash.map, ramda.map, Immutable.map) in your head. You only have to know one.
  5. It’s concise. It can express ideas in fewer lines of code than most any other programming language (usually much fewer).
  6. Functional programming. Clojurescript is a functional programming language from the ground up — implicit return statements, functions are first class, lambda expressions, etc.
  7. Use anything you want from Javascript. You can use anything from Javascript and its ecosystem, fromconsole.log to npm libraries.
  8. Performance. Clojurescript uses the Google Closure compiler to optimize the Javascript it outputs. Bundle sizes are comically small. It requires no configuration apart from setting optimizations to :advanced when bundling for production.
  9. Readable library code. It’s sometimes useful to know “What does this library function do?” When I use “goto definition” in Javascript, I usually end up seeing the minified or mangled source. Clojure and Clojurescript libraries show up the way they were written, so it’s easy to see how something works without leaving your editor, because you can just read the code.
  10. It’s a LISP. It’s hard to enumerate the benefits of this, as there are many. One thing I like is that it’s formulaic (there’s a pattern to it that I can always count on) and code is expressed in terms of the language’s data structures (which makes metaprogramming easy). Clojure differs from LISP because it is not 100% (). It uses[] and {} for code and for data structures, just like most programming languages.
  11. Metaprogramming. Clojurescript allows you to write code that writes code. This has vast implications that I won’t attempt to cover either. One is that you can effectively extend the language itself. Here’s an example from Clojure for the Brave and True:
(defmacro infix
[infixed]
(list (second infixed) (first infixed) (last infixed)))
(infix (1 + 1))
=> 2
(macroexpand '(infix (1 + 1)))
=> (+ 1 1)
; The macro passes this to Clojure. Clojure evaluates it no problem because it's native Clojure syntax

Why isn’t it popular?

--

--

Elijah McClain, George Floyd, Eric Garner, Breonna Taylor, Ahmaud Arbery, Michael Brown, Oscar Grant, Atatiana Jefferson, Tamir Rice, Bettie Jones, Botham Jean

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store