Code Monkey home page Code Monkey logo

brn's Introduction

Strongly Typed Functional Branching

brn exports only one function that takes a test, a left, and a right function and return a combined one that will run left if test returns truthy and right otherwise

Example

const brn = require('brn');

const isOdd = x => x % 2;

const left = x => `${x} is odd`;
const right = x => `${x} is even`;

const fn = brn(isOdd, left, right);

console.log(fn(2)); // returns 2 is even
console.log(fn(1)); // returns 1 is odd

TypeScript

import brn from 'brn'

const isOdd = (x: number) => x % 2;

const fn = brn(
  isOdd,
  x => `${x} is odd` as const,
  x => `${x} is even` as const
);

expect(fn(2, 'two')).toBe('2 is even'); // returns 2 is even
expect(fn(1, 'one')).toBe('1 is odd'); // returns 1 is odd

In this example, parameter x in the "left" and "right" functions is typed as number. fn also has return type of `${number} is even` | `${number} is odd`

// another example
function always<X extends string>(x: X) {
  return () => x;
}
const fn = brn(
  (x: number) => x >= 10,
  always(`double digit`),
  brn(
    x => x < 0,
    always('negative'),
    always('single digit')
  )
)

In this example, return type of fn is one of "single digit", "double digit" or "negative".

brn's People

Contributors

tungv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

brn's Issues

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.