fix: list related primops

This commit is contained in:
2026-01-22 16:46:11 +08:00
parent 95faa7b35f
commit 7679a3b67f

View File

@@ -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;
};