• Stars
    star
    766
  • Rank 57,082 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 8 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Specification for common algebraic structures in JavaScript based on Fantasy Land

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.

More Repositories

1

fantasy-land

Specification for interoperability of common algebraic structures in JavaScript
JavaScript
9,863
star
2

daggy

Library for creating tagged constructors.
JavaScript
696
star
3

fantasy-lenses

Composable, immutable getters and setters.
JavaScript
108
star
4

fantasy-promises

JavaScript
89
star
5

fantasy-birds

port of the haskell package Data.Aviary.Birds. Every thing for your combinatory needs.
JavaScript
85
star
6

fantasy-combinators

Common combinators.
JavaScript
66
star
7

fantasy-io

IO control structure.
JavaScript
37
star
8

fantasy-maybes

Option data structure.
JavaScript
36
star
9

sweet-fantasies

sweet.js macros for Fantasy Land compatible structures.
JavaScript
34
star
10

fantasy-laws

Property-based tests for FL-compatible ADTs
JavaScript
33
star
11

fantasy-monoids

A collection of monoids
JavaScript
28
star
12

fantasy-frees

Free monad
JavaScript
25
star
13

fantasy-readers

Reader control structure.
JavaScript
24
star
14

fantasy-eithers

Either data structure.
JavaScript
21
star
15

fantasy-identities

Identity data structure.
JavaScript
20
star
16

fantasy-sorcery

JavaScript
19
star
17

fantasy-check

QuickCheck Library using Fantasy-Land
JavaScript
18
star
18

fantasy-states

State control structure.
JavaScript
16
star
19

fantasy-examples

Example uses of Fantasy Land projects.
JavaScript
15
star
20

fantasy-tuples

Tuple data structures.
JavaScript
15
star
21

ECMAScript-proposals

Meta repository for discussions regarding ECMAScript proposals
12
star
22

fantasy-writers

Writer control structure.
JavaScript
10
star
23

fantasy-helpers

Helper functions
JavaScript
7
star
24

fantasy-validations

Validation data structure.
JavaScript
6
star
25

fantasy-stores

Store control structure.
JavaScript
5
star
26

unified-specification

Temporary repository for unified static-land and fantasy-land specification
5
star
27

function-prototype-map

4
star
28

fantasy-profunctors

Profunctor
JavaScript
3
star
29

fantasy-these

Data type isomorphic to α ∨ β ∨ (α ∧ β)
JavaScript
3
star
30

fantasy-consts

JavaScript
2
star
31

fantasy-equality

Library to enable equality of different types.
JavaScript
2
star
32

fantasy-cofrees

Cofree comonad structure.
JavaScript
2
star
33

fantasy-coproducts

Coproducts
JavaScript
2
star