JSFiddle - React, Tailwind, and code Playground

by mcsf

CSS

body { font-family: monospace }

Babel + JSX

const p = s => document.body.innerText += s + '\n'
const l = p //console.log.bind( console )

class Superposition {
	static of = ( outcomes ) =>
  	new Superposition( outcomes )
    
	constructor( outcomes ) {
  	this.outcomes = outcomes
  }
	
	combine( otherSuperposition ) {
		return Superposition.of(
			flatten( this.outcomes.map(a =>
				otherSuperposition.outcomes.map(b =>
					[ a, b ]
			) ) )
		)
	}
  
  toString() {
  	return `{ ${this.outcomes.map(JSON.stringify)} }`
  }
}

const resolve = x => {
	if ( ! ( x instanceof Superposition ) )
		return x

	const i = Math.floor( Math.random() * x.outcomes.length )
	const chosen = x.outcomes[ i ]
	return resolve( chosen )
}

const flatMap = ( f, xs ) =>
	flatten( xs.map( f ) )
  
const flatten = ( [ xs, ys ] ) =>
	[ ...xs, ...ys ]

//

const nested =
	Superposition.of( [
  	Superposition.of( [ 1, 2 ] ),
    Superposition.of( [ 3, 4 ] )
  ] )

l( '### nested ->' )
l( resolve( nested ) )
l( resolve( nested ) )
l( resolve( nested ) )
l( resolve( nested ) )

//

l( '### combining ( 1; 2 ) and ( 3; 4 ) ->' )
l( Superposition.of( [ 1, 2 ] )
	.combine( Superposition.of( [ 3, 4 ] ) ) )
l( resolve(
	Superposition.of( [ 1, 2 ] )
		.combine( Superposition.of( [ 3, 4 ] ) ) ) )