GithubHelp home page GithubHelp logo

zhangaz1 / static-land Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fantasyland/static-land

0.0 1.0 0.0 120 KB

Specification for common algebraic structures in JavaScript based on Fantasy Land

License: MIT License

JavaScript 100.00%

static-land's Introduction

Static Land

This is a specification for common algebraic structures in JavaScript based on Fantasy Land.

Difference from Fantasy Land

Fantasy Land uses methods to define interfaces that a type must implement in order to support a particular Algebra. For example values of a type that implements the Monoid algebra must have fantasy-land/empty and fantasy-land/concat methods on them.

Static Land takes a different approach. Instead of methods, we use static functions, that are grouped together in modules.

For example, here is an Addition module that uses numbers as values and satisfies the Monoid algebra requirements:

const Addition = {

  empty() {
    return 0
  },

  concat(a, b) {
    return a + b
  },

}

Pros

  • No name clashes. Since a module is just a collection of functions that don't share any namespace we don't have problems with name clashes.
  • We can implement many modules for one type, therefore we can have more than one instance of the same Algebra for a single type. For example, we can implement two Monoids for numbers: Addition and Multiplication.
  • We can implement modules that work with built-in types as values (Number, Boolean, Array, etc).

Cons

  • We have to pass around modules when we write generic code. In Fantasy Land most of generic code can be written using only methods, only if we need methods like of or empty we might need to pass the type representative. (This can be fixed!)

How to add compatibility with Static Land to your library

Simply expose a module that works with types that your library provides or with types defined in another library or with native types like Array.

Modules don't have to be simple JavaScript objects; they can also be constructors if desired. The only requirements are:

  • this object contains some static methods from Static Land; and
  • if it contains a method with one of the names that Static Land reserves, that method must be the Static Land method (obey laws etc).

Example 1. Static Land module for Array

const SArray = {

  of(x) {
    return [x]
  },

  map(fn, arr) {
    return arr.map(fn)
  },

  chain(fn, arr) {
    // ...
  },

}

export {SArray}

Example 2. Static Land module as a Class

class MyType {

  constructor() {
    // ...
  }

  someInstanceMethod() {
    // ...
  }

  static someNonStaticLandStaticMethod() {
    // ...
  }


  // Static Land methods

  static of(x) {
    // ...
  }

  static map(fn, value) {
    // ...
  }

}

export {MyType}

Example 3. Static Land module as ECMAScript modules

// mytype.js

// Static Land methods

export function of(x) {
  // ...
}

export function map(fn, value) {
  // ...
}

Import as

import * as MyType from "./mytype" // MyType is now a Static Land module

Compatible libraries

We have a list in the wiki. Feel free to add your library there.

static-land's People

Contributors

rpominov avatar 1point7point4 avatar benji6 avatar trysound avatar davidchambers avatar kwijibo avatar shard avatar rjmk avatar paldepind avatar pelotom avatar mvorwieger avatar npmcdn-to-unpkg-bot 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.