genList
builtins.genList
Primop
Docs pulled from | This Revision | about 4 hours ago
Nix manual
Takes 2 arguments
generator, length
Generate list of size length, with each element i equal to the
value returned by generator i. For example,
builtins.genList (x: x * x) 5
returns the list [ 0 1 4 9 16 ].
Time Complexity
Complexity of genList generator n: O(n)
Complexity of deepSeq (genList generator n): O(n * T_f)
where:
n = requested length
T_f = generator call evaluation time
Noogle detected
Detected Type
genList :: (Int -> a) -> Int -> [a]
Implementation
This function is implemented in c++ and is part of the native nix runtime.
static void prim_genList(EvalState & state, const PosIdx pos, Value ** args, Value & v)
{
auto len_ = state.forceInt(*args[1], pos, "while evaluating the second argument passed to builtins.genList").value;
if (len_ < 0 || std::make_unsigned_t<NixInt::Inner>(len_) > std::numeric_limits<size_t>::max())
state.error<EvalError>("cannot create list of size %1%", len_).atPos(pos).debugThrow();
size_t len = size_t(len_);
// More strict than strictly (!) necessary, but acceptable
// as evaluating map without accessing any values makes little sense.
state.forceFunction(*args[0], noPos, "while evaluating the first argument passed to builtins.genList");
auto list = state.buildList(len);
for (const auto & [n, v] : enumerate(list)) {
auto arg = state.allocValue();
arg->mkInt(n);
(v = state.allocValue())->mkApp(args[0], arg);
}
v.mkList(list);
}