Skip to content

Instantly share code, notes, and snippets.

@suissa
Created November 20, 2017 17:51
Show Gist options
  • Save suissa/11ead004c692b0c31e0e40350bbffaac to your computer and use it in GitHub Desktop.
Save suissa/11ead004c692b0c31e0e40350bbffaac 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
*/
const equal = function( x, y ) {
return ( x === y );
};
const max = 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;
}
/*
* 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