[Nice-commit] Nice/stdlib/nice/lang collections.nice,1.44,1.45
Brought to you by:
bonniot
|
From: <bo...@us...> - 2003-02-26 19:19:42
|
Update of /cvsroot/nice/Nice/stdlib/nice/lang
In directory sc8-pr-cvs1:/tmp/cvs-serv16819/stdlib/nice/lang
Modified Files:
collections.nice
Log Message:
Added similarEmptyCollection, to factorize the creation of a collection
of the same class as a given one.
Used to implement map and filter.
Also added filter that allows to keep a certain subtype of the collection
element type.
Index: collections.nice
===================================================================
RCS file: /cvsroot/nice/Nice/stdlib/nice/lang/collections.nice,v
retrieving revision 1.44
retrieving revision 1.45
diff -C2 -d -r1.44 -r1.45
*** collections.nice 24 Jan 2003 12:36:47 -0000 1.44
--- collections.nice 26 Feb 2003 19:19:37 -0000 1.45
***************
*** 12,18 ****
--- 12,44 ----
<Any T> void iter(java.util.Collection<T> c, T->void f) = c.foreach(f);
+ <java.util.Collection C, T, U> C<U> similarEmptyCollection(C<T>);
+
<java.util.Collection C, Any T, Any U> C<U> map(C<T>, T->U);
+
+ // A more precise type for filter would be:
+ // <Collection C, T, U | T <: U> C<U> filter(C<T>, T -> boolean);
+ // This would allow List<A> lA = lB.filter(...) where B is a subclass of A.
+
<java.util.Collection C, Any T> C<T> filter(C<T>, T->boolean);
+ /** Return a collection containing all elements for which converter
+ returns a non-null value.
+ It is possible to retain only elemnts with a subtype of the original
+ element type, in which case the returned collection has that
+ element type.
+ */
+ <Collection C, T, U> C<!U> filter(C<T> source, T->?U converter)
+ {
+ C<!U> res = source.similarEmptyCollection();
+ source.foreach(T elem => {
+ ?U converted = converter(elem);
+ if (converted != null)
+ res.add(converted);
+ });
+ return res;
+ }
+
+
+ /** Modifies c, only keeping the elements for which test returns true. */
<T> void keep (Collection<T> c, T->boolean test)
{
***************
*** 26,29 ****
--- 52,56 ----
}
+ /** Modifies c, removing the elements for which test returns true. */
<T> void remove(Collection<T> c, T->boolean test)
{
***************
*** 37,43 ****
}
- // A more precise type for filter would be:
- // <Collection C, T, U | T <: U> C<U> filter(C<T>, T -> boolean);
- // This would allow List<A> lA = lB.filter(...) where B is a subclass of A.
<Any T, Any U> U foldLeft(java.util.List<T> l, (U, T)->U f, U init)
--- 64,67 ----
***************
*** 93,145 ****
}
! map(c, f)
! {
! throw new Error("Map not implemented for " + c.getClass());
! }
!
! map<C,T,U>(c#ArrayList, f)
{
! ArrayList<U> res = new ArrayList(c.size());
c.foreach(T elem => { res.add(f(elem)); });
return res;
}
! map<C,T,U>(c#java.util.Vector, f)
{
! java.util.Vector<U> res = new java.util.Vector(c.size());
! c.foreach(T elem => { res.add(f(elem)); });
return res;
}
! map<C,T,U>(c#java.util.Stack, f)
{
! java.util.Stack<U> res = new java.util.Stack();
! c.foreach(T elem => { res.add(f(elem)); });
! return res;
}
! filter(c, f)
{
! throw new Error("Filter not implemented for " + c.getClass());
}
! filter<C,T>(c#ArrayList, test)
{
! ArrayList<T> res = new ArrayList(c.size());
! c.foreach(T elem => { if(test(elem)) res.add(elem); });
return res;
}
! filter<C,T>(c#java.util.Vector, test)
{
! java.util.Vector<T> res = new java.util.Vector(c.size());
! c.foreach(T elem => { if(test(elem)) res.add(elem); });
return res;
}
! filter<C,T>(c#java.util.Stack, test)
{
! java.util.Stack<T> res = new java.util.Stack();
! c.foreach(T elem => { if(test(elem)) res.add(elem); });
return res;
}
--- 117,160 ----
}
! map<C,T,U>(c, f)
{
! C<U> res = c.similarEmptyCollection();
c.foreach(T elem => { res.add(f(elem)); });
return res;
}
! filter<C,T>(c, test)
{
! C<T> res = c.similarEmptyCollection();
! c.foreach(T elem => { if(test(elem)) res.add(elem); });
return res;
}
! similarEmptyCollection(c)
{
! throw new Error("The method similarEmptyCollection is not yet implemented for " + c.getClass());
}
! similarEmptyCollection<C,T,U>(c#ArrayList)
{
! ArrayList<U> res = new ArrayList(c.size());
! return res;
}
! similarEmptyCollection<C,T,U>(c#LinkedList)
{
! LinkedList<U> res = new LinkedList();
return res;
}
! similarEmptyCollection<C,T,U>(c#java.util.Vector)
{
! java.util.Vector<U> res = new java.util.Vector(c.size());
return res;
}
! similarEmptyCollection<C,T,U>(c#java.util.Stack)
{
! java.util.Stack<U> res = new java.util.Stack();
return res;
}
***************
*** 249,251 ****
c.foreach(T elem => res[i++] = elem);
return res;
! }
\ No newline at end of file
--- 264,266 ----
c.foreach(T elem => res[i++] = elem);
return res;
! }
|