fpu

0.3.8 • Public • Published

FPU

Functional programming utilities

Changelog

0.3.8 (latest)

Upgrade babel, webpack and co, remove browser build

0.3.7

Fix Factories::reduce and Immutables

0.3.0

Add Factories

0.2.0

Add Immutables

0.1.0

Initial release : Curries, Functions, Helpers, Predicates & Types

Requirement

node >= 6

Installation

npm install fpu

Documentation

Usage

// ES6-
const {
  Curries,
  Factories,
  Functions,
  Helpers,
  Immutables,
  Predicates,
  Types 
= require('fpu')
 
// ES6+
import {
  Curries,
  Factories,
  Functions,
  Helpers,
  Immutables,
  Predicates,
  Types
} from 'fpu'

Helpers

apply

Partial application

import { Functions, Helpers } from 'fpu'
 
const inc = Helpers.apply( Functions.add, 1 )
 
inc( 4 )
// 5

compose

Right to left function composition

import { Functions, Helpers } from 'fpu'
 
const { add, multiply } = Functions,
      dbl = Helpers.apply( multiply, 2 ),
      inc = Helpers.apply( add, 1 )
 
const addIncThenDbl = Helpers.compose( dbl, inc, add )
 
addIncThenDbl( 1, 2 )
// 8

curry

Function currying

import { Functions, Helpers } from 'fpu'
 
const curriedAdd = Helpers.curry( Functions.add ),
      add5 = curriedAdd( 5 )
 
curriedAdd( 1, 2 )
// 3
 
add5( 3 )
// 8

pipe

Left to right function composition

import { Functions, Helpers } from 'fpu'
 
const { add, multiply } = Functions,
      dbl = Helpers.apply( multiply, 2 ),
      inc = Helpers.apply( add, 1 )
 
const addIncThenDbl = Helpers.pipe( add, inc, dbl )
 
addIncThenDbl( 1, 2 )
// 8

Immutables

Immutables always return an immutable.

array

import { Immutables } from 'fpu'
 
const a = Immutables( [] )
 
// or
 
const a = Immutables()
append

Append items

import { Immutables } from 'fpu'
 
const a = Immutables([ 'foo', 'bar', 'qnx' ])
 
a.append( 'doh', 'woo' )
// [ 'foo', 'bar', 'qnx', 'doh', 'woo' ]
insert

Insert item(s) at a position

import { Immutables } from 'fpu'
 
const a = Immutables([ 'foo', 'bar', 'qnx' ])
 
a.insert( 2, 'doh', 'woo' )
// [ 'foo','bar', 'doh', 'woo', 'qnx' ]
length

Length of the array

import { Immutables } from 'fpu'
 
const a = Immutables([ 'foo', 'bar', 'qnx' ])
 
a.length
// 3
prepend

Prepend item(s)

import { Immutables } from 'fpu'
 
const a = Immutables([ 'foo', 'bar', 'qnx' ])
 
a.prepend( 'doh', 'woo' )
// [ 'doh', 'woo', 'foo', 'bar', 'qnx' ]
remove

Remove item(s) with position(s) and/or value(s)

import { Immutables } from 'fpu'
 
const a = Immutables([ 'foo', 'bar', 'qnx', 'woo' ])
 
a.remove( 1 )
// [ 'foo', 'qnx', woo' ]
 
a.remove( 1, 2 )
// [ 'foo', woo' ]
 
a.remove( 'bar' )
// [ 'foo', 'qnx', woo' ]
 
a.remove( 'bar', 'qnx' )
// [ 'foo', 'woo' ]
 
a.remove( 'bar', 3 )
// [ 'foo', 'qnx' ]
replace

Replace item(s) from a position

import { Immutables } from 'fpu'
 
const a = Immutables([ 'foo', 'bar', 'qnx', 'woo' ])
 
a.replace( 1, null, null )
// [ 'foo', null, null, 'qnx', 'woo' ]
sort

Aka quick sort

import { Immutables } from 'fpu'
 
const a = Immutables([ 'woo', 'foo', 'doh', 'qnx', 'bar' ])
 
a.sort()
// [ 'bar', 'doh', 'foo', 'qnx', 'woo' ]
 
a.sort( true )
// [ 'woo', 'qnx', 'foo', 'doh', 'bar' ]
unique

Only unique values

import { Immutables } from 'fpu'
 
const a = Immutables([ 17, 43, 10, 51, 10, 43, 28 ])
 
a.unique()
// [ 17, 43, 10, 51, 28 ]
value

Return array's value

import { Immutables } from 'fpu'
 
const a = Immutables([ 'foo', 'bar', 'qnx' ])
 
a.value
// [ 'foo', 'bar', 'qnx' ]

object

import { Immutables } from 'fpu'
 
const a = Immutables( {} )
keys

Keys of the object

import { Immutables } from 'fpu'
 
const o = Immutables({ foo: 'bar', qnx: null, hello: 'world' })
 
o.keys
// [ 'foo', 'qnx', 'hello' ]
set

Add and/or update property's values

import { Immutables } from 'fpu'
 
const o = Immutables({ foo: 'bar', qnx: null, hello: 'world' })
 
o.set({ pid: 0 })
// { foo: 'bar', qnx: null, hello: 'world', pid: 0 }
 
o.set({ qnx: 'doh' })
// { foo: 'bar', qnx: 'doh', hello: 'world' }
 
o.set({ qnx: 'doh', pid: 0 })
// { foo: 'bar', qnx: 'doh', hello: 'world', pid: 0 }
unset

Unset properties

import { Immutables } from 'fpu'
 
const o = Immutables({ foo: 'bar', qnx: null, hello: 'world' })
 
o.unset( 'qnx', 'hello' )
// { foo: 'bar' }
value

Return object's value

import { Immutables } from 'fpu'
 
const a = Immutables({ foo: 'bar', qnx: null, hello: 'world' })
 
a.value
// { foo: 'bar', qnx: null, hello: 'world' }

Factories

filter

Array.filter factory

import { Factories } from 'fpu'
 
const a = [ 0, 1, 2, 3, 4, 5 ]
const oddOnly = (v) => v % 2 === 1
const oddFilter = Factories.filter( oddOnly )
 
oddFilter( a )
// [ 1, 3, 5 ]

map

Array.map factory

import { Factories } from 'fpu'
 
const a = [ 0, 1, 2, 3, 4, 5 ]
const dbl = (v) => v * 2
const dblMap = Factories.map( dbl )
 
dblMap( a )
// [ 0, 2, 4, 6, 8, 10 ]

reduce

Array.reduce factory

import { Factories } from 'fpu'
 
const items = [
  { foo: 'bar', woo: null },
  { qnx: true },
  { woo: false }
]
 
const merge = (a, b) => ({ ...a, ...})
const mergeObj = Factories.reduce( merge )
 
mergeObj( items )
// { foo: 'bar', woo: false, qnx: true }

Functions

All this functions have an arity of 2.

add, and, divide, multiply, or, substract

Predicates

All this functions have an arity of 2.

eq, gt, gte, lt, lte, not

Curries

Curried functions and predicates, with an arity of 1.

add, and, divide, eq, gt, gte, lt, lte, multiply, not, or, substract

Types

Types detection's functions, with and arity of 1.

isArray, isboolean, isNull, isNumber, isObject, isString, isUndefined

Readme

Keywords

none

Package Sidebar

Install

npm i fpu

Weekly Downloads

8

Version

0.3.8

License

none

Unpacked Size

54 kB

Total Files

5

Last publish

Collaborators

  • littlepsylo