@rgeraldporter/

Maybe Monad Example

Nodejs

No description

fork
loading
Files
  • index.js
  • Maybe.js

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.3975278230651018","path":"index.js","file":{"path":"index.js","content":{"asEncoding":{"base64":"Y29uc3QgeyBNYXliZSB9ID0gcmVxdWlyZSgnLi9NYXliZScpOwoKY29uc3QgZmFocmVuaGVpdFRvQ2Vsc2l1cyA9IGEgPT4gKGEgLSAzMikgKiAwLjU1NTY7Cgpjb25zdCByZWFkaW5nMSA9IDE1Owpjb25zdCByZWFkaW5nMiA9IG51bGw7Cgpjb25zdCB0ZW1wMUMgPSBNYXliZS5vZihyZWFkaW5nMSkKCQkJCQkubWFwKGZhaHJlbmhlaXRUb0NlbHNpdXMpOwoJCQkJCQpjb25zb2xlLmxvZyh0ZW1wMUMuaW5zcGVjdCgpKTsKLy8gPiBKdXN0KC05LjQ0NDQpCgkJCQkJCmNvbnN0IHRlbXAyQyA9IE1heWJlLm9mKHJlYWRpbmcyKQoJCQkJCS5tYXAoZmFocmVuaGVpdFRvQ2Vsc2l1cyk7Cgpjb25zb2xlLmxvZyh0ZW1wMkMuaW5zcGVjdCgpKTsKLy8gPiBOb3RoaW5nKCkKCmNvbnN0IGRpc3BsYXkgPSBhID0+IHsKCWNvbnNvbGUubG9nKGEpOwoJcmV0dXJuIGE7Cn07CgpNYXliZS5vZihyZWFkaW5nMSkKCS5tYXAoZmFocmVuaGVpdFRvQ2Vsc2l1cykKCS5mb3JrKAoJCV8gPT4gZGlzcGxheSgnRVJSIScpLAoJCXQgPT4gZGlzcGxheShgJHt0fcKwQ2ApIC8vIHdpbGwgcmVhZCBgLTkuNDQ1MsKwQ2AKCSk7CgkJCQkJCk1heWJlLm9mKHJlYWRpbmcyKQoJLm1hcChmYWhyZW5oZWl0VG9DZWxzaXVzKQoJLmZvcmsoCgkJXyA9PiBkaXNwbGF5KCdFUlIhJyksIC8vIHdpbGwgcmVhZCBgRVJSIWAKCQl0ID0+IGRpc3BsYXkoYCR7dH3CsENgKQoJKTsKCmNvbnN0IHRlbXAzQyA9IE1heWJlLm9mKHJlYWRpbmcxKQoJLm1hcChmYWhyZW5oZWl0VG9DZWxzaXVzKQoJLmZvcmsoCgkJXyA9PiBkaXNwbGF5KCdFUlIhJyksCgkJdCA9PiBkaXNwbGF5KGAke3R9wrBDYCkKCSk7CgkKY29uc29sZS5sb2codGVtcDNDKQovLyA+ICItOS40NDUywrBDIg=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const { Maybe } = require('./Maybe');

const fahrenheitToCelsius = a => (a - 32) * 0.5556;

const reading1 = 15;
const reading2 = null;

const temp1C = Maybe.of(reading1)
					.map(fahrenheitToCelsius);
					
console.log(temp1C.inspect());
// > Just(-9.4444)
					
const temp2C = Maybe.of(reading2)
					.map(fahrenheitToCelsius);

console.log(temp2C.inspect());
// > Nothing()

const display = a => {
	console.log(a);
	return a;
};

Maybe.of(reading1)
	.map(fahrenheitToCelsius)
	.fork(
		_ => display('ERR!'),
		t => display(`${t}°C`) // will read `-9.4452°C`
	);
					
Maybe.of(reading2)
	.map(fahrenheitToCelsius)
	.fork(
		_ => display('ERR!'), // will read `ERR!`
		t => display(`${t}°C`)
	);

const temp3C = Maybe.of(reading1)
	.map(fahrenheitToCelsius)
	.fork(
		_ => display('ERR!'),
		t => display(`${t}°C`)
	);
	
console.log(temp3C)
// > "-9.4452°C"