aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr/primops.cc
diff options
context:
space:
mode:
authorvolth <volth@volth.com>2018-07-05 02:52:02 +0000
committervolth <volth@volth.com>2018-07-05 02:54:09 +0000
commit403a76a18fbbbc7b1ad4b65ed8c66c9abbbffa6f (patch)
tree491ac5ef065bc2b7f03cc3f40e0f786956e14a5f /src/libexpr/primops.cc
parent3defdccf4b46fe678496e5e3430c55b827477d0d (diff)
lib.concatMap and lib.mapAttrs to be builtins
Diffstat (limited to 'src/libexpr/primops.cc')
-rw-r--r--src/libexpr/primops.cc42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index 3a6c4035b..7ff57f0b0 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -1356,6 +1356,24 @@ static void prim_functionArgs(EvalState & state, const Pos & pos, Value * * args
}
+/* Apply a function to every element of a list. */
+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
*************************************************************/
@@ -1627,6 +1645,28 @@ 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 len = args[1]->listSize();
+
+ Value vList;
+ state.mkList(vList, len);
+
+ for (unsigned int n = 0; n < len; ++n) {
+ Value * vElem = args[1]->listElems()[n];
+ state.forceValue(*vElem);
+ state.callFunction(*args[0], *vElem, *(vList.listElems()[n] = state.allocValue()), pos);
+ }
+
+ state.concatLists(v, len, vList.listElems(), pos);
+}
+
+
+
/*************************************************************
* Integer arithmetic
*************************************************************/
@@ -2212,6 +2252,7 @@ void EvalState::createBaseEnv()
addPrimOp("__intersectAttrs", 2, prim_intersectAttrs);
addPrimOp("__catAttrs", 2, prim_catAttrs);
addPrimOp("__functionArgs", 1, prim_functionArgs);
+ addPrimOp("__mapAttrs", 2, prim_mapAttrs);
// Lists
addPrimOp("__isList", 1, prim_isList);
@@ -2229,6 +2270,7 @@ void EvalState::createBaseEnv()
addPrimOp("__genList", 2, prim_genList);
addPrimOp("__sort", 2, prim_sort);
addPrimOp("__partition", 2, prim_partition);
+ addPrimOp("__concatMap", 2, prim_concatMap);
// Integer arithmetic
addPrimOp("__add", 2, prim_add);