1.2.1 • Published 5 years ago

@masaeedu/infix v1.2.1

Weekly downloads
1
License
MIT
Repository
-
Last release
5 years ago

Infix

Summary

A simple trick for applying a bag of static functions where the data goes last as infix functions.

Usage

const { infix, uncurry } = require("@masaeedu/infix");

const Int = {
  "+": x => y => y + x,
  "-": x => y => y - x,
  "*": x => y => y * x,
  "/": x => y => y / x
};

const result = infix(Int) (1) ["+"] (2) ["+"] (5) ["*"] (5) ["+"] (2) .unwrap;
// => 42

Properties

The expression:

infix(F)(a)
  .f(b)
  .g(c).unwrap

desugars to:

F.g(c)(F.f(b)(a))

Note that this only works when F.f, F.g etc. are binary functions with the data as the second argument.

Some functions may not fit this mould. Functions of arbitrary arity are supported using uncurrying. The expression:

infix(F)(a)
  .f(b)
  [uncurry](g)([c, d])
  .h(e).unwrap

desugars to:

F.h(e)(
  F.g(c)(d)(
    F.f(b)(
      a)))

Disclaimer

Use of this library may result in you being instantly fired. I accept no responsibility.