Nice TWiki > Dev > CurrentDiscussions > StandardLibrary > StandardLibraryMethods TWiki webs:
Dev | Doc | Main | TWiki | Sandbox
Dev . { Changes | Index | Search | Go }
An overview of the methods and function in stdlib for discussing semactics and as starting point of the documentation. The documentation comments are before the signatures and discussing about sematics and implementation after the signature.

Collection methods

<Any T> void foreach(java.util.Collection<T>, T->void);

<java.util.Collection C, Any T, Any U> C<U> map(C<T>, T->U);

<java.util.Collection C, Any T> C<T> filter(C<T>, T->boolean);
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 lA = lB.filter(...) where B is a subclass of A.

/** 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);

/** Modifies c, only keeping the elements for which test returns true. */
<T> void retain (Collection<T> c, T->boolean test);

/** Modifies c, removing the elements for which test returns true. */
<T> void remove (Collection<T> c, T->boolean test);

<Any T, Any U> U foldLeft(java.util.List<T> l, (U, T)->U f, U init);

<Any T, Any U> U foldRight(java.util.List<T> l, (T, U)->U f, U init);

<Any T> List<T> slice(List<T> list, int from = 0, int to = -1);
to is now inclusive maybe exclusive is better.

/**
   comparator must return a negative integer, zero, or a positive integer 
   as the first argument is less than, equal to, or greater than the second.
*/
<T> void sort(List<T> list, (T,T) -> int comparator);

<Any T> boolean contains (java.util.List<T> s, T->boolean test);

/** Find the first element that passes the given test.
    @throw java.util.NoSuchElementException if there is no such element.
*/
<Any T> T find (java.util.List<T> s, T->boolean test);

/** Find the last element that passes the given test.
    @throw java.util.NoSuchElementException if there is no such element.
*/
<Any T> T findLast (java.util.List<T> s, T->boolean test);

/** Find the first element that passes the given test.
    Returns <code>null</code> if there is no such element.
*/
<Any T> ?T search (java.util.List<!T> s, !T->boolean test);

/** Find the last element that passes the given test.
    Returns <code>null</code> if there is no such element.
*/
<Any T> ?T searchLast (java.util.List<!T> s, !T->boolean test);

<Any T> int indexOf (List<T> s, T->boolean test);

boolean or(java.util.List<boolean> s);
shouldn't this be defined for Collection
long max(java.util.List<long> s) requires !s.isEmpty();
shouldn't this be defined for Collection

/** @return the concatenation of the two lists, as a new list. */
<T, T1, T2 | T1 <: T, T2 <: T> List<T> `+`(List<T1> l1, List<T2> l2);

Array methods

<Any T> !T[] elementsNotNull(?T[] arr);

/** Returns an array with the newSize first elements. 
    The result can be the same array as the argument.
*/
<Any T> T[] resize(T[], int newSize);

<T,U | T <: U> void copy(T[] from, int fromIndex = 0, U[] to, int toIndex = 0, int count); 

<T,U | T <: U> void copy(List<T> from, int fromIndex = 0, U[] to, int toIndex = 0, int count);

<T, U | U <: T> T[] copy(U[] array);

<Any T> T[] slice(T[] array, int from = 0, int to = -1);
to is now inclusive maybe exclusive is better
/**
   Fills a newly created array with non-null values.

   A typical usage is to allocate a new array and set its values at the same time:
   <code>
     String[] numbers = fill(new String[10], int i => "number " + i);
   </code>

   The equivalent code in Java would be:
   <code>
     String[] numbers = new String[10];
     for (int i = 0; i < 10; i++) {
       numbers[i] = "number " + i;
     }
   </code>

   It is important that no reference to the array is kept,
   because that would make it possible to store null values in it.
   There is no danger as long as the array is created inside the call, 
   like in the above example.
*/
<Any T, Any U | U <: T> U[] fill(T[] array, int->U value);
In particular, this is useful with T = ?X and U = !X. The above type is more general, and useful when the component type is a type variable.

/** Returns an array containing, in order, the elements of both arguments.

    The type of the elements of the arguments can be subtypes of the result 
    elements, since a new array is created.
*/
<T, T1, T2 | T1 <: T, T2 <: T> T[] concat(T1[] a1, T2[] a2);

<T, T1, T2 | T1 <: T, T2 <: T>  T[] `+`(T1[] a1, T2[] a2);

Other methods

<Any T> !T notNull(?T value);

/* 
   Allows to consider any nice object as an instance of java.lang.Object
*/
<Any T> Object object(T);

/** Unsafe cast operator. UNSAFE! */
<Any T, Any U> U cast(T) = inline nice.lang.inline.Nop();

String replace(String source, char c, String with);

String deleteAtEnd(String source, String what);

/**
 * Break a string into a <code>List<String></code> of substrings,
 * splitting at (and removing) every occurrence of the <code>separator</code>
 * string.
 * 
 * @param str the string to split
 * @param separator the separator to split on 
 */ 
List<String> split(String str, String separator)
  requires separator.length() > 0 : "separator cannot be the empty string";

/**
 * Join a collection of strings together, interspersing <code>separator</code>
 * among them.
 *
 * @param strings the strings to join
 * @param separator the separator string to place between each 
 *    string and the one before it
 */
String join(Collection<String> strings, String separator);

Classes

Proposals

/**
  Count the number of elements for which test returns true
*/
<T> int count (Collection<T>, T->boolean test);

/**
  @return true if all boolean in the list are true otherwise returns false
*/
boolean and (List<boolean>);

/**
  @return true if the test returns true for all elements in the Collection otherwise return false
*/
<T> boolean all (Collection<T>, T->boolean test);

<T> T max(Collection<T> coll (T,T)->int comparator) requires !coll.isEmpty()

<T> T min(Collection<T> coll (T,T)->int comparator) requires !coll.isEmpty()

/**
  Fold left without init value
*/
<T> T foldl(List<T> list, (T, T)->T func) requires !list.isEmpty();

/**
  Fold right without init value
*/
<T> T foldr(List<T> list, (T, T)->T func) requires !list.isEmpty();

-- ArjanB - 08 Aug 2003

Topic StandardLibraryMethods . { Edit | Attach | Ref-By | Printable | Diffs | r1.3 | > | r1.2 | > | r1.1 | More }
Revision r1.3 - 08 Aug 2003 - 15:47 GMT - ArjanB
Parents: WebHome > CurrentDiscussions > StandardLibrary
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.