[Omake] help not exporting the loop variable?

Erick Tryzelaar erickt at dslextreme.com
Wed Jan 10 15:21:38 PST 2007


Erick Tryzelaar wrote:
> keys(m) =
>    result[] =
>    m.foreach(k, v)
>        result += $k
>        export result
>    return $(result)
>
> And maybe one for values as well?
>
> Then this could just be:
>
> .PHONY: $(packages.keys)

Here's a patch to implement this as a built in. Would something like 
this be interesting enough to add? It seems to work. Oh, and I added the 
map-length function to Maps, since it seems to have been missing.

-e

Index: src/builtin/omake_builtin_object.ml
===================================================================
--- src/builtin/omake_builtin_object.ml (revision 9932)
+++ src/builtin/omake_builtin_object.ml (working copy)
@@ -338,6 +338,32 @@
         ValEnv (venv', ExportAll)

(*
+ * Get an array of keys of the map.
+ *)
+let map_keys venv pos loc args =
+   let pos = string_pos "map-keys" pos in
+      match args with
+         [arg] ->
+            let map = map_of_value venv pos arg in
+            let keys = venv_map_fold (fun keys k v -> k::keys) [] map in
+               ValArray (keys)
+       | _ ->
+            raise (OmakeException (loc_pos loc pos, ArityMismatch 
(ArityExact 1, List.length args)))
+
+(*
+ * Get an array of values of the map.
+ *)
+let map_values venv pos loc args =
+   let pos = string_pos "map-values" pos in
+      match args with
+         [arg] ->
+            let map = map_of_value venv pos arg in
+            let keys = venv_map_fold (fun keys k v -> v::keys) [] map in
+               ValArray (keys)
+       | _ ->
+            raise (OmakeException (loc_pos loc pos, ArityMismatch 
(ArityExact 1, List.length args)))
+
+(*
 * \begin{doc}
 * \twofuns{create-map}{create-lazy-map}
 *
@@ -699,6 +725,8 @@
       true, "map-length",           map_length,          ArityExact 1;
       true, "map-map",              map_map,             ArityRange (3, 
4);
       true, "map-remove",           map_remove,          ArityExact 1;
+       true, "map-keys",             map_keys,            ArityExact 1;
+       true, "map-values",           map_values,          ArityExact 1;
       true, "sequence-map",         foreach_fun,         ArityRange (2, 
3);
       true, "sequence-length",      sequence_length,     ArityExact 1;
       true, "sequence-nth",         sequence_nth,        ArityExact 1;
Index: lib/Pervasives.om
===================================================================
--- lib/Pervasives.om   (revision 9932)
+++ lib/Pervasives.om   (working copy)
@@ -180,11 +180,14 @@
# The Map object provides the following methods.
#
# \begin{itemize}
+# \item \verb+$(o.length)+: the number of items in the map.
# \item \verb+$(o.mem <key>)+: returns \verb+true+ iff the \verb+<key>+ 
is defined
#    in the map.
# \item \verb+$(o.add <key>, <value>)+: adds the field to the map,
#    returning a new map.
# \item \verb+$(o.find <key>)+: fetches the field from the map.
+# \item \verb+$(o.keys)+: fetches an array of all the keys in the map.
+# \item \verb+$(o.values)+: fetches an array of all the values in the map.
# \item \verb+$(o.map <fun>)+: maps a function over the map.  The function
#    should take two arguments; the first is a field name, the second is 
the
#    value of that field.  The result is a new object constructed from the
@@ -256,6 +259,15 @@
    foreach(body, v, x) =
        return $(map-map $(this), $(v), $(x), $(body))

+    length() =
+        return $(map-length $(this))
+
+    keys() =
+        return $(map-keys $(this))
+
+    values() =
+        return $(map-values $(this))
+
########################################################################
# \begin{doc}
# \obj{Number}


More information about the Omake mailing list