Skip to content

Instantly share code, notes, and snippets.

@AlyoshaS
Created November 21, 2017 00:21
Show Gist options
  • Save AlyoshaS/f734ab13905cded9f94568a1b77db0f8 to your computer and use it in GitHub Desktop.
Save AlyoshaS/f734ab13905cded9f94568a1b77db0f8 to your computer and use it in GitHub Desktop.
JavaScript Pure
const { log } = console;
/*
* Aritmética: equal, max, min, negative, positive, reverseSign, opositeSigns
* swap, def, undef, plus, minus, sum, fact, addUp
*/
/**
* Verifica a igualdade entre dois números.
*
* @method binaryEqual
* @param {Number} x o inteiro a ser comparado.
* @param {Number} y o inteiro a ser comparado.
* @return {Boolean} Retorna true se x = y ou false caso contrário.
* @example
* // returns true
* binraryEqual(5, 5);
* @returns {Boolean} Retorna o boolean resultante da comparação.
*/
const binaryEqual = function( x, y ) {
return ( x === y );
};
const binaryMax = function( x, y ) {
return ( x > y ) && x || y;
};
const min = function( x, y ) {
return ( x < y ) && x || y;
};
const negative = function( n ) {
return ( n < 0 );
};
const positive = function( n ) {
return !negative( n );
};
const reverseSign = function( n ) {
return ~n + 1;
};
const opositeSigns = function( x, y ) {
return ( ( x ^ y ) < 0 );
};
const swap = function( [ x, y ] ) {
return [ y, x ];
};
const plus = function( x ) {
return x + 1;
}
const minus = function( x ) {
return x - 1;
}
const sum = function( x, y ) {
return x + y;
}
const fact = function( n, acc = 1 ) {
return ( n <= 1 ) && 1*acc || fact( n-1 , acc*n );
};
const addUp = function( n ) {
return n * ( n + 1 ) / 2;
};
/*
* Tipos: def, undef
*/
const def = function( x ) {
return typeof x !== 'undefined';
}
const undef = function( x ) {
return !def( x );
}
/*
* Vetores: addV, clone, deletee, head, tail, pop, push, shift
* unshift, sort, splice, uniq
*/
const addA = function( [ x, y, ...ys ] ) {
return undef( x )
? 0 : undef( y )
? x : equal( ys.length, 0 )
? sum( x, y ) : sum( x, addA( [ y, ...ys ] ) );
};
const head = function( [ x, ...xs ] ) {
return x;
};
const tail = function( [ , ...xs ] ) {
return xs;
}
const clone = function( x ) {
return [ ...x ];
};
const deletee = function( i ) {
return function( x ) {
return [ ...x.slice( 0, i ), ...x.slice( i + 1 ) ];
};
};
const pop = function( x ) {
return x.slice( 0, -1 );
};
const push = function( y ) {
return function( x ) {
return [ ...x, y ];
};
};
const shift = function( x ) {
return x.slice( 1 );
};
const unshift = function( y ) {
return function( y ) {
return [ y, ...x ]
};
};
const sort = function( f ) {
return function( x ) {
return [ ...x ].sort( f );
}
}
const splice = function( s, c, ...y ) {
return function( x ) {
return [ ...x.slice( 0, s ), ...y, ...x.slice( s + c ) ]
};
};
const uniq = function( xs ) {
return [ ...new Set( xs ) ];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment