Merge pull request #2273 from volth/issue-1776

[wip] lib.concatMap and lib.mapAttrs to be builtins
This commit is contained in:
Eelco Dolstra 2018-07-05 16:50:20 +02:00 committed by GitHub
commit ddc9b87df1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 0 deletions

View file

@ -1356,6 +1356,24 @@ static void prim_functionArgs(EvalState & state, const Pos & pos, Value * * args
} }
/* Apply a function to every element of an attribute set. */
static void prim_mapAttrs(EvalState & state, const Pos & pos, Value * * args, Value & v)
{
state.forceFunction(*args[0], pos);
state.forceAttrs(*args[1], pos);
state.mkAttrs(v, args[1]->attrs->size());
for (auto & i : *args[1]->attrs) {
Value vName, vFun2;
mkString(vName, i.name);
state.callFunction(*args[0], vName, vFun2, pos);
state.callFunction(vFun2, *i.value, *state.allocAttr(v, i.name), pos);
}
}
/************************************************************* /*************************************************************
* Lists * Lists
*************************************************************/ *************************************************************/
@ -1627,6 +1645,36 @@ static void prim_partition(EvalState & state, const Pos & pos, Value * * args, V
} }
/* concatMap = f: list: concatLists (map f list); */
/* C++-version is to avoid allocating `mkApp', call `f' eagerly */
static void prim_concatMap(EvalState & state, const Pos & pos, Value * * args, Value & v)
{
state.forceFunction(*args[0], pos);
state.forceList(*args[1], pos);
auto nrLists = args[1]->listSize();
Value lists[nrLists];
size_t len = 0;
for (unsigned int n = 0; n < nrLists; ++n) {
Value * vElem = args[1]->listElems()[n];
state.forceValue(*vElem);
state.callFunction(*args[0], *vElem, lists[n], pos);
state.forceList(lists[n], pos);
len += lists[n].listSize();
}
state.mkList(v, len);
auto out = v.listElems();
for (unsigned int n = 0, pos = 0; n < nrLists; ++n) {
auto l = lists[n].listSize();
if (l)
memcpy(out + pos, lists[n].listElems(), l * sizeof(Value *));
pos += l;
}
}
/************************************************************* /*************************************************************
* Integer arithmetic * Integer arithmetic
*************************************************************/ *************************************************************/
@ -2212,6 +2260,7 @@ void EvalState::createBaseEnv()
addPrimOp("__intersectAttrs", 2, prim_intersectAttrs); addPrimOp("__intersectAttrs", 2, prim_intersectAttrs);
addPrimOp("__catAttrs", 2, prim_catAttrs); addPrimOp("__catAttrs", 2, prim_catAttrs);
addPrimOp("__functionArgs", 1, prim_functionArgs); addPrimOp("__functionArgs", 1, prim_functionArgs);
addPrimOp("__mapAttrs", 2, prim_mapAttrs);
// Lists // Lists
addPrimOp("__isList", 1, prim_isList); addPrimOp("__isList", 1, prim_isList);
@ -2229,6 +2278,7 @@ void EvalState::createBaseEnv()
addPrimOp("__genList", 2, prim_genList); addPrimOp("__genList", 2, prim_genList);
addPrimOp("__sort", 2, prim_sort); addPrimOp("__sort", 2, prim_sort);
addPrimOp("__partition", 2, prim_partition); addPrimOp("__partition", 2, prim_partition);
addPrimOp("__concatMap", 2, prim_concatMap);
// Integer arithmetic // Integer arithmetic
addPrimOp("__add", 2, prim_add); addPrimOp("__add", 2, prim_add);

View file

@ -0,0 +1 @@
[ [ 1 3 5 7 9 ] [ "a" "z" "b" "z" ] ]

View file

@ -0,0 +1,5 @@
with import ./lib.nix;
[ (builtins.concatMap (x: if x / 2 * 2 == x then [] else [ x ]) (range 0 10))
(builtins.concatMap (x: [x] ++ ["z"]) ["a" "b"])
]

View file

@ -0,0 +1 @@
{ x = "x-foo"; y = "y-bar"; }

View file

@ -0,0 +1,3 @@
with import ./lib.nix;
builtins.mapAttrs (name: value: name + "-" + value) { x = "foo"; y = "bar"; }