aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2006-09-22 14:46:36 +0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2006-09-22 14:46:36 +0000
commitc02a44183fcff7c28cfed1c84c142cc2cf80f167 (patch)
treea22e8e94ab228480141ece3354c7c91602b5a47b
parent8a1ab709a47f0896cb5b47521e31c8c27f88b2b3 (diff)
* Builtin functions `head' and `tail' to return the head and tail of
list. Useful for lots of things, such as implementing a fold function (see NIX-30, example is in tests/lang/eval-okay-list.nix).
-rw-r--r--src/libexpr/primops.cc25
-rw-r--r--tests/lang/eval-okay-list.exp1
-rw-r--r--tests/lang/eval-okay-list.nix13
3 files changed, 39 insertions, 0 deletions
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index f08ad0464..b05082293 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -673,6 +673,27 @@ static Expr primAbort(EvalState & state, const ATermVector & args)
}
+/* Return the first element of a list. */
+static Expr primHead(EvalState & state, const ATermVector & args)
+{
+ ATermList list = evalList(state, args[0]);
+ if (ATisEmpty(list))
+ throw Error("`head' called on an empty list");
+ return evalExpr(state, ATgetFirst(list));
+}
+
+
+/* Return a list consisting of everything but the the first element of
+ a list. */
+static Expr primTail(EvalState & state, const ATermVector & args)
+{
+ ATermList list = evalList(state, args[0]);
+ if (ATisEmpty(list))
+ throw Error("`tail' called on an empty list");
+ return makeList(ATgetNext(list));
+}
+
+
/* Apply a function to every element of a list. */
static Expr primMap(EvalState & state, const ATermVector & args)
{
@@ -703,6 +724,7 @@ static Expr primCurrentTime(EvalState & state, const ATermVector & args)
}
+/* Dynamic version of the `.' operator. */
static Expr primGetAttr(EvalState & state, const ATermVector & args)
{
string attr = evalString(state, args[0]);
@@ -710,6 +732,7 @@ static Expr primGetAttr(EvalState & state, const ATermVector & args)
}
+/* Dynamic version of the `?' operator. */
static Expr primHasAttr(EvalState & state, const ATermVector & args)
{
string attr = evalString(state, args[0]);
@@ -761,6 +784,8 @@ void EvalState::addPrimOps()
addPrimOp("isNull", 1, primIsNull);
addPrimOp("dependencyClosure", 1, primDependencyClosure);
addPrimOp("abort", 1, primAbort);
+ addPrimOp("__head", 1, primHead);
+ addPrimOp("__tail", 1, primTail);
addPrimOp("map", 2, primMap);
addPrimOp("__getAttr", 2, primGetAttr);
diff --git a/tests/lang/eval-okay-list.exp b/tests/lang/eval-okay-list.exp
new file mode 100644
index 000000000..369d34641
--- /dev/null
+++ b/tests/lang/eval-okay-list.exp
@@ -0,0 +1 @@
+Str("foobarblatest")
diff --git a/tests/lang/eval-okay-list.nix b/tests/lang/eval-okay-list.nix
new file mode 100644
index 000000000..72a120d0d
--- /dev/null
+++ b/tests/lang/eval-okay-list.nix
@@ -0,0 +1,13 @@
+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) "";
+
+ body = concat ["foo" "bar" "bla" "test"];
+
+} \ No newline at end of file