aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2006-09-22 14:55:19 +0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2006-09-22 14:55:19 +0000
commitd315210612a8d5eb52654407903544b72222130b (patch)
treeedf5de3774a9a4faca84268b2c3ed2e5f3d6d175
parentc02a44183fcff7c28cfed1c84c142cc2cf80f167 (diff)
* 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).
-rw-r--r--src/libexpr/primops.cc9
-rw-r--r--tests/lang/eval-okay-flatten.exp1
-rw-r--r--tests/lang/eval-okay-flatten.nix19
3 files changed, 29 insertions, 0 deletions
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index b05082293..190d58733 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -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)
{
if (dep[0] == '/') throw EvalError(
@@ -782,6 +790,7 @@ void EvalState::addPrimOps()
addPrimOp("__toXML", 1, primToXML);
addPrimOp("__toFile", 1, primToFile);
addPrimOp("isNull", 1, primIsNull);
+ addPrimOp("__isList", 1, primIsList);
addPrimOp("dependencyClosure", 1, primDependencyClosure);
addPrimOp("abort", 1, primAbort);
addPrimOp("__head", 1, primHead);
diff --git a/tests/lang/eval-okay-flatten.exp b/tests/lang/eval-okay-flatten.exp
new file mode 100644
index 000000000..e76d25de1
--- /dev/null
+++ b/tests/lang/eval-okay-flatten.exp
@@ -0,0 +1 @@
+Str("1234567")
diff --git a/tests/lang/eval-okay-flatten.nix b/tests/lang/eval-okay-flatten.nix
new file mode 100644
index 000000000..2019263b8
--- /dev/null
+++ b/tests/lang/eval-okay-flatten.nix
@@ -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);
+}