After some time playing around with Haskell, I miss some of its straightforward functions in the default JavaScript std library. So I'm translating a small portion of Prelude into TypeScript.
Works similar to Promise.then, but is synchronous.
[1, 2, 3].chain(xs => [...xs, 4]); // => [1, 2, 3, 4]Returns the head of an array.
[1, 2, 3].head(); // => [1]Returns the last element of an array.
[1, 2, 3].last(); // => [3]Returns the product of all the number inside an array of numbers.
[1, 2, 3, 4].product(); // => 12Returns an array of numbers from start to end (inclusive).
You can also specify a step size.
[].range(1, 5); // => [1, 2, 3, 4, 5]
[].range(2, 10, 2); // => [2, 4, 6, 8, 10]
[].range(1.5, 5.5, 0.5); // => [1.5, 2.5, 3.5, 4.5, 5.5]Returns the sum of all the number inside an array of numbers.
[1, 2, 3, 4].product(); // => 10Returns the tail of an array.
[1, 2, 3].tail(); // => [2, 3]Transposes a matrix.
[[1, 2, 3], [4, 5, 6], [7, 8, 9]].transpose(); // => [[1, 4, 7], [2, 5, 8], [3, 6, 9]]Returns the union of two arrays, with unique values.
[1, 2, 3, 4].union([2, 5, 6]); // => [1, 2, 3, 4, 5, 6]Returns the lines in a string
const str = `Hello!
I am a multiline
string!
`;
str.lines(); // => ['Hello!', 'I am a multiline', 'string!']Converts a string into a list of characters.
"Hello, world!".toList(); // => ['H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!']Returns the words in a string.
"Hello, world!".words(); // => ["Hello,", "world!"]