diff --git a/src/prelude/list.ts b/src/prelude/list.ts index 1f0ec76..8e861b9 100644 --- a/src/prelude/list.ts +++ b/src/prelude/list.ts @@ -14,6 +14,13 @@ export const list = { } return acc }, + reject: async (list: any[], cb: Function) => { + let acc: any[] = [] + for (const value of list) { + if (!(await cb(value))) acc.push(value) + } + return acc + }, reduce: async (list: any[], cb: Function, initial: any) => { let acc = initial for (const value of list) acc = await cb(acc, value) diff --git a/src/prelude/tests/prelude.test.ts b/src/prelude/tests/prelude.test.ts index 04ceb8b..069b174 100644 --- a/src/prelude/tests/prelude.test.ts +++ b/src/prelude/tests/prelude.test.ts @@ -193,6 +193,15 @@ describe('collections', () => { `).toEvaluateTo([3, 4, 5]) }) + test('list.reject doesnt keep matching elements', async () => { + await expect(` + is-even = do x: + (x % 2) == 0 + end + list.reject [1 2 3 4 5] is-even + `).toEvaluateTo([1, 3, 5]) + }) + test('list.reduce accumulates values', async () => { await expect(` add = do acc x: