fix: list related primops
This commit is contained in:
@@ -5,17 +5,30 @@
|
|||||||
|
|
||||||
import type { NixValue, NixList, NixAttrs } from "../types";
|
import type { NixValue, NixList, NixAttrs } from "../types";
|
||||||
import { force } from "../thunk";
|
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 =
|
export const map =
|
||||||
(f: NixValue) =>
|
(f: NixValue) =>
|
||||||
(list: NixValue): NixList =>
|
(list: NixValue): NixList => {
|
||||||
forceList(list).map(forceFunction(f));
|
const forcedList = forceList(list);
|
||||||
|
if (forcedList.length) {
|
||||||
|
const func = forceFunction(f);
|
||||||
|
return forcedList.map(func);
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
};
|
||||||
|
|
||||||
export const filter =
|
export const filter =
|
||||||
(f: NixValue) =>
|
(f: NixValue) =>
|
||||||
(list: NixValue): NixList =>
|
(list: NixValue): NixList => {
|
||||||
forceList(list).filter(forceFunction(f));
|
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 => {
|
export const length = (e: NixValue): bigint => {
|
||||||
const forced = force(e);
|
const forced = force(e);
|
||||||
@@ -30,7 +43,7 @@ export const tail = (list: NixValue): NixList => forceList(list).slice(1);
|
|||||||
export const elem =
|
export const elem =
|
||||||
(x: NixValue) =>
|
(x: NixValue) =>
|
||||||
(xs: NixValue): boolean =>
|
(xs: NixValue): boolean =>
|
||||||
forceList(xs).includes(force(x));
|
forceList(xs).find((e) => op.eq(x, e)) !== undefined;
|
||||||
|
|
||||||
export const elemAt =
|
export const elemAt =
|
||||||
(xs: NixValue) =>
|
(xs: NixValue) =>
|
||||||
@@ -116,10 +129,22 @@ export const genList =
|
|||||||
|
|
||||||
export const all =
|
export const all =
|
||||||
(pred: NixValue) =>
|
(pred: NixValue) =>
|
||||||
(list: NixValue): boolean =>
|
(list: NixValue): boolean => {
|
||||||
forceList(list).every(forceFunction(pred));
|
const forcedList = forceList(list);
|
||||||
|
if (forcedList.length) {
|
||||||
|
const f = forceFunction(pred);
|
||||||
|
return forcedList.every((e) => forceBool(f(e)));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
export const any =
|
export const any =
|
||||||
(pred: NixValue) =>
|
(pred: NixValue) =>
|
||||||
(list: NixValue): boolean =>
|
(list: NixValue): boolean => {
|
||||||
forceList(list).some(forceFunction(pred));
|
const forcedList = forceList(list);
|
||||||
|
if (forcedList.length) {
|
||||||
|
const f = forceFunction(pred);
|
||||||
|
return forcedList.some((e) => forceBool(f(e)));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user