Skip to content

Instantly share code, notes, and snippets.

@ukslim
ukslim / ramda-typescript.md
Last active October 12, 2020 11:37
Ramda and Typescript

Ramda and TypeScript

... when @types/ramda is installed.

Type inference for currying:

const curried = add(1);
// infers: const curried: (b: number) => number

...

/*
Find a 5 digit combination that meets these constraints:
x[0] * x[1] == 24
x[3] == x[1] / 3
x[3] + x[4] == x[0] + x[2]
sum(x) == 26
not all the digits are unique
*/
/*
@ukslim
ukslim / fpscaling.js
Last active March 4, 2024 09:57
Avoiding Floating Point errors by scaling up.
// TL:DR
// In JS (and most other languages using floating point)
// 0.3 - 0.2 != 0.1
// 0.6 + 0.3 != 0.9
// These might cause problems for you.
// But!
// (0.3 * 10) - (0.2 * 10) == 0.1 * 10 == 1
// (0.6 * 10) + (0.3 * 10) == 0.9 * 10 == 9
// Use this to your advantage.
// For example, if your get inputs in numbers representing ££.pp, multiply by 100 and do your maths in pennies.
@ukslim
ukslim / refactor-from-ramda.md
Last active May 24, 2024 13:09
Refactoring from Ramda

Refactor away from Ramda

I love Ramda, but my organisation is falling away from it - we have hires who don't think in FP, and Typescript and Ramda fight somewhat.

You can coax Ramda code to work in Typescript by strategically adding as in the right places. But it's often quite hard work -- harder work than refactoring away from Ramda before converting the plain JS to TS.

Here's some patterns for refactoring away from Ramda. In most cases, applying any of these changes leaves you with less clear code than you started with. But you can now apply more refactorings - extract function, extract method, pulling up