0.1.0 • Published 5 years ago

@aprillion/algebraic-effects v0.1.0

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

JavaScript Algebraic Effects

Test implementation of concepts from https://overreacted.io/algebraic-effects-for-the-rest-of-us/ using only the language features available in 2019.

NOT intended for production code, just exploration of the concepts!

Usage

npm i @aprillion/algebraic-effects
import {perform, withPerform, resumeWith} from 'algebraic-effects'

function getName(user) {
  let name = user.name
  if (name === null) {
    name = perform('ask_name')
  }
  return name
}

function makeFriends(user1, user2) {
  user1.friendNames.push(getName(user2))
  user2.friendNames.push(getName(user1))
}

const arya = {name: null, friendNames: []}
const gendry = {name: 'Gendry', friendNames: []}
withPerform(
  () => {
    makeFriends(arya, gendry)
  },
  effect => {
    if (effect === 'ask_name') {
      resumeWith('Arya Stark')
    }
  },
)
// expect(arya.friendNames).toEqual(['Gendry'])
// expect(gendry.friendNames).toEqual(['Arya Stark'])

See index.test.js for more examples.

Contribution

GitHub Issues are welcome, but no promisses about response time.