list.reject

This commit is contained in:
Chris Wanstrath 2025-11-08 08:14:46 -08:00
parent 3aa40ae2c2
commit f57452ece2
2 changed files with 16 additions and 0 deletions

View File

@ -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)

View File

@ -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: