JSFiddle - React, Tailwind, and code Playground

by dp0ch

JavaScript

function Just(value) {
	function bind(f) {
  	return f(val);
  }
  
  return {
  	bind,
    toString: () => `Just(${value})`,
  };
}

const Nothing = {
	bind: () => Nothing,
  toString: () => 'Nothing',
}


function maybeSomething(val) {
	return val > 0.5 ? Just(Math.random()) : Nothing;
}

console.log(
	maybeSomething(Math.random())
		.bind(maybeSomething)
  	.bind(maybeSomething)
  	.toString()
);