Skip to content

Instantly share code, notes, and snippets.

View e-mihaylin's full-sized avatar
🏠
Working from home

Eugene Mihaylin e-mihaylin

🏠
Working from home
View GitHub Profile
const angle = (a, b, c ,d) => ~~((Math.atan2(d - b, c - a)) * 180 / Math.PI);
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
const hammingWeight = v => {
v = v - (v>>1 & 0x55555555);
v = (v & 0x33333333) + (v>>2 & 0x33333333);
return ((v + (v>>4) & 0xF0F0F0F) * 0x1010101) >> 24;
}
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++;
}
const fib = n => Math.round(Math.pow(((1 + Math.sqrt(5)) / 2), n - 1) / Math.sqrt(5));
const numberFormat = n => String(n).replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,');
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);
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) ;
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))))
const once = fn => ((...a) => {
let executed = false;
return (...a) => {
if (!executed) {
executed = true;
return fn(...a);
}
};
})();