forked from lix-project/lix
* Added a builtin function `isList' to test whether a value is a list.
With this primitive, a list-flattening function can be implemented (NIX-55, example is in tests/lang/eval-okay-flatten.nix).
This commit is contained in:
parent
c02a44183f
commit
d315210612
|
@ -521,6 +521,14 @@ static Expr primIsNull(EvalState & state, const ATermVector & args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Determine whether the argument is a list. */
|
||||||
|
static Expr primIsList(EvalState & state, const ATermVector & args)
|
||||||
|
{
|
||||||
|
ATermList list;
|
||||||
|
return makeBool(matchList(evalExpr(state, args[0]), list));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static Path findDependency(Path dir, string dep)
|
static Path findDependency(Path dir, string dep)
|
||||||
{
|
{
|
||||||
if (dep[0] == '/') throw EvalError(
|
if (dep[0] == '/') throw EvalError(
|
||||||
|
@ -782,6 +790,7 @@ void EvalState::addPrimOps()
|
||||||
addPrimOp("__toXML", 1, primToXML);
|
addPrimOp("__toXML", 1, primToXML);
|
||||||
addPrimOp("__toFile", 1, primToFile);
|
addPrimOp("__toFile", 1, primToFile);
|
||||||
addPrimOp("isNull", 1, primIsNull);
|
addPrimOp("isNull", 1, primIsNull);
|
||||||
|
addPrimOp("__isList", 1, primIsList);
|
||||||
addPrimOp("dependencyClosure", 1, primDependencyClosure);
|
addPrimOp("dependencyClosure", 1, primDependencyClosure);
|
||||||
addPrimOp("abort", 1, primAbort);
|
addPrimOp("abort", 1, primAbort);
|
||||||
addPrimOp("__head", 1, primHead);
|
addPrimOp("__head", 1, primHead);
|
||||||
|
|
1
tests/lang/eval-okay-flatten.exp
Normal file
1
tests/lang/eval-okay-flatten.exp
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Str("1234567")
|
19
tests/lang/eval-okay-flatten.nix
Normal file
19
tests/lang/eval-okay-flatten.nix
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
let {
|
||||||
|
|
||||||
|
fold = op: nul: list:
|
||||||
|
if list == []
|
||||||
|
then nul
|
||||||
|
else op (builtins.head list) (fold op nul (builtins.tail list));
|
||||||
|
|
||||||
|
concat =
|
||||||
|
fold (x: y: x + y) "";
|
||||||
|
|
||||||
|
flatten = x:
|
||||||
|
if builtins.isList x
|
||||||
|
then fold (x: y: (flatten x) ++ y) [] x
|
||||||
|
else [x];
|
||||||
|
|
||||||
|
l = ["1" "2" ["3" ["4"] ["5" "6"]] "7"];
|
||||||
|
|
||||||
|
body = concat (flatten l);
|
||||||
|
}
|
Loading…
Reference in a new issue