= Standard Library Class Extension methods = The Cobra standard library adds the following extension methods to existing classes. This enable convenient expressions such as: {{{ #!python for item in someList.sorted.reversed print item }}} Here they are: {{{ #!python namespace Cobra.Lang extend System.Object def toTechString as String def toPrintString as String extend String def split(chars as List) as List test s = 'a,b:c:d,e,f' assert s.split([c',', c':']) == ['a', 'b', 'c', 'd', 'e', 'f'] def split(chars as IList) as List 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 def toList as List return List(this) extend IList 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* """ Returns a stream of pairs of (index, value). Can be used like so: for i, value in someList.numbered ... """ def reversed as List ensure result is not this result.count == .count def sorted as List ensure result is not this result.count == .count def sorted(comparison as Comparison) as List ensure result is not this result.count == .count def sorted(comparer as Comparer) as List 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