From d10c241332cf14a0b9171739a76cbdafdbdf2826 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Thu, 13 Jan 2022 23:05:21 +0530 Subject: feat(eval): implements sequence --- src/eval/index.ts | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'src/eval/index.ts') 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 = ( index: number, skip: ( n: index, - ) => (m: MatchGroupIndexed[]) => MatchGroupIndexed[] = _ => + ) => (m: MatchGroupIndexed[]) => MatchGroupIndexed[] = _ => identity, -): MatchGroupIndexed[] => { +): MatchGroupIndexed[] => { return pipe( expr, match[], Expr>({ @@ -111,6 +111,26 @@ const checkExpr = ( ) }, + Sequence: ({ exprs }) => { + const getGroups = () => { + if (exprs.length > list.length) return [] + + const indexed = (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`) }, }), ) -- cgit v1.3.1