Wiki

Changes between Initial Version and Version 1 of StandardLibraryExtensionMethods

Show
Ignore:
Timestamp:
02/04/09 03:01:04 (15 years ago)
Author:
Chuck
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • StandardLibraryExtensionMethods

    v1 v1  
     1The Cobra standard library adds the following extension methods to existing classes. This enable convenient expressions such as: 
     2{{{ 
     3#!python 
     4for item in someList.sorted.reversed 
     5    print item 
     6}}} 
     7 
     8Here they are: 
     9 
     10{{{ 
     11#!python 
     12namespace Cobra.Lang 
     13 
     14        extend System.Collections.IList 
     15         
     16                def swap(i as int, j as int) 
     17                        require 
     18                                i >= -.count and i < .count 
     19                                j >= -.count and j < .count 
     20                        ensure 
     21                                old this[i] == this[j] 
     22                                old this[j] == this[i] 
     23                                .count == old.count 
     24 
     25 
     26namespace Cobra.Lang 
     27 
     28        extend IList<of T> 
     29         
     30                def get(flexibleIndex as int) as T 
     31                        require 
     32                                .count > 0 
     33                                (flexibleIndex >= 0 and flexibleIndex < .count) _ 
     34                                        or (flexibleIndex < 0 and flexibleIndex >= -.count) 
     35                 
     36                def get(flexibleIndex as int, default as T) as T 
     37                        ensure 
     38                                .count == 0 implies result == default 
     39                                (flexibleIndex > .count or flexibleIndex < -.count) _ 
     40                                        implies result == default 
     41         
     42                def last as T 
     43                        """ 
     44                        Returns the last element in the list. 
     45                        """ 
     46                        require .count > 0 
     47         
     48                def numbered as KeyValuePair<of int, T>* 
     49                        """ 
     50                        Returns a stream of pairs of (index, value). 
     51                        Can be used like so: 
     52                                for i, value in someList.numbered 
     53                                        ... 
     54                        """ 
     55         
     56                def reversed as List<of T> 
     57                        ensure 
     58                                result is not this 
     59                                result.count == .count 
     60         
     61                def sorted as List<of T> 
     62                        ensure 
     63                                result is not this 
     64                                result.count == .count 
     65                 
     66                def sorted(comparison as Comparison<of T>) as List<of T> 
     67                        ensure 
     68                                result is not this 
     69                                result.count == .count 
     70                         
     71                def sorted(comparer as Comparer<of T>) as List<of T> 
     72                        ensure 
     73                                result is not this 
     74                                result.count == .count 
     75         
     76                def swap(i as int, j as int) 
     77                        """ 
     78                        Swaps the elements at the given indexes which can be negative to index from the end of the 
     79                        list towards the front (-1 is last element, -2 is second to last, etc.). 
     80                        """ 
     81                        require 
     82                                i >= -.count and i < .count 
     83                                j >= -.count and j < .count 
     84                        ensure 
     85                                old this[i] == this[j] 
     86                                old this[j] == this[i] 
     87                                .count == old.count 
     88}}}