From 7679a3b67fae19e36aaf9f91743e43568feeb7c7 Mon Sep 17 00:00:00 2001 From: imxyy_soope_ Date: Thu, 22 Jan 2026 16:46:11 +0800 Subject: [PATCH] fix: list related primops --- nix-js/runtime-ts/src/builtins/list.ts | 45 ++++++++++++++++++++------ 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/nix-js/runtime-ts/src/builtins/list.ts b/nix-js/runtime-ts/src/builtins/list.ts index 6f3b46e..7b79ef6 100644 --- a/nix-js/runtime-ts/src/builtins/list.ts +++ b/nix-js/runtime-ts/src/builtins/list.ts @@ -5,17 +5,30 @@ import type { NixValue, NixList, NixAttrs } from "../types"; import { force } from "../thunk"; -import { forceList, forceFunction, forceInt } from "../type-assert"; +import { forceList, forceFunction, forceInt, forceBool } from "../type-assert"; +import { op } from "../operators"; export const map = (f: NixValue) => - (list: NixValue): NixList => - forceList(list).map(forceFunction(f)); + (list: NixValue): NixList => { + const forcedList = forceList(list); + if (forcedList.length) { + const func = forceFunction(f); + return forcedList.map(func); + } + return []; + }; export const filter = (f: NixValue) => - (list: NixValue): NixList => - forceList(list).filter(forceFunction(f)); + (list: NixValue): NixList => { + const forcedList = forceList(list); + if (forcedList.length) { + const func = forceFunction(f); + return forcedList.filter((e) => forceBool(func(e))); + } + return []; + }; export const length = (e: NixValue): bigint => { const forced = force(e); @@ -30,7 +43,7 @@ export const tail = (list: NixValue): NixList => forceList(list).slice(1); export const elem = (x: NixValue) => (xs: NixValue): boolean => - forceList(xs).includes(force(x)); + forceList(xs).find((e) => op.eq(x, e)) !== undefined; export const elemAt = (xs: NixValue) => @@ -116,10 +129,22 @@ export const genList = export const all = (pred: NixValue) => - (list: NixValue): boolean => - forceList(list).every(forceFunction(pred)); + (list: NixValue): boolean => { + const forcedList = forceList(list); + if (forcedList.length) { + const f = forceFunction(pred); + return forcedList.every((e) => forceBool(f(e))); + } + return true; + }; export const any = (pred: NixValue) => - (list: NixValue): boolean => - forceList(list).some(forceFunction(pred)); + (list: NixValue): boolean => { + const forcedList = forceList(list); + if (forcedList.length) { + const f = forceFunction(pred); + return forcedList.some((e) => forceBool(f(e))); + } + return true; + };