Sitemap

Level Up Coding

Coding tutorials and news. The developer homepage gitconnected.com && skilled.dev && levelup.dev

Stop Writing Unsafe JavaScript — Functional Programming for JavaScript Developers

7 min readMar 9, 2020

--

Press enter or click to view image in full size
Image

When we are talking about Functional Programming in JavaScript, there are many routes you can take. On one side, we have the version where you just focus on immutability and pure functions while still use a more object-oriented (OO) approach to your architecture. On the other side, we have the all-in strictly-typed functional programming style that does not allow any OO concepts at all. This article series points more towards the latter. Writing in this style from the ground up can be an arduous task. We can simplify this immensely by using a library like Sanctuary in our project.

Welcome to part 8 of Functional Programming for JavaScript Developers. Today we will focus on setting you up for real-life development by introducing you to Sanctuary, mainly written by

.

What is Sanctuary?

Sanctuary is a JavaScript library that emphasizes on the strictly typed version of Functional Programming. It does so by using features that already exist in JavaScript. So there is no need for Webpack or other compilers. You just add the library to your project, and you are good to go.

Sanctuary is true to the Fantasy Land specification. So you can be sure that it follows all the laws for Algebraic Structures.

The library splits into several parts. Let’s take a look at each piece separately.

The Sanctuary main library

In the beginning, this is the only part you’ll need. In short, the Sanctuary main library (referenced as “Sanctuary” from now on) is a utility library for working with data.

If you have ever played with Ramda, then think of Sanctuary like Ramda on steroids when it comes to type safety. If you provide a function with the wrong type, a friendly error will be thrown, telling you where the mistake occurred. Here is an example:

Press enter or click to view image in full size
Image

Here, the function S.head expected a Foldable(algebraic structure) f of any type (For example Array Integer). Instead, it got a String. The benefit of this…

--

--

Level Up Coding
Level Up Coding
Jan-Vidar Tandberg Bakke
Jan-Vidar Tandberg Bakke

Written by Jan-Vidar Tandberg Bakke

For my English followers: I am currently writing an article series in Norwegian. I promise to come back with more English content in the future.

Responses (1)