mirror of
https://github.com/NixOS/nix.git
synced 2025-11-15 06:52:43 +01:00
* A `map' primop.
This commit is contained in:
parent
af54a60204
commit
5d48dd6912
3 changed files with 36 additions and 0 deletions
|
|
@ -35,6 +35,7 @@ EvalState::EvalState()
|
||||||
addPrimOp1("baseNameOf", primBaseNameOf);
|
addPrimOp1("baseNameOf", primBaseNameOf);
|
||||||
addPrimOp1("toString", primToString);
|
addPrimOp1("toString", primToString);
|
||||||
addPrimOp1("isNull", primIsNull);
|
addPrimOp1("isNull", primIsNull);
|
||||||
|
addPrimOp1("map", primMap);
|
||||||
|
|
||||||
primOpsAll.add(primOps0);
|
primOpsAll.add(primOps0);
|
||||||
primOpsAll.add(primOps1);
|
primOpsAll.add(primOps1);
|
||||||
|
|
|
||||||
|
|
@ -329,3 +329,35 @@ Expr primCurTime(EvalState & state)
|
||||||
{
|
{
|
||||||
return ATmake("Int(<int>)", time(0));
|
return ATmake("Int(<int>)", time(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Expr primMap(EvalState & state, Expr arg)
|
||||||
|
{
|
||||||
|
arg = evalExpr(state, arg);
|
||||||
|
|
||||||
|
ATMatcher m;
|
||||||
|
ATermList es;
|
||||||
|
if (!(atMatch(m, arg) >> "Attrs" >> es))
|
||||||
|
throw Error("function `map' expects an attribute set");
|
||||||
|
|
||||||
|
Expr function = queryAttr(arg, "function");
|
||||||
|
if (!function)
|
||||||
|
throw Error("function `map' expects an attribute `function'");
|
||||||
|
|
||||||
|
Expr list = queryAttr(arg, "list");
|
||||||
|
if (!list)
|
||||||
|
throw Error("function `map' expects an attribute `list'");
|
||||||
|
|
||||||
|
list = evalExpr(state, list);
|
||||||
|
|
||||||
|
ATermList es2;
|
||||||
|
if (!(atMatch(m, list) >> "List" >> es2))
|
||||||
|
throw Error("attribute `list' in call to `map' must be a list");
|
||||||
|
|
||||||
|
ATermList res = ATempty;
|
||||||
|
for (ATermIterator i(es2); i; ++i)
|
||||||
|
res = ATinsert(res,
|
||||||
|
ATmake("Call(<term>, <term>)", function, *i));
|
||||||
|
|
||||||
|
return ATmake("List(<term>)", ATreverse(res));
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -39,5 +39,8 @@ Expr primIsNull(EvalState & state, Expr arg);
|
||||||
value for a particular run of the program. */
|
value for a particular run of the program. */
|
||||||
Expr primCurTime(EvalState & state);
|
Expr primCurTime(EvalState & state);
|
||||||
|
|
||||||
|
/* Apply a function to each element of a list. */
|
||||||
|
Expr primMap(EvalState & state, Expr arg);
|
||||||
|
|
||||||
|
|
||||||
#endif /* !__PRIMOPS_H */
|
#endif /* !__PRIMOPS_H */
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue