mondo: add .. operator
This commit is contained in:
parent
55a5f1d62a
commit
3505732afa
3 changed files with 12 additions and 2 deletions
|
|
@ -21,7 +21,7 @@ strudelScope.leaf = (token, scope) => {
|
|||
|
||||
strudelScope.call = (fn, args, name) => {
|
||||
const [pat, ...rest] = args;
|
||||
if (!['seq', 'cat', 'stack', ':'].includes(name)) {
|
||||
if (!['seq', 'cat', 'stack', ':', '..'].includes(name)) {
|
||||
args = [...rest, pat];
|
||||
}
|
||||
return fn(...args);
|
||||
|
|
@ -30,9 +30,18 @@ strudelScope.call = (fn, args, name) => {
|
|||
strudelScope['*'] = fast;
|
||||
strudelScope['/'] = slow;
|
||||
|
||||
// : operator
|
||||
const tail = (pat, friend) => pat.fmap((a) => (b) => (Array.isArray(a) ? [...a, b] : [a, b])).appLeft(friend);
|
||||
strudelScope[':'] = tail;
|
||||
|
||||
// .. operator
|
||||
const arrayRange = (start, stop, step = 1) =>
|
||||
Array.from({ length: Math.abs(stop - start) / step + 1 }, (_, index) =>
|
||||
start < stop ? start + index * step : start - index * step,
|
||||
);
|
||||
const range = (min, max) => min.squeezeBind((a) => max.bind((b) => seq(...arrayRange(a, b))));
|
||||
strudelScope['..'] = range;
|
||||
|
||||
export function mondo(code, offset = 0) {
|
||||
if (Array.isArray(code)) {
|
||||
code = code.join('');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue