Skip to content

Instantly share code, notes, and snippets.

@suissa
Created May 12, 2017 17:17
Show Gist options
  • Save suissa/42819571d993f6964d548818c114266d to your computer and use it in GitHub Desktop.
Save suissa/42819571d993f6964d548818c114266d to your computer and use it in GitHub Desktop.
/**
Nossa lib de JS Funcional
*/
const _pipe = ( f, g ) => ( ...args ) =>
g( f( ...args ) )
const pipe = ( ...fns ) =>
fns.reduce(_pipe)
const pipeSimple = ( f, g ) => ( x ) =>
g( f( x ) )
const compose = ( f, g ) => ( x ) =>
f( g( x ) )
const curry = ( f, ...args ) =>
( f.length <= args.length )
? f( ...args )
: ( ...more ) => curry( f, ...args, ...more )
const map = ( fn ) => ( xs ) =>
xs.map( fn )
const reduce = ( fn, ini ) => xs =>
xs.reduce( fn, ini )
const isEvery = ( test ) => ( bool, x ) => bool && test( x )
const isSome = ( test ) => ( bool, x ) => bool || test( x )
const every = ( test ) => ( arr ) =>
arr.reduce( isEvery( test ), true )
const some = ( test ) => ( arr ) =>
arr.reduce( isSome( test ), false )
const divideBy = ( y ) => ( x ) => x / y
const multiplyBy = ( y ) => ( x ) => x * y
const addBy = ( y ) => ( x ) => x + y
const root = ( y ) => ( x ) => Math.pow( x, 1 / y )
const pow = ( y ) => ( x ) => Math.pow( x, y )
const addField = ( field ) => ( obj ) =>
Object.assign( {}, obj, field )
const Lib = {
pipe
, pipeSimple
, compose
, root
, pow
, divideBy
, multiplyBy
, addBy
, addField
, map
, reduce
, every
, some
}
module.exports = Lib
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment