Giter Site home page Giter Site logo

proposal-function-composition's Introduction

Function composition operators for JS

Currently, there's a lot of talk about a possible pipeline operator being added. My experience says that it's also quite often we need to compose functions, too. And I'm not alone - there's mountains of library precedent:

And of course, there's quite a bit of precedent in other languages, too:

Proposal

I propose a single left-associative operator for composition: a |>> b. This is roughly equivalent to (...args) => g(f(...args)), but to give it added flexibility and power, you can also construct the result and it invokes the left side accordingly. (The result is still coerced to an object in the end in this case.) So in reality, the actual desugaring would be this:

const _apply = Reflect.apply
const _construct = Reflect.construct
const _defineProperty = Object.defineProperty

function _compose(a, b) {
  if (!%IsCallable(a) && !%IsConstructible(a)) {
    throw new TypeError("Expected left hand side to be callable or constructible.")
  }

  if (!%IsCallable(b)) {
    throw new TypeError("Expected right hand side to be callable.")
  }

  const f = (function () {
    return b(
      new.target === void 0
        ? _apply(a, this, arguments)
        : _construct(a, new.target, arguments)
    )
  })

  _defineProperty(f, "name", {value: ""})
  _defineProperty(f, "length", {value: a.length})
  _markAsNativeSomehow(f)

  return f
}

// const composed = a |>> b |>> c
const composed = _compose(_compose(a, b), c)

Syntax concerns

The syntax is meant to be based on https://github.com/tc39/proposal-pipeline-operator, so any discrepancy from that is not intended, and any concerns with it also carry over to this (like async/await and optional chaining).

proposal-function-composition's People

Contributors

dead-claudia avatar

Stargazers

Anne Thorpe avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.