Version 8 (modified by Chuck, 16 years ago) |
---|
Standard Library Class Extension methods
The Cobra standard library adds the following extension methods to existing classes. This enables convenient expressions such as:
for item in someList.sorted.reversed print item for i, item in someList.sorted.numbered print i, item # i will be 0, 1, 2, ...
Here they are:
namespace Cobra.Lang extend System.Object def typeOf as System.Type """ Using.typeOf is portable between platforms in contrast to CLR .getType and JVM .getClass. """ return .getType def toTechString as String def toPrintString as String extend String def split(chars as List<of char>) as List<of String> test s = 'a,b:c:d,e,f' assert s.split([c',', c':']) == ['a', 'b', 'c', 'd', 'e', 'f'] def split(chars as IList<of char>) as List<of String> extend System.Collections.IList def swap(i as int, j as int) require i >= -.count and i < .count j >= -.count and j < .count ensure old this[i] == this[j] old this[j] == this[i] .count == old.count extend IEnumerable<of T> def toList as List<of T> return List<of T>(this) extend IList<of T> def concated(other as IList<of T>?) as IList<of T> """ Returns a new list with the contents of this list and the other. Does not modify this list or the other. The returned list is the same class as the receiver. Assumes the receiving type has an initializer that takes a `capacity as int`. """ def get(flexibleIndex as int) as T require .count > 0 (flexibleIndex >= 0 and flexibleIndex < .count) _ or (flexibleIndex < 0 and flexibleIndex >= -.count) def get(flexibleIndex as int, default as T) as T ensure .count == 0 implies result == default (flexibleIndex > .count or flexibleIndex < -.count) _ implies result == default def last as T """ Returns the last element in the list. """ require .count > 0 def numbered as KeyValuePair<of int, T>* """ Returns a stream of pairs of (index, value). Can be used like so: for i, value in someList.numbered ... """ def numberedDown as KeyValuePair<of int, T>* """ Returns a stream of pairs of (index, value) in reverse order from the end of the list down. Can be used like so: for i, value in someList.numberedDown ... """ def random as T require .count > 0 return .random(CobraCore.random) def random(r as Random) as T require .count > 0 return this[r.next % .count] def removeLast require .count > 0 ensure .count == old .count - 1 .removeAt(.count-1) def reversed as List<of T> ensure result is not this result.count == .count def sorted as List<of T> ensure result is not this result.count == .count def sorted(comparison as Comparison<of T>) as List<of T> ensure result is not this result.count == .count def sorted(comparer as Comparer<of T>) as List<of T> ensure result is not this result.count == .count def swap(i as int, j as int) """ Swaps the elements at the given indexes which can be negative to index from the end of the list towards the front (-1 is last element, -2 is second to last, etc.). """ require i >= -.count and i < .count j >= -.count and j < .count ensure old this[i] == this[j] old this[j] == this[i] .count == old.count
Back to AdditionalDoc