map
builtins.map
Primop
Docs pulled from | This Revision | about 11 hours ago
Nix manual
Takes 2 arguments
f, list
Apply the function f to each element in the list list. For example,
map (x: "foo" + x) [ "bar" "bla" "abc" ]
evaluates to [ "foobar" "foobla" "fooabc" ].
Time Complexity
O(n) where:
n = list length
Calls to f are performed afterwards when needed.
Noogle detected
Detected Type
map :: (a -> b) -> [a] -> [b]
Implementation
This function is implemented in c++ and is part of the native nix runtime.
static void prim_map(EvalState & state, const PosIdx pos, Value ** args, Value & v)
{
state.forceList(*args[1], pos, "while evaluating the second argument passed to builtins.map");
if (args[1]->listSize() == 0) {
v = *args[1];
return;
}
state.forceFunction(*args[0], pos, "while evaluating the first argument passed to builtins.map");
auto list = state.buildList(args[1]->listSize());
for (const auto & [n, v] : enumerate(list))
(v = state.allocValue())->mkApp(args[0], args[1]->listView()[n]);
v.mkList(list);
}