Giter Club home page Giter Club logo

futil's Introduction

futil

a small functional library

to use

map

const arr = [ 1, 2, 3, 4, 5 ]
const f = i => i + 1

map(f, arr)
  => [ 2, 3, 4, 5, 6 ]

// partial application
const addOne = map(f)
addOne(arr)
  => [ 2, 3, 4, 5, 6 ]

addOne([ 2, 3, 4, 5, 6 ])
  => [ 3, 4, 5, 6, 7 ]

unique

const arr = [ 1, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6, 6 ]
unique(arr)
  => [ 1, 2, 3, 4, 5, 6 ]

every

const a = [ 1, 2, 3, 4, 5 ]
const f = i => typeof i === 'number'

every(f, a)
  => true

// partial application
const isNumeric = every(f)
isNumeric(a)
  => true

sum

const arr = [ 1, 2, 3, 4, 5 ]
sum(arr)
  => 15

equals

const a = [ 1, 2, 3, 4, 5 ]
const b = [ 1, 2, 3, 4, 5 ]
const c = [ 2, 3, 4, 5, 6 ]

equals(a, b)
  => true

// partial application
const equalsA = equals(a)

equalsA(c)
  => false

range

const r = range(10)
  => [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

lhs & rhs

const a = lhs('a', 'b')
  => 'a'
const b = rhs('a', 'b')
  => 'b'

filter

const arr = [ 1, 2, 3, 4, 5 ]
const f = i => i > 3

filter(f, arr)
  => [ 4, 5 ]

// partial application
greaterThanThree = filter(f)
greaterThanThree(arr)
  => [ 4, 5 ]

greaterThanThree([ 2, 3, 4, 5, 6 ])
  => [ 4, 5, 6 ]

reduce

const arr = [ 1, 2, 3, 4, 5 ]
const f = (total, item) => total + item
const base = 0

reduce(f, base, arr)
  => 15

// partial application
const sum = reduce(f, base)
sum(arr)
  => 15

sum([ 2, 3, 4, 5, 6 ])
  => 20

flatten

const arr = [ 1, 2, 3, [ 4, 5 ] ]

flatten(arr)
  => [ 1, 2, 3, 4, 5 ]

lift

const arr = [ 1, 2, 3, 4, 5 ]

lift(arr, 2)
  => 3

lift(arr, 'this is not an index!')
  => null

// partial application
const lifted = lift(arr)
lifted(0)
  => 1

lifted('also not an index!')
  => null

flatmap

(see flatten && map)

to build

$ npm run build

to run tests

$ npm run test

to typecheck

$ npm run flow

futil's People

Contributors

adccb avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.