This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const add = n => { | |
const f = x => add(n + x); | |
f.valueOf = () => n; | |
return f; | |
} | |
// Other Variants: | |
function add(n) { | |
var f = function(x) { return add(n+x); }; | |
f.valueOf = function() { return n; }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const gcd = (a, b) => a ? gcd(b % a, a) : b; | |
const lcm = (a, b) => a * b / gcd(a, b); | |
[1, 2, 3, 4, 5].reduce(lcm); // Returns 60 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const once = fn => ((...a) => { | |
let executed = false; | |
return (...a) => { | |
if (!executed) { | |
executed = true; | |
return fn(...a); | |
} | |
}; | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const chained = f => x => f.reduce((r, f) => f(r), x); | |
// chained([a,b,c,d])(input) | |
// yields the same result as: | |
// d(c(b(a(input)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const hexToBase64 = hex => Buffer.from(hex, 'hex').toString('base64'); | |
const hex2bin = Object.assign( ["0000","0001","0010","0011","0100","0101","0110","0111","1000","1001"], { a:"1010", b:"1011", c:"1100", d:"1101", e:"1110", f:"1111" } ) , | |
bin2base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" , | |
hexToBase64 = s => s.replace( /./g, c => hex2bin[c] ).replace( /.{1,6}/g, s => bin2base64[Number.parseInt(s.padEnd(6,0),2)] ) + "=".repeat(s.length%3) ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const makeNumber = (n, f) => f ? f(n) : n; | |
const zero = f => makeNumber(0, f); | |
const one = f => makeNumber(1, f); | |
const two = f => makeNumber(2, f); | |
const three = f => makeNumber(3, f); | |
const four = f => makeNumber(4, f); | |
const five = f => makeNumber(5, f); | |
const six = f => makeNumber(6, f); | |
const seven = f => makeNumber(7, f); | |
const eight = f => makeNumber(8, f); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const numberFormat = n => String(n).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fib = n => Math.round(Math.pow(((1 + Math.sqrt(5)) / 2), n - 1) / Math.sqrt(5)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const hamming = n => { | |
const seq = [1]; | |
let i2 = 0, i3 = 0, i5 = 0; | |
for (let i = 1; i < n; i++) { | |
let x = Math.min(2 * seq[i2], 3 * seq[i3], 5 * seq[i5]); | |
seq.push(x); | |
if (2 * seq[i2] <= x) i2++; | |
if (3 * seq[i3] <= x) i3++; | |
if (5 * seq[i5] <= x) i5++; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const quickSort = (arr, left, right) => { | |
let len = arr.length, | |
pivot, | |
partitionIndex; | |
if (left < right){ | |
pivot = right; | |
partitionIndex = partition(arr, pivot, left, right); | |
//sort left and right |