All Projects → quadrupleslap → Union Js

quadrupleslap / Union Js

Licence: mit
🏷️ Tagged unions for vanilla JavaScript!

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Union Js

Vanilla Lazyload
LazyLoad is a lightweight, flexible script that speeds up your website by deferring the loading of your below-the-fold images, backgrounds, videos, iframes and scripts to when they will enter the viewport. Written in plain "vanilla" JavaScript, it leverages IntersectionObserver, supports responsive images and enables native lazy loading.
Stars: ✭ 6,596 (+27383.33%)
Mutual labels:  vanilla-js
Gifcurry
😎 The open-source, Haskell-built video editor for GIF makers.
Stars: ✭ 830 (+3358.33%)
Mutual labels:  functional-programming
Es Cqrs Shopping Cart
A resilient and scalable shopping cart system designed using Event Sourcing (ES) and Command Query Responsibility Segregation (CQRS)
Stars: ✭ 19 (-20.83%)
Mutual labels:  functional-programming
Deeplearning.scala
A simple library for creating complex neural networks
Stars: ✭ 745 (+3004.17%)
Mutual labels:  functional-programming
Ergonomica
🖥️ a cross-platform modern shell.
Stars: ✭ 815 (+3295.83%)
Mutual labels:  functional-programming
Push State
Turn static web sites into dynamic web apps.
Stars: ✭ 16 (-33.33%)
Mutual labels:  vanilla-js
Frameless
Expressive types for Spark.
Stars: ✭ 717 (+2887.5%)
Mutual labels:  functional-programming
J8plus
Library containing useful tools for Java 8
Stars: ✭ 23 (-4.17%)
Mutual labels:  functional-programming
Satysfi
A statically-typed, functional typesetting system
Stars: ✭ 815 (+3295.83%)
Mutual labels:  functional-programming
Kea
Composable Functional Programming in R
Stars: ✭ 18 (-25%)
Mutual labels:  functional-programming
Fp Core.rs
A library for functional programming in Rust
Stars: ✭ 772 (+3116.67%)
Mutual labels:  functional-programming
Egison
The Egison Programming Language
Stars: ✭ 800 (+3233.33%)
Mutual labels:  functional-programming
Kaur
A bunch of helper functions to ease the development of your applications.
Stars: ✭ 17 (-29.17%)
Mutual labels:  functional-programming
Lambda
Functional patterns for Java
Stars: ✭ 737 (+2970.83%)
Mutual labels:  functional-programming
Txmonad
A toy xmonad
Stars: ✭ 22 (-8.33%)
Mutual labels:  functional-programming
Moon
🌙 The minimal & fast library for functional user interfaces
Stars: ✭ 6,058 (+25141.67%)
Mutual labels:  functional-programming
Grin
GRIN is a compiler back-end for lazy and strict functional languages with whole program optimization support.
Stars: ✭ 834 (+3375%)
Mutual labels:  functional-programming
Search Ui
🔍 A set of UI components to build a fully customized search!
Stars: ✭ 24 (+0%)
Mutual labels:  vanilla-js
Proppy
Functional props composition for UI components (React.js & Vue.js)
Stars: ✭ 921 (+3737.5%)
Mutual labels:  functional-programming
Serverless Aws Lambda Node Postgres
Serverless AWS Lambda with Node.js,Postgres Rest API with Sequelize.
Stars: ✭ 18 (-25%)
Mutual labels:  functional-programming

tagged-union

Tagged unions, in vanilla JavaScript.

npm install tagged-union

What's a tagged union?

Tagged unions are the idea that a thing can be one of many things, but you want to keep track of which of those things the thing is. They're simple and really useful.

I definitely understood all of that. So, how does it work?

Well, let's go through an example with some variables that can hold either Good or Bad people, where:

  • Good people have chocolate.
  • Bad people have toast with a vegetable (one vegetable! 😦)
const Union = require('tagged-union');

// Define the things that a person can be.

const Person = new Union(['Good', 'Bad']);

// Make some people!

let alice = Person.Bad('burnt', 'broccoli');
let bob = Person.Good('lindt');

console.log(alice.toString()); // => Bad(burnt, broccoli)

// Good people go to heaven, bad people go to hell.

function afterlife(person) {
  // Note that match clauses can, but don't have to, return a value.
  
  return person.match({
    Good(chocolate) {
      return 'heaven';
    },
    Bad(toast, vegetable) {
      return 'hell';
    },
    _() {
      // A function named '_' catches every kind without its own clause,
      // which in this case is none of them.
      throw new Error('We can\'t get here!');
    }
  });
}

console.log(afterlife(alice)); // => hell
console.log(afterlife(bob)); // => heaven

// Oh, and did I mention that they can be serialized?

let bobInABox = JSON.stringify(bob);
let bobClone = Union.fromJSON(JSON.parse(bobInABox));

console.log(bobClone.toString()); // => Good(lindt)

That's cool and all, but why's it useful again?

Your SPA only has a couple of screens, a result can either be successful or an error, trees are made of nodes and leaves, and there are only half a dozen ways you can pay for something online. The (programming) world is full of discriminated unions! Without the proper abstraction, you end up with manually tagged unions, which are the worst kind, because you have to manually track what's actually in the object. Here, you can just match on it, and it's all rainbows and sunshine.

🌈 and ☀️.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].