S-expressions: Difference between revisions

m
→‎JavaScript :: Functional: Some reduction and tidying.
m (→‎JavaScript :: Functional: Some reduction and tidying.)
Line 3,792:
// A tuple of (parsed trees, residual tokens)
// derived from a list of tokens.
until(finished)(readToken)([
Tuple([])(, tokens)
]);
 
 
// finished :: ([Expr], [String]) -> Bool
const finished = ([, tknstokens]) =>
0// ===True tkns.lengthif ||no ")"tokens ===remain, tkns[0];or the next
// closes a sub-expression.
0 <=== xstokens.length ?|| (")" === tokens[0];
 
 
// readToken :: ([Expr], [String]) -> ([Expr], [String])
const readToken = ([exprsxs, tknstokens]) => {
0// <A tkns.lengthtuple ?of (()enriched =>expressions {and
// depleted const [t, tokens...ts] = tkns;
const [token, ...ts] = tokens;
 
// SubforestsAn open arebracket introducedintroduces byrecursion brackets,over
// a sub-expression to returndefine "("a === t ? (sub-list.
return "(" === token ? (() => bimap({
const [expr, xsrest] => exprs.concatparseExpr([xs]ts);
)(tail)(parseExpr(ts))
 
// and conclude where brackets close.
) : ")" === t ? (
Tuple(exprs)(ts)
 
// Other tokens are appended leaves.
) : Tuple(
exprs.concat(atom(t))
)(ts);
})() : Tuple(exprs)(tkns);
 
return [xs.concat([expr]), rest.slice(1)];
})() : ")" === ttoken ? (
)[xs, : Tuple(token]
) : exprs[xs.concat(atom(ttoken)), ts];
});
 
// ------------------- ATOM PARSER -------------------
Line 3,883 ⟶ 3,880:
 
// --------------------- GENERIC ---------------------
 
// Tuple (,) :: a -> b -> (a, b)
const Tuple = a =>
b => ({
type: "Tuple",
"0": a,
"1": b,
length: 2,
*[Symbol.iterator]() {
for (const k in this) {
if (!isNaN(k)) {
yield this[k];
}
}
}
});
 
 
// bimap :: (a -> b) -> (c -> d) -> (a, c) -> (b, d)
const bimap = f =>
// Tuple instance of bimap.
// A tuple of the application of f and g to the
// first and second values respectively.
g => ([a, b]) => Tuple(f(a))(g(b));
 
 
// even :: Int -> Bool
Line 3,922 ⟶ 3,894:
f => {
const go = x =>
p(x) ? x : go(f(x));
x
) : go(f(x));
 
return go;
};
 
 
// tail :: [a] -> [a]
const tail = xs =>
// A new list consisting of all
// items of xs except the first.
0 < xs.length ? (
xs.slice(1)
) : undefined;
 
return main();
9,655

edits