[svn] r6307: nemerle/trunk/lib/rlist.n

d svnadmin at nemerle.org
Thu May 18 18:22:30 CEST 2006


Log:
Add Exists, Member/Contains, Map and Iter.

Author: d
Date: Thu May 18 18:22:29 2006
New Revision: 6307

Modified:
   nemerle/trunk/lib/rlist.n

Modified: nemerle/trunk/lib/rlist.n
==============================================================================
--- nemerle/trunk/lib/rlist.n	(original)
+++ nemerle/trunk/lib/rlist.n	Thu May 18 18:22:29 2006
@@ -350,6 +350,118 @@
       }
     }    
   
+    /** Returns true if there exists an element on [xs], that
+        satisfies the predicate [f] (that is f (elem) == true).
+        Time complexity: O (|xs|).
+        <param name="xs">
+          The RList containing the tested elements.
+        </param>
+        <param name="f">
+          The predicate used during the tests.
+        </param>
+        <returns> 
+          Returns true if for any element on the RList [xs],
+          applying [f] to that element returns true, otherwise
+          returns false.
+        </returns>
+     */
+    public static Exists (xs : RList ['a], f : 'a -> bool) : bool {
+      def f' (x) { f (x.fst) || f (x.snd) }
+      match (xs) {
+        | One (x, ps) => f (x) || RList [Pair ['a]].Exists (ps, f')
+        | Zero (ps) => RList [Pair ['a]].Exists (ps, f')
+        | Nil => false
+      }
+    }
+
+    /** Returns true if there exists an element on [this], that
+        satisfies the predicate [f] (that is f (elem) == true).
+        Time complexity: O (|xs|).
+        <param name="f">
+          The predicate used during the tests.
+        </param>
+        <returns> 
+          Returns true if for any element on the RList [this],
+          applying [f] to that element returns true, otherwise
+          returns false.
+        </returns>
+     */
+    public Exists (f : 'a -> bool) : bool {
+      Exists (this, f)
+    }
+
+    /** Returns true if the element [elem] exists on the
+        RList [xs].
+        Time complexity: O (|xs|).
+        <param name="xs">
+          The RList containing the tested elements.
+        </param>
+        <param name="elem">
+          The element, which existence on the RList [xs] is 
+          being tested.
+        </param>
+        <returns> 
+          Returns true if for any element on the RList [xs],
+          element.Equals ([elem]), otherwise returns false.
+        </returns>
+     */
+    public static Member (xs : RList ['a], elem : 'a) : bool {
+      Exists (xs, x => x.Equals (elem))
+    }
+
+    /** Returns true if the element [elem] exists on the
+        RList [this].
+        Time complexity: O (|this|).
+        <param name="elem">
+          The element, which existence on the RList [this] is 
+          being tested.
+        </param>
+        <returns> 
+          Returns true if for any element on the RList [this],
+          element.Equals ([elem]), otherwise returns false.
+        </returns>
+     */
+    public Member (elem : 'a) : bool {
+      Member (this, elem)
+    }
+
+    /** Returns true if the element [elem] exists on the
+        RList [xs].
+        Time complexity: O (|xs|).
+        An alias for Member.
+        <param name="xs">
+          The RList containing the tested elements.
+        </param>
+        <param name="elem">
+          The element, which existence on the RList [xs] is 
+          being tested.
+        </param>
+        <returns> 
+          Returns true if for any element on the RList [xs],
+          element.Equals ([elem]), otherwise returns false.
+        </returns>
+     */
+    public static Contains (xs : RList ['a], elem : 'a) : bool {
+      Member (xs, elem)
+    }
+
+    /** Returns true if the element [elem] exists on the
+        RList [this].
+        Time complexity: O (|this|).
+        An alias for Member.
+        <param name="elem">
+          The element, which existence on the RList [this] is 
+          being tested.
+        </param>
+        <returns> 
+          Returns true if for any element on the RList [this],
+          element.Equals ([elem]), otherwise returns false.
+        </returns>
+     */
+    public Contains (elem : 'a) : bool {
+      Member (this, elem)
+    }
+
     /** Returns the [i]-th element of the RList [xs].
         Time complexity: O (log (|xs|)).
         <param name="xs">
@@ -524,6 +636,77 @@
       FoldRight (this, acc, f)
     }      
 
+    /** Returns a new RList composed from [xs] by applying [f] 
+        to every element on that RList. 
+        Time complexity: O (|xs|).
+        <param name="xs">
+          The source RList from which the return RList is composed by
+          applying [f] to each of its elements.
+        </param>
+        <param name="f">
+          The function being applied to every [xs] element. The values
+          it returns will make up the new RList returned by Map.
+        </param>
+        <returns>
+          A new RList composed from [xs] by applying [f] to every 
+          element on that RList.
+        </returns>
+     */
+    public static Map ['b] (xs : RList ['a], f : 'a -> 'b) : RList ['b] {
+      def f' (x) { Pair (f (x.fst), f (x.snd)) }
+      match (xs) {
+        | One (x, ps) => One (f (x), RList [Pair ['a]].Map (ps, f'))
+        | Zero (ps) => Zero (RList [Pair ['a]].Map (ps, f'))
+        | Nil => Nil ()
+      }
+    }
+
+    /** Returns a new RList composed from [this] by applying [f] 
+        to every element on that RList. 
+        Time complexity: O (|this|).
+        <param name="f">
+          The function being applied to every [this] element. The values
+          it returns will make up the new RList returned by Map.
+        </param>
+        <returns>
+          A new RList composed from [this] by applying [f] to every 
+          element on that RList.
+        </returns>
+     */
+    public Map ['b] (f : 'a -> 'b) : RList ['b] {
+      Map (this, f)
+    }
+
+    /** Iterates over [xs] applying [f] to each of its elements.
+        Time complexity: O (|xs|).
+        <param name="xs">
+          The RList on which [f] is iterated.
+        </param>
+        <param name="f">
+          The function being applied to every [xs] element during
+          iteration.
+        </param>
+     */
+    public static Iter (xs : RList ['a], f : 'a -> void) : void {
+      def f' (x) { f (x.fst); f (x.snd) }
+      match (xs) {
+        | One (x, ps) => f (x); RList [Pair ['a]].Iter (ps, f') 
+        | Zero (ps) => RList [Pair ['a]].Iter (ps, f') 
+        | Nil => ()
+      }
+    }
+
+    /** Iterates over [this] applying [f] to each of its elements.
+        Time complexity: O (|this|).
+        <param name="f">
+          The function being applied to every [this] element during
+          iteration.
+        </param>
+     */
+    public Iter (f : 'a -> void) : void {
+      Iter (this, f)
+    }
+
     /** Returns an RList composed by reversing [xs].
         Time complexity: O (|xs| * log (|xs|)).
         <param name="xs">



More information about the svn mailing list