diff options
| author | Akshay Nair <phenax5@gmail.com> | 2022-01-13 23:05:21 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2022-01-13 23:05:21 +0530 |
| commit | d10c241332cf14a0b9171739a76cbdafdbdf2826 (patch) | |
| tree | 280c38b4fa792e4556188458bd2b43f2a2e5aa46 /src | |
| parent | 11bb9b17ad84e0c07aaa50ce65411a8adf692685 (diff) | |
| download | elxr-d10c241332cf14a0b9171739a76cbdafdbdf2826.tar.gz elxr-d10c241332cf14a0b9171739a76cbdafdbdf2826.zip | |
feat(eval): implements sequence
Diffstat (limited to 'src')
| -rw-r--r-- | src/eval/index.ts | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/src/eval/index.ts b/src/eval/index.ts index d0d7363..efede36 100644 --- a/src/eval/index.ts +++ b/src/eval/index.ts @@ -1,5 +1,5 @@ import { identity, pipe } from 'fp-ts/function' -import { takeLeftWhile, zip } from 'fp-ts/lib/Array' +import { filter, takeLeftWhile, zip, zipWith } from 'fp-ts/lib/Array' import { chain, getOrElseW, @@ -35,9 +35,9 @@ const checkExpr = <T>( index: number, skip: ( n: index, - ) => (m: MatchGroupIndexed<T | T[]>[]) => MatchGroupIndexed<T | T[]>[] = _ => + ) => (m: MatchGroupIndexed<any>[]) => MatchGroupIndexed<any>[] = _ => identity, -): MatchGroupIndexed<T | T[]>[] => { +): MatchGroupIndexed<any>[] => { return pipe( expr, match<MatchGroupIndexed<any>[], Expr>({ @@ -111,6 +111,26 @@ const checkExpr = <T>( ) }, + Sequence: ({ exprs }) => { + const getGroups = () => { + if (exprs.length > list.length) return [] + + const indexed = <T>(ls: T[]): Array<[number, T]> => ls.map((x, i) => [i, x]) + const result = pipe( + zipWith(exprs, indexed(list), (expr, [i, val]) => checkExpr(expr, val, list, index + i)), + filter(matches => !!matches.length), + ) + if (result.length !== exprs.length) return [] + + return [group(result, index)] + }; + + return pipe( + getGroups(), + skip(exprs.length || 1), + ) + }, + _: _ => { throw new Error(`TODO: ${expr.tag} not implemented for match`) }, }), ) |
