Wiki

root/cobra/trunk/Source/Cobra.Lang/ExtendIEnumerable.cobra

Revision 2479, 3.0 KB (checked in by Charles.Esterbrook, 16 months ago)

Fixed: One of the String.split overloads does not use its options argument.
Add or improve doc strings.
credit:hopscc

  • Property svn:eol-style set to native
Line 
1namespace Cobra.Lang
2
3    extend System.Collections.IEnumerable
4   
5        def join(sep as String) as String
6            """
7            Join the items in an IEnumerable collection separated by a string.
8            """
9            test
10                assert ['a', 'b'].join('.') == 'a.b'
11                assert ['a'].join('.') == 'a'
12                assert [1, 2].join(', ') == '1, 2'
13                assert [1, 2].join('') == '12'
14                assert [].join('.') == ''
15            body
16                sb = StringBuilder()
17                s = ''
18                for item in this
19                    sb.append(s)
20                    sb.append(CobraCore.printStringMaker.makeString(item))
21                    s = sep
22                return sb.toString
23
24        def join(sep as String, lastSep as String) as String
25            """
26            Join the items in an IEnumerable collection with a separator string except for the last
27            two items; join them with the lastSep string
28            """
29            test
30                assert ['a', 'b'].join(', ', ' and ') == 'a and b'
31                assert ['a', 'b', 'c'].join(', ', ' and ') == 'a, b and c'
32                assert ['a', 'b', 'c', 'd'].join(', ', ' and ') == 'a, b, c and d'
33                assert [1, 2, 3, 4].join(', ', ' and ') == '1, 2, 3 and 4'
34                assert [1].join('.', ':') == '1'
35                assert [1, 2].join('', '') == '12'
36                assert [].join('.', ':') == ''
37            body
38                sb = StringBuilder()
39                s = ''
40                items = []
41                for item in this, items.add(item)
42                count = items.count
43                for i in count
44                    sb.append(s)
45                    sb.append(items[i].toString)
46                    s = if(i==count-2, lastSep, sep)
47                return sb.toString
48
49    extend IEnumerable<of T>
50
51        def toList as List<of T>
52            """ Turn an IEnumerable of T into a list of T. """
53            return List<of T>(this)
54/#
55    TODO: cannot do the following yet due to a bug in extensions and how they are chosen with respect to inheritance
56    these are available in lists though, so you can use .toList above
57    http://cobra-language.com/trac/cobra/ticket/130
58   
59        def last as T
60            for item in this, pass
61            return item
62
63        def numbered as KeyValuePair<of int, T>*
64            """
65            Returns a stream of pairs of (index, value).
66            Can be used like so:
67                for i, value in someStream.numbered
68                    ...
69            """
70            i = 0
71            for item in this
72                yield KeyValuePair<of int, T>(i, item)
73                i += 1
74
75        def reversed as List<of T>
76            return .toList.reversed
77
78        def sorted as List<of T>
79            return .toList.sorted
80
81        def sorted(comparison as Comparison<of T>) as List<of T>
82            # TODO: needs a test case
83            return .toList.sorted(comparison)
84
85        def sorted(comparer as Comparer<of T>) as List<of T>
86            # TODO: needs a test case
87            return .toList.sorted(comparer)
88#/
89
90
91    class TestIEnumerableExtensions
92
93        shared
94       
95            def stuff as int*
96                """ Returns 2, 1, 3 in that order. """
97                yield 2
98                yield 1
99                yield 3
100   
101            def empty as int*
102                yield break
103
104        test
105       
106            t = .stuff.toList
107            assert t.count == 3 and t == [2, 1, 3]
108           
109/#
110            assert .stuff.last == 3
111           
112            # TODO: test .numbered
113           
114            t = .stuff.reversed
115            assert t.count == 3 and t == [3, 1, 2]
116           
117            t = .stuff.sorted
118            assert t.count == 3 and t == [1, 2, 3]
119#/
120
121            t = .empty.toList
122            assert t.count == 0 and t == []
123
124/#
125            # TODO: test .numbered
126           
127            assert .empty.reversed == []
128            assert .empty.sorted == []
129#/
Note: See TracBrowser for help on using the browser.