~ubuntu-branches/ubuntu/maverick/haxe/maverick

« back to all changes in this revision

Viewing changes to haxe/std/Lambda.hx

  • Committer: Bazaar Package Importer
  • Author(s): Jens Peter Secher
  • Date: 2008-06-15 11:04:09 UTC
  • mfrom: (2.1.6 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080615110409-7pyykgwmk5v0cues
Tags: 1:1.19-3
* Remove bashism in script.
  (Closes: #484390)
* Upgrade to Policy 3.8.0 by including a README.source explaining how to
  use dpatch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
class Lambda {
31
31
 
32
32
        /**
33
 
                Creates an [Array] from an [Iterator]
 
33
                Creates an [Array] from an [Iterable]
34
34
        **/
35
 
        public static function array<A>( it : Iterator<A> ) : Array<A> {
 
35
        public static function array<A>( it : Iterable<A> ) : Array<A> {
36
36
                var a = new Array<A>();
37
37
                for(i in it)
38
38
                        a.push(i);
40
40
        }
41
41
 
42
42
        /**
43
 
                Creates a [List] from an [Iterator]
 
43
                Creates a [List] from an [Iterable]
44
44
        **/
45
 
        public static function list<A>( it : Iterator<A> ) : List<A> {
 
45
        public static function list<A>( it : Iterable<A> ) : List<A> {
46
46
                var l = new List<A>();
47
47
                for(i in it)
48
48
                        l.add(i);
50
50
        }
51
51
 
52
52
        /**
53
 
                Creates a new [Iterator] that will apply the function 'f' to all
 
53
                Creates a new [Iterable] by appling the function 'f' to all
54
54
                elements of the iterator 'it'.
55
55
        **/
56
 
        public static function map<A,B>( it : Iterator<A>, f : A -> B ) : Iterator<B> {
57
 
                return {
58
 
                        hasNext : it.hasNext,
59
 
                        next : function() { return f(it.next()); }
 
56
        public static function map<A,B>( it : Iterable<A>, f : A -> B ) : List<B> {
 
57
                var l = new List<B>();
 
58
                for( x in it )
 
59
                        l.add(f(x));
 
60
                return l;
 
61
        }
 
62
 
 
63
        /**
 
64
                Similar to [map], but also pass an index for each item iterated.
 
65
        **/
 
66
        public static function mapi<A,B>( it : Iterable<A>, f : Int -> A -> B ) : List<B> {
 
67
                var l = new List<B>();
 
68
                var i = 0;
 
69
                for( x in it )
 
70
                        l.add(f(i++,x));
 
71
                return l;
 
72
        }
 
73
 
 
74
        /**
 
75
                Tells if the element is part of an iterable
 
76
        **/
 
77
        public static function has<A>( it : Iterable<A>, elt : A, ?cmp : A -> A -> Bool ) : Bool {
 
78
                if( cmp == null ) {
 
79
                        for( x in it )
 
80
                                if( x == elt )
 
81
                                        return true;
 
82
                } else {
 
83
                        for( x in it )
 
84
                                if( cmp(x,elt) )
 
85
                                        return true;
60
86
                }
61
 
        }
62
 
 
63
 
        /**
64
 
                Call the function 'f' on all elements of the [Iterator] 'it'.
65
 
        **/
66
 
        public static function iter<A>( it : Iterator<A>, f : A -> Void ) {
 
87
                return false;
 
88
        }
 
89
 
 
90
        /**
 
91
                Tells if at least one element of the iterable if found by using the specific function.
 
92
        **/
 
93
        public static function exists<A>( it : Iterable<A>, f : A -> Bool ) {
 
94
                for( x in it )
 
95
                        if( f(x) )
 
96
                                return true;
 
97
                return false;
 
98
        }
 
99
 
 
100
        /**
 
101
                Tells if all elements of the iterable have the specified property defined by [f].
 
102
        **/
 
103
        public static function foreach<A>( it : Iterable<A>, f : A -> Bool ) {
 
104
                for( x in it )
 
105
                        if( !f(x) )
 
106
                                return false;
 
107
                return true;
 
108
        }
 
109
 
 
110
        /**
 
111
                Call the function 'f' on all elements of the [Iterable] 'it'.
 
112
        **/
 
113
        public static function iter<A>( it : Iterable<A>, f : A -> Void ) {
67
114
                for( x in it )
68
115
                        f(x);
69
116
        }
70
117
 
71
118
        /**
72
 
                Creates an [Array] from an [Array] 'a' by applying the function 'f'
73
 
                on all elements of 'a'.
 
119
                Return the list of elements matching the function 'f'
74
120
        **/
75
 
        public static function amap<A,B>(a : Array<A>,f : A -> B) : Array<B> {
76
 
                var b = new Array();
77
 
                for( x in a )
78
 
                        b.push(f(x));
79
 
                return b;
 
121
        public static function filter<A>( it : Iterable<A>, f : A -> Bool ) {
 
122
                var l = new List<A>();
 
123
                for( x in it )
 
124
                        if( f(x) )
 
125
                                l.add(x);
 
126
                return l;
80
127
        }
81
128
 
82
129
        /**
83
 
                Functional 'fold' using an [Iterator]
 
130
                Functional 'fold' using an [Iterable]
84
131
        **/
85
 
        public static function fold<A,B>( it : Iterator<A>, f : A -> B -> B, first : B ) : B {
 
132
        public static function fold<A,B>( it : Iterable<A>, f : A -> B -> B, first : B ) : B {
86
133
                for( x in it )
87
134
                        first = f(x,first);
88
135
                return first;
89
136
        }
90
137
 
91
138
        /**
92
 
                Count the number of elements in an [Iterator]
 
139
                Count the number of elements in an [Iterable]
93
140
        **/
94
 
        public static function count<A>( it : Iterator<A> ) {
 
141
        public static function count<A>( it : Iterable<A> ) {
95
142
                var n = 0;
96
143
                for( _ in it )
97
144
                        ++n;
98
145
                return n;
99
146
        }
100
147
 
101
 
}
 
 
b'\\ No newline at end of file'
 
148
        /**
 
149
                Tells if an iterable does not contain any element.
 
150
        **/
 
151
        public static function empty( it : Iterable<Dynamic> ) : Bool {
 
152
                return !it.iterator().hasNext();
 
153
        }
 
154
 
 
155
}