DEV Community

Cover image for Pragmatic Functional Programming in JavaScript
Caleb Weeks
Caleb Weeks

Posted on β€’ Edited on

4 1

Pragmatic Functional Programming in JavaScript

If you have been following along with my recent posts, it is no surprise that I am an avid fan of functional programming. I attempted to solve the Advent of Code 2021 problems using both Haskell and JavaScript to find an idiomatic functional solution. Unfortunately, I learned along the way that certain approaches and techniques are not practical in JavaScript. This led me to the conclusion that functional programming has different flavors, and it is important to find the right blend for your language of choice.

JavaScript is the language I use the most (whether by choice or not), and fortunately it supports functional programming fairly well. I think the bare minimum requirement needed for a language to claim to support functional programming is ergonomic support for higher order functions. (First-class functions are a requirement for higher order functions). What I mean by ergonomic support is that it has to feel natural, not gimmicky. Higher order functions work very well in JavaScript, particularly with ES6 arrow functions.

The lack of certain features such as static types (in particular algebraic data types), pattern matching, or immutable data structures limits the techniques we can use without sacrificing ergonomics, but fortunately, you can get pretty far without these features.

I came across two resources recently which have helped me think through pragmatic functional programming in JavaScript. The first is this talk by Richard Feldman that does a great job of explaining the why of functional programming. The second is this book by Kyle Simpson which proposes a flavor of functional programming that is practical in JavaScript. There are certain areas where my opinions differ from Kyle's, but this book introduces pragmatic functional programming from first principals. Also check out the Mostly Adequate Guide to Functional Programming.

Learning functional programming and figuring out how to use it practically has been a very long journey, and I think many people have felt the same frustrations that I did while learning. Although the functional programming paradigm is gaining momentum, practical resources get drowned out in the noise of both dissenters and people just riding the hype train. This is the start of a series where we will try to make sense of the plethora of information and distill it into actionable conclusions.

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo πŸ“Šβœ¨

Top comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup πŸš€

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay