Skip to content
This repository was archived by the owner on Jan 26, 2022. It is now read-only.
This repository was archived by the owner on Jan 26, 2022. It is now read-only.

Other useful common functions #17

@rbuckton

Description

@rbuckton

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 returns undefined.
    • This is essentially the same as Function.prototype, though it might be useful to have a separate copy.
  • 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.

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
  • propertykey => object => object[key]
  • propertyWriterkey => (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)
  • uncurryThisFunction.prototype.bind.bind(Function.prototype.call)
  • complementf => (...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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions