Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including fp-array with all npm packages installed. Try it out:

var fpArray = require("fp-array")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

fp-array v1.0.2

More declarative and convenient functions than in Array.prototype

fp-array

fp-array is a group of functions to do declarative programming in a more convenient way than in Array.prototype

Build Status Standard - JavaScript Style Guide

Installation

$ npm install fp-array

Syntax

const map = require('fp-array').map
const filter = require('fp-array').filter
const reduce = require('fp-array').reduce
const arr = [...]

map(arr)
  .with(callback)
  [.and(callback2)]
  [...]
  .result()

filter(arr)
  .is(callback)
  [.and(callback2)]
  [...]
  .result()

reduce(arr, callback[, initialValue])

Examples

#map()

const arr = [1, 2, 3]
// callbacks
const increment = n => n + 1
const double = n => n * 2

map(arr)
  .with(increment)
  .and(double)
  .and(String)
  .result() // ['4', '6', '8']

arr // [1, 2, 3]

Notes

  • Iterates only one time over arr
  • Inmutable: arr has not mutated, a new Array is returned when result()
  • Declarative: Easy to read syntax

#filter()

const arr = [1, 'a', true, 6, 0, 4, undefined, 10]

const number = n => typeof n === 'number'
const greaterThan4 = n => n > 4
const even = n => n % 2 === 0

filter(arr)
  .is(number)
  .and(even)
  .and(greaterThan4)
  .result() // [6, 10]

arr // [1, 'a', true, 6, 0, 4, undefined, 10]

Same notes than before applies.

#reduce()

const arr = [1, 2, 3]
reduce(
  arr,
  (acc, currentValue) => acc + currentValue,
  5
) // 11

reduce() works exactly like the original except now is outside the Array.prototype

More examples in tests.

Have any idea ?

Open an issue or say hi on twitter: @juliomatcom

MIT Licensed

Copyright (c) 2016 Julio César Martín

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free