JSFiddle - React, Tailwind, and code Playground
JavaScript
"use strict";
// Library of functions:
/**
* Function that resolves the output of a function.
*/
let $$ = (val) => {
while (typeof val === "function") {
val = val();
}
return val;
}
/**
* Functional if
*
* The $ suffix is a convention I use to show that it is "functional"
* style, and I need to use $$() to "unwrap" the value when I need it.
*/
let if$ = (test, whenTrue, otherwise) => () =>
$$(test) ? whenTrue : otherwise;
/**
* Functional lt (less then)
*/
let lt$ = (leftSide, rightSide) => () =>
$$(leftSide) < $$(rightSide)
/**
* Functional add (+)
*/
let add$ = (leftSide, rightSide) => () =>
$$(leftSide) + $$(rightSide)
// My hand compiled Charm script:
/**
* Functional fib
*/
/*
CHARM CODE:
fib: (n) => if (
n < 2
n
fib(n-1) + fib(n-2)
)
COMPILED TO:
*/
let fib$ = (n) => if$(
lt$(n, 2),
() => n,
() => add$(fib$(n-2), fib$(n-1))
)
console.log(fib$(5)); // () => charm_resolve(test) ? whenTrue : otherwise
// When you need the value, just wrap it with $$()
console.log($$(fib$(5)))