aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2022-01-13 23:05:21 +0530
committerAkshay Nair <phenax5@gmail.com>2022-01-13 23:05:21 +0530
commitd10c241332cf14a0b9171739a76cbdafdbdf2826 (patch)
tree280c38b4fa792e4556188458bd2b43f2a2e5aa46
parent11bb9b17ad84e0c07aaa50ce65411a8adf692685 (diff)
downloadelxr-d10c241332cf14a0b9171739a76cbdafdbdf2826.tar.gz
elxr-d10c241332cf14a0b9171739a76cbdafdbdf2826.zip
feat(eval): implements sequence
-rw-r--r--src/eval/index.ts26
-rw-r--r--tests/basic.spec.ts10
2 files changed, 33 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`) },
}),
)
diff --git a/tests/basic.spec.ts b/tests/basic.spec.ts
index 3c49267..482428a 100644
--- a/tests/basic.spec.ts
+++ b/tests/basic.spec.ts
@@ -120,5 +120,15 @@ describe('Basic tests', () => {
{ value: [{ v: 1 }], index: 10 },
])
})
+
+ it('should match sequence of matchers', () => {
+ expect(
+ matchAll(/ true, \n, \s /, [true, 5, 'five', 1, 2, 3, true, 7, 'seven'])
+ .groups,
+ ).toEqual([
+ { value: [ [{ value: true, index: 0 }], [{ value: 5, index: 1 }], [{ value: 'five', index: 2 }] ], index: 0 },
+ { value: [ [{ value: true, index: 6 }], [{ value: 7, index: 7 }], [{ value: 'seven', index: 8 }] ], index: 6 },
+ ])
+ })
})
})