Monads & TypeScript
One of the reasons I don't love TypeScript is that it takes me significantly longer to write TypeScript than Javascript, and TypeScript encourages you to more functions with unique signatures (the opposite of the convenient monadic functions I want). (TypeScript supports function overloading, the way Swift does (which in turn is derived from Objective-C's named-parameters-that-are-part-of-the-signature, but also similar to Dylan).
E.g. you could define:
function multiply(a: number, b: number) : number { return a * b }
// multiply(3, 4) => 12
function multiply(a: string, b: string) : string {
return `${a} x ${b}`
}
// multiply("3", "4") => "3 x 4"
function multiply(args: number[]) : number {
return args.reduce((p = 1, a) => p * a))
}
// multiply ([4, 3, 2, 1]) => 24
function multiply(args: number[]) : number {
return args.reduce((p = 1, a) => p * a))
}
// multiply ([4, 3, 2, 1]) => 24
function multiply(args: string[]) : number {
return args.reduce((p, a) => p ? p + ` x ${a}` : 'a')
}
// multiply (['4', '3', '2', '1']) => "4 x 3 x 2 x 1"
function multiply(a: string, b: number) : string {
let s = ''
for ( let i = 0; i < b; i++ ) s += a
return s
}
// multiply("3", 4) => "3333"
In Swift the equivalent declaration would lead the parser to figure out which multiply to use given the parameters.
TypeScript isn't that smart, it won't let you vary the number of parameters over an overloaded function.
After writing my previous post, I started playing around with a TypeScript version of monadic.