Functional programming with SanctuaryJs (with optional types of course..)

Rafi
computer Stuff
Published in
4 min readMar 11, 2017

Functional Programming has been on the rise recently in Javascript with the introduction of ES6, ReactJs, CycleJs, ramdaJs etc. in which you use just functions to do every thing without using any loops (recursion and other functions are there for doing that). So is the style of Point free programming where code is written with minimal use of using variable names. There is an awesome article on that In functional programming you make your code into small Lego pieces(functions) that fit together to make a Lego set. making it easier to debug and reason about code.

In spite of all this Javascript seems to be lacking something. Types No I’m not talking about the horrible one that C, C++ or Java has that require you to write verbose code making your code into a abomination. I’m talking about type inference and optional typing like the one Haskell has. Type inference means you don’t need to specify the type and the compiler decides it for itself. Languages like typescript tried to do that but most of us don’t want to write in a language that compiles to Javascript. So how do you do that? SanctuaryJs provides a simple solution. So what advantage does it offer you may ask as they say it in their website

Sanctuary makes it possible to write safe code without null checks

Getting Started With SanctuaryJs

SanctuaryJs performs run-time type checking meaning that it will check using a module called sanctuary-def but this comes with performance cost so you are given an option to disable it during run-time. This type checking is done during development and the code is optimized without type checking during production.

And functions in sanctuaryJs are curried by default. This video explains currying.

SanctuaryJs create() function is used to enable and disable type checking by default. So you can do

const {create, env} = require('sanctuary');
const S = create({
checkTypes: process.env.NODE_ENV !== 'production',
env: env,
});

the code segment process.env.NODE_ENV !== ‘production’ enables type checking in developement and disables it in production. The env defines types you can extend env if you want to create your own type most of the time you don’t need to mess with it.

Handling missing Values

Pure functions should always return something so how will you deal with missing values in pure function?. The solution is Maybe

consider you have an array and a function head that returns the first element of the array

var arr = [1,2,3];
function head(array) {
return array[0]
}
head(arr)

so how do you deal with it when array of zero length is passed to it. trivial approach might be

var arr = [1,2,3];
function head(array) {
if(array.length != 0)
return array[0];
}
head(arr)

but pure function should always return something. You might instead try returning an object that has value and key indicating if it has value or not. so you can do

var arr = [1,2,3];
function head(array) {
if(array.length != 0)
return {
"isNothing": false,
"isJust": true,
"value": array[0]
}
else
return {
"isNothing": true,
"isJust": false
}
}
head(arr)

here isJust is true if value is there else it is false. isNothing is the reverse of it. The value key is added to the object only if value is present.

sanctuaryJs excatly does this.and it has a head function that does this

var arr = [1,2,3]
S.head(arr)

produces

{
"isNothing": false,
"isJust": true,
"value": 1
}

this object is said to be of type Maybe since it can maybe have value or not. The function that returns object of type maybe is said to be of type Maybe

Either

Either is similar like Maybe but unlike missing values it can have values either of type a or value of type b. so for example we say the function can return value either of type ‘a’ or of type ‘b’. The value on the left (here it is ‘a’) is called left and the value on the right (here it is ‘b’) is called right. similar to may be a function that is said to be of type either will return either value of type ‘a’ or of type ‘b’ inside a JSON and will have keys isRight or isLeft set to true or false accordingly.

and also the functions are curried by default the docs in santuaryJs says. Here is an awesome video that explains what is currying is. If you look into the docs you will find some weird symbols like this

// add :: FiniteNumber -⁠> FiniteNumber -⁠> FiniteNumber
S.add(1,1)

for S.add meaning it takes a number and returns a function which can take a number and then return a FiniteNumber (meaning some countable number). also note that type is shown as after double colon :: (it is inspired from haskell).

And you can compose multiple functions into one using S.compose. Meaning it will take many functions and return a function that does what all those functions will do. This article goes into in depth.

S.compose(Math.sqrt, S.inc)(99)

--

--

Rafi
computer Stuff

I'm developer who works mostly with JS and python. In my free time I like to play with data and contribute to open source