Coder Social home page Coder Social logo

multimethod-js's Introduction

multimethod-js

Clojure inspired multimethods for JavaScript

API

defMulti

Creates new multimethods.

Takes a dispatching function, primitive or no value as its only argument. Returns a multimethod.

  • Primitive implies a deep equality check dispatching function
  • No value implies a deep equality check when dispatching function is called with a primitive or an instanceof check when called with a non-primitave.
  const area = defmulti(obj => obj.type);

defMethod

Creates and installs a new method of multimethod associated with a dispatch value.

Takes a multimethod as its first argument, a dispatch value second, and a function to be invoked with the argument(s) supplied to the multimethod's dispatching function third.

Alternatively, a direct value to be returned on match can be passed as the second argument.

  defmethod(area, 'rect', r => r.width * r.height);

getMethod

Given a multimethod and a dispatch value, returns the method that applies to that value, or null if none apply and no default was set.

  let rectArea = getmethod(area, 'rect');

getAllMethods

Given a multimethod, returns a collection of dispatch values and associated dispatch functions.

  let allShapeAreas = getAllMethods(area);

removeMethod

Removes the method of multimethod associate with a dispatch value.

  removeMethod(area, 'rect');

removeAllMethods

Removes all of the methods of multimethod.

  removeAllMethods(area);

Example

import {defmulti, defmethod, default} from 'multimethod-js';

const area = defmulti(obj => obj.type);

defmethod(area, 'rect', r => r.width * r.height);

defmethod(area, 'circle', c => Math.PI * c.radius * c.radius);

defmethod(area, default, o => throw new Error(`No area calc for ${o.type}`));

const rect = {
  type: 'rect',
  width: 40,
  height: 40
};

area(rect); // => 1600

removeMethod(area, 'circle')

area(rect) // => Error: "No area calc for rect"

Alt Chaining Example

import {defmulti, default} from 'multimethod-js';

const area =
  defmulti(obj => obj.type)
    .defmethod('rect', r => r.width * r.height)
    .defmethod('circle', c => Math.PI * c.radius * c.radius)
    .defmethod(default, o => throw new Erorr(`No area calc for ${o.type}`));

area(rect); // => 1600

multimethod-js's People

Contributors

colindresj avatar

Watchers

 avatar  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.