Skip to content

Instantly share code, notes, and snippets.

@renanpvaz
Created September 4, 2017 19:40
Show Gist options
  • Save renanpvaz/d829ccd5fc64b125f44099b3c4e4c2ea to your computer and use it in GitHub Desktop.
Save renanpvaz/d829ccd5fc64b125f44099b3c4e4c2ea to your computer and use it in GitHub Desktop.
helper to extract functions from prototypes
const decoupleMethods = (Type, ...names) => names.map(
name => ({ [name]: (a, b) => Type.prototype[name].call(b, a) }) // could be curried
).reduce((acc, next) => Object.assign({}, acc, next))
const { map, filter, forEach, find } = decoupleMethods(
Array,
'map',
'filter',
'forEach',
'find'
)
find(n => n === 1, [4, 3, 2, 1]) // 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment