diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2006-12-12 16:14:31 +0000 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2006-12-12 16:14:31 +0000 |
commit | 1a7e88bbd9290987e72616d42c9e9d344acc2a86 (patch) | |
tree | 6eb4f83ec6270a30cb137b5e84d0c380354bb5cb | |
parent | 5e6699188fba38619dc23c0b65ee70849f90ea6c (diff) | |
download | guix-1a7e88bbd9290987e72616d42c9e9d344acc2a86.tar.gz |
* New built-in function `builtins.attrNames' that returns the
names of the attributes in an attribute set.
-rw-r--r-- | doc/manual/writing-nix-expressions.xml | 18 | ||||
-rw-r--r-- | src/libexpr/primops.cc | 21 | ||||
-rw-r--r-- | tests/lang/eval-okay-attrnames.exp | 1 | ||||
-rw-r--r-- | tests/lang/eval-okay-attrnames.nix | 11 |
4 files changed, 51 insertions, 0 deletions
diff --git a/doc/manual/writing-nix-expressions.xml b/doc/manual/writing-nix-expressions.xml index 1cc5bb95b8..9770e91822 100644 --- a/doc/manual/writing-nix-expressions.xml +++ b/doc/manual/writing-nix-expressions.xml @@ -1362,6 +1362,24 @@ is also available as <function>builtins.derivation</function>.</para> </varlistentry> + <varlistentry><term><function>builtins.attrNames</function> + <replaceable>attrs</replaceable></term> + + <listitem><para>Return the names of the attributes in the + attribute set <replaceable>attrs</replaceable> in a sorted list. + For instance, <literal>builtins.attrNames {y = 1; x = + "foo";}</literal> evaluates to <literal>["x" "y"]</literal>. + There is no built-in function <function>attrValues</function>, but + you can easily define it yourself: + +<programlisting> +attrValues = attrs: map (name: builtins.getAttr name attrs) (builtins.attrNames attrs);</programlisting> + + </para></listitem> + + </varlistentry> + + <varlistentry><term><function>baseNameOf</function> <replaceable>s</replaceable></term> <listitem><para>Return the <emphasis>base name</emphasis> of the diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 1d0d9c6b22..fcf0354509 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -620,6 +620,26 @@ static Expr primGetEnv(EvalState & state, const ATermVector & args) } +/* Return the names of the attributes in an attribute set as a sorted + list of strings. */ +static Expr primAttrNames(EvalState & state, const ATermVector & args) +{ + ATermMap attrs(128); /* !!! */ + queryAllAttrs(evalExpr(state, args[0]), attrs); + + StringSet names; + for (ATermMap::const_iterator i = attrs.begin(); i != attrs.end(); ++i) + names.insert(aterm2String(i->key)); + + ATermList list = ATempty; + for (StringSet::const_reverse_iterator i = names.rbegin(); + i != names.rend(); ++i) + list = ATinsert(list, makeStr(*i, PathSet())); + + return makeList(list); +} + + /* Apply a function to every element of a list. */ static Expr primMap(EvalState & state, const ATermVector & args) { @@ -732,6 +752,7 @@ void EvalState::addPrimOps() addPrimOp("__head", 1, primHead); addPrimOp("__tail", 1, primTail); addPrimOp("__getEnv", 1, primGetEnv); + addPrimOp("__attrNames", 1, primAttrNames); addPrimOp("map", 2, primMap); addPrimOp("__getAttr", 2, primGetAttr); diff --git a/tests/lang/eval-okay-attrnames.exp b/tests/lang/eval-okay-attrnames.exp new file mode 100644 index 0000000000..98af99a0ca --- /dev/null +++ b/tests/lang/eval-okay-attrnames.exp @@ -0,0 +1 @@ +Str("newxfoonewxy",[]) diff --git a/tests/lang/eval-okay-attrnames.nix b/tests/lang/eval-okay-attrnames.nix new file mode 100644 index 0000000000..978138f0c0 --- /dev/null +++ b/tests/lang/eval-okay-attrnames.nix @@ -0,0 +1,11 @@ +with import ./lib.nix; + +let + + attrs = {y = "y"; x = "x"; foo = "foo";} // rec {x = "newx"; bar = x;}; + + names = builtins.attrNames attrs; + + values = map (name: builtins.getAttr name attrs) names; + +in concat values |