/fewer-lambdas

yet another fewer lambdas

Primary LanguageJavaScript

ƒewer-lambdas

ƒ references

This steals heavily from the folowing:

phrogz's fewer lambdas

roadtolarissa's even fewer lambdas

d3-jetpacks's implementation

ƒ usage

var ƒ = require('fewer-lambdas')

Note the function name ƒ is a legal JavaScript identifier. This character is easy to type in OSX by using option + f.

ƒ takes a string|number and returns a function that takes an object and returns whatever property the string is named.

This lets you replace function(d){ return ... } ECMAScript 5 lambda function syntax with the shorter easy to use ƒ(...) function.

ƒ Examples

Identity function

Given an array

[1,2,3,4,...].map(function(d){ return d})

becomes

[1,2,3,4,...].map(ƒ())

Object properties

Given an array of objects

var data = [
  {
    key:'0',
    value:0.00
  },
  {
    key:'1',
    value:1.11
  },
  {
    key:'2',
    value:2.22
  },
  ...
]

The following

data.map(function(d){ return d.value})

becomes

data.map(ƒ('value'))

Function composition

The following

data.map(function(d){ return parseInt(d.value)})

becomes

data.map(ƒ('value',parseInt))

Fat arrow

Combine this with Es6 fat arrow functions and things get even shorter

data.map(ƒ('value', JSON.stringify, d => d.split('.'), d => d.join(''), d => +d))