Wiki
Version 7 (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 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 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 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