Sign in

Senior Software Engineer, Functional Programming ๐Ÿง™โ€โ™‚๏ธ

In typescript, type guards are extremely unsafe. I will try to explain why and whatโ€™s the solution. To start letโ€™s take a look at this simple program:

function isNumber(x: any): x is number {
return typeof x === "number";
}
function main(x: unknown) {
if (isNumber(x)) {
console.log(x.toFixed(10))
} else {
console.log("0.0")
}
}
main("")

There is nothing crazy about it. it works as expected and all good. but did you know that you can change definition of to this:

function isNumber(x: any): x is number {
return typeof x === "string";
}

and it will still compile. Butโ€ฆ


What is a Monoid

if you have this 3 things T, concat and empty together, you have a monoid:

  • T โ€” Set of values for example set of strings, or set of array etc,
  • concat โ€” Function which takes 2 values from that set T, and returns new value which is in the same set T, such that this function is Associative.
  • empty โ€” special value from the set T such that it is an identity element for that function concat.

We can express this in typescript like this:

type Monoid<T> = {
concat: (a:T, b:T) => T;
empty: T;
}

And we canโ€ฆ


or how to invert some functions

I will introduce simple motivation behind this whole story and shortly we will know how to derive from , with reasonable constraints โ€” and .

Motivation

Type system is a friend whoโ€™s got your back by making sure all inputs are encountered when writing functions. Usually takes some type which is smaller (has smaller number of values) than its resulting type. โ€ฆ


แƒ”แƒกแƒ” แƒ˜แƒ’แƒ˜ แƒ’แƒ•แƒ˜แƒœแƒ“แƒ แƒ แƒแƒ› แƒ’แƒแƒ‘แƒ แƒฌแƒงแƒ˜แƒœแƒ“แƒ”แƒก แƒกแƒแƒฅแƒแƒ แƒ—แƒ•แƒ”แƒšแƒ.

แƒ›แƒแƒ’แƒ แƒแƒ› แƒกแƒแƒœแƒแƒ› แƒ’แƒแƒ‘แƒ แƒฌแƒงแƒ˜แƒœแƒ”แƒ‘แƒแƒ›แƒ“แƒ” แƒ›แƒ˜แƒ•แƒแƒšแƒ—, แƒแƒ› แƒ•แƒ˜แƒ“แƒ”แƒแƒก แƒฃแƒงแƒฃแƒ แƒ”แƒ— แƒ แƒแƒ› แƒ“แƒแƒ˜แƒœแƒแƒฎแƒแƒ— แƒ—แƒฃ แƒกแƒแƒ“ แƒ•แƒแƒ แƒ— แƒ“แƒฆแƒ”แƒก (HD-แƒจแƒ˜ แƒฉแƒแƒ แƒ—แƒ”แƒ—):

แƒ แƒ‘แƒ˜แƒšแƒแƒ“ แƒ แƒแƒ› แƒ•แƒ—แƒฅแƒ•แƒแƒ— แƒชแƒฃแƒ“แƒแƒ“แƒแƒ แƒกแƒแƒฅแƒ›แƒ”.

แƒแƒ› แƒ•แƒ˜แƒ“แƒ”แƒแƒ› แƒ’แƒแƒ“แƒแƒ›แƒแƒ“แƒ’แƒ›แƒ”แƒ•แƒ˜แƒœแƒ แƒ”แƒก แƒœแƒแƒ‘แƒ˜แƒฏแƒ˜, แƒ“แƒ แƒแƒฎแƒšแƒ แƒจแƒ”แƒ•แƒ”แƒชแƒ“แƒ”แƒ‘แƒ˜ แƒ’แƒแƒ“แƒ›แƒแƒ•แƒชแƒ” แƒ˜แƒก แƒแƒ–แƒ แƒ”แƒ‘แƒ˜ แƒ แƒแƒ›แƒšแƒ”แƒ‘แƒ˜แƒช แƒ‘แƒแƒšแƒ แƒ“แƒ แƒแƒก แƒ›แƒ˜แƒขแƒ แƒ˜แƒแƒšแƒ”แƒ‘แƒ“แƒ แƒ—แƒแƒ•แƒจแƒ˜.

แƒแƒ› แƒ•แƒ˜แƒ“แƒ”แƒแƒจแƒ˜ แƒ แƒแƒ’แƒแƒ แƒช โ€œแƒ›แƒ—แƒแƒ•แƒแƒ แƒ˜ แƒ’แƒ›แƒ˜แƒ แƒ˜โ€ แƒแƒ›แƒ‘แƒแƒ‘แƒก:

แƒ›แƒ˜แƒšแƒ˜แƒแƒœ แƒœแƒแƒฎแƒ”แƒ•แƒแƒ  แƒแƒ“แƒแƒ›แƒ˜แƒแƒœแƒก แƒแƒ แƒฉแƒ”แƒœแƒก แƒ แƒฃแƒกแƒ”แƒ—แƒ˜!

แƒจแƒ”แƒกแƒแƒ‘แƒแƒ›แƒ˜แƒกแƒ˜ แƒกแƒขแƒแƒขแƒ˜แƒกแƒขแƒ˜แƒ™แƒ แƒแƒ  แƒ›แƒแƒ›แƒ˜แƒซแƒ”แƒ‘แƒœแƒ˜แƒ, แƒ›แƒแƒ’แƒ แƒแƒ› แƒแƒ  แƒ›แƒ’แƒแƒœแƒ˜แƒ แƒ แƒ”แƒšแƒแƒแƒ‘แƒ แƒ“แƒ แƒแƒ›แƒแƒขแƒฃแƒšแƒแƒ“ แƒ’แƒแƒœแƒกแƒฎแƒ•แƒแƒ•แƒ“แƒ”แƒ‘แƒแƒ“แƒ”แƒก. แƒ›แƒแƒ แƒขแƒ แƒ˜แƒก แƒคแƒแƒฅแƒขแƒ˜ แƒ แƒแƒ› แƒ˜แƒก แƒแƒกแƒ” แƒคแƒ˜แƒฅแƒ แƒแƒ‘แƒก, แƒฉแƒ”แƒ›แƒ—แƒ•แƒ˜แƒก แƒกแƒแƒ™แƒ›แƒแƒ แƒ˜แƒกแƒ˜แƒ แƒ แƒแƒ› แƒ“แƒแƒ•แƒ˜แƒฏแƒ”แƒ แƒ.

แƒแƒœแƒฃ แƒ แƒ แƒฎแƒ“แƒ”แƒ‘แƒ,

แƒกแƒแƒฅแƒแƒ แƒ—แƒ•แƒ”แƒšแƒแƒจแƒ˜ แƒกแƒแƒจแƒฃแƒแƒšแƒ แƒฎแƒ”แƒšแƒคแƒแƒกแƒ˜ 1000 แƒšแƒแƒ แƒ˜แƒ (แƒ—แƒฃ แƒ›แƒ˜แƒ•แƒแƒ›แƒแƒขแƒ”แƒ‘แƒ— แƒ˜แƒ› แƒแƒ“แƒแƒ›แƒ˜แƒแƒœแƒ”แƒ‘แƒก, แƒ แƒแƒ›แƒšแƒ”แƒ‘แƒ˜แƒช แƒแƒ  แƒ˜แƒฎแƒ“แƒ˜แƒแƒœ แƒ’แƒแƒ“แƒแƒกแƒแƒฎแƒแƒ“แƒก โ€” แƒ’แƒšแƒ”แƒฎแƒ”แƒ‘แƒ˜, แƒขแƒแƒฅแƒกแƒ˜แƒก แƒ›แƒซแƒฆแƒแƒšแƒ”แƒ‘แƒ˜, แƒ›แƒฃแƒจแƒ”แƒ‘แƒ˜ แƒ“แƒ แƒกแƒฎแƒ•แƒ แƒ—แƒ•แƒ˜แƒ—แƒ“แƒแƒกแƒแƒฅแƒ›แƒ”แƒ‘แƒฃแƒšแƒ”แƒ‘แƒ˜ แƒ”แƒกโ€ฆ


Executing composition of many functions is not stack safe. For example:

// if we have a list of many functions
const fs = Array(100000).fill().map((_,idx) => x => x + idx)
// and we want to compose them
const compose = (f,g) => x => f(g(x))
// we can either use compose to create a new function which is
// composition of other functions, which is not stack safe
fs.reduce(compose)(1)
// or we can "interpret" the list of functions step by step
// in a stack safe way
fs.reduceRight((arg,f) => f(arg),1) // 4999950001

To fix this issue we need a wayโ€ฆ

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