This repository was archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
This repository was archived by the owner on Jan 26, 2022. It is now read-only.
Other useful common functions #17
Copy link
Copy link
Closed
Labels
enhancementNew feature or requestNew feature or request
Description
In https://github.com/esfx/esfx/blob/master/packages/fn/src/common.ts I've gathered a bunch of useful one-off functions for FP-style development. If we are considering adding .constant
and .identity
, perhaps some of those make sense as well. There are at least two that might be worth adding to the standard library:
noop()
— Always returnsundefined
.- This is essentially the same as
Function.prototype
, though it might be useful to have a separate copy.
- This is essentially the same as
lazy(factory, ...args)
— Creates a function with the following properties:- When invoked for the first time, calls
factory
with the supplied arguments and returns the result - Any subsequent invocation returns the result of the first invocation.
- When invoked for the first time, calls
There's a bunch of other ones in there, but most are very niche:
incrementer
—(start = 0, step = 1) => () => start += step
decrementer
—(start = 0, step = 1) => () => start -= step
property
—key => object => object[key]
propertyWriter
—key => (object, value) => object[key] = value
invoker
—(key, ...args) => (object, ...rest) => object[key](...args, ...rest)
caller
—(...args) => (func, ...rest) => func(...args, ...rest)
allocater
—(...args) => (ctor, ...rest) => new ctor(...args, ...rest)
factory
—(ctor, ...args) => (...rest) => new ctor(...args, ...rest)
uncurryThis
—Function.prototype.bind.bind(Function.prototype.call)
complement
—f => (...args) => !f(...args)
both
—(a, b) => (...args) => a(...args) && b(...args)
either
—(a, b) => (...args) => a(...args) || b(...args)
fallback
—(a, b) => (...args) => a(...args) ?? b(...args)
celmaun
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request