Skip to content

astuanax/fun

Repository files navigation

fun.js

Functional javascript library Fork on Github

Build Status Test Coverage Code Climate GitHub file size in bytes

Check bundle size on bundlephobia

Install fun.js

$ npm install fun.js --save

Docs

The documentation is done using jsdocs and can be found in the /docs folder or at the url https://astuanax.github.io/fun/

Functional library?

The fun.js is a functional library which supports monads and follows mostly he ramda specifications but tries to keep the bite size down. The focus is to provide helpful functions for react development with a small footprint.

Autocurried ?

Most if not all functions are autocurried, which means they can have any arity and you can still use default parameters in your function parameters.

Neat.

const abc = curry((a, b, c = [1, 2, 3]) => concat(a, b, c))

const add1 = abc(['tss tss'])
add1(['check']) // ['tss tss', 'check', 1, 2, 3]

Examples

Returning the sum of the length of all the arrays in an array

const lengthDeep = compose(sum, map(x => x.length))
const lengthDeep2 = fold((total, xs) => total + xs.length, 0)
    
const mapReduce = curry((m, r) => (x, y) => r(x, m(y)))

const deepLength = xs => fold(mapReduce(x => x.length, add), 0, xs)

lengthDeep([[1], [2, 3], [4, 5, 6]]) === 6

Adds a number to al values of an object

  const mapReduce = curry((m, r) => (x, y) => r(x, m(y)))

  const omap = curry((f, o) => fold(mapReduce(k => ({ [k]: f(o[k]) }), Object.assign), {}, keys(o)))
  const omapAdd10 = omap(add(10))
  omapAdd10({ a: 1, b: 2, c: 3 })) == { 'a': 11, 'b': 12, 'c': 13 }

Get the difference between 2 dates in seconds

const diffInSeconds = compose(toSeconds, diffDate)
const date1 = new Date('1999-12-31')
const date2 = new Date('2000-01-01')
   
diffInSeconds(date1, date2)) === -86400

Other libraries

The fun.js library is used together with some other libraries:

Matrix applicative providing standard matrix operations

Machine learning with functional undertones: perceptron, neural network and decision trees.

WIP React library with helper function