Use hold_until_date in item_scheduler.
[order_line_extra.git] / hincludes / lib / Lambda.class.php
1 <?php
2
3 class Lambda {
4         public function __construct(){}
5         static function harray($it) {
6                 $a = new _hx_array(array());
7                 if(null == $it) throw new HException('null iterable');
8                 $»it = $it->iterator();
9                 while($»it->hasNext()) {
10                         $i = $»it->next();
11                         $a->push($i);
12                 }
13                 return $a;
14         }
15         static function hlist($it) {
16                 $l = new HList();
17                 if(null == $it) throw new HException('null iterable');
18                 $»it = $it->iterator();
19                 while($»it->hasNext()) {
20                         $i = $»it->next();
21                         $l->add($i);
22                 }
23                 return $l;
24         }
25         static function map($it, $f) {
26                 $l = new HList();
27                 if(null == $it) throw new HException('null iterable');
28                 $»it = $it->iterator();
29                 while($»it->hasNext()) {
30                         $x = $»it->next();
31                         $l->add(call_user_func_array($f, array($x)));
32                 }
33                 return $l;
34         }
35         static function mapi($it, $f) {
36                 $l = new HList();
37                 $i = 0;
38                 if(null == $it) throw new HException('null iterable');
39                 $»it = $it->iterator();
40                 while($»it->hasNext()) {
41                         $x = $»it->next();
42                         $l->add(call_user_func_array($f, array($i++, $x)));
43                 }
44                 return $l;
45         }
46         static function has($it, $elt, $cmp = null) {
47                 if($cmp === null) {
48                         if(null == $it) throw new HException('null iterable');
49                         $»it = $it->iterator();
50                         while($»it->hasNext()) {
51                                 $x = $»it->next();
52                                 if($x == $elt) {
53                                         return true;
54                                 }
55                         }
56                 } else {
57                         if(null == $it) throw new HException('null iterable');
58                         $»it = $it->iterator();
59                         while($»it->hasNext()) {
60                                 $x = $»it->next();
61                                 if(call_user_func_array($cmp, array($x, $elt))) {
62                                         return true;
63                                 }
64                         }
65                 }
66                 return false;
67         }
68         static function exists($it, $f) {
69                 if(null == $it) throw new HException('null iterable');
70                 $»it = $it->iterator();
71                 while($»it->hasNext()) {
72                         $x = $»it->next();
73                         if(call_user_func_array($f, array($x))) {
74                                 return true;
75                         }
76                 }
77                 return false;
78         }
79         static function hforeach($it, $f) {
80                 if(null == $it) throw new HException('null iterable');
81                 $»it = $it->iterator();
82                 while($»it->hasNext()) {
83                         $x = $»it->next();
84                         if(!call_user_func_array($f, array($x))) {
85                                 return false;
86                         }
87                 }
88                 return true;
89         }
90         static function iter($it, $f) {
91                 if(null == $it) throw new HException('null iterable');
92                 $»it = $it->iterator();
93                 while($»it->hasNext()) {
94                         $x = $»it->next();
95                         call_user_func_array($f, array($x));
96                 }
97         }
98         static function filter($it, $f) {
99                 $l = new HList();
100                 if(null == $it) throw new HException('null iterable');
101                 $»it = $it->iterator();
102                 while($»it->hasNext()) {
103                         $x = $»it->next();
104                         if(call_user_func_array($f, array($x))) {
105                                 $l->add($x);
106                         }
107                 }
108                 return $l;
109         }
110         static function fold($it, $f, $first) {
111                 if(null == $it) throw new HException('null iterable');
112                 $»it = $it->iterator();
113                 while($»it->hasNext()) {
114                         $x = $»it->next();
115                         $first = call_user_func_array($f, array($x, $first));
116                 }
117                 return $first;
118         }
119         static function count($it, $pred = null) {
120                 $n = 0;
121                 if($pred === null) {
122                         if(null == $it) throw new HException('null iterable');
123                         $»it = $it->iterator();
124                         while($»it->hasNext()) {
125                                 $_ = $»it->next();
126                                 $n++;
127                         }
128                 } else {
129                         if(null == $it) throw new HException('null iterable');
130                         $»it = $it->iterator();
131                         while($»it->hasNext()) {
132                                 $x = $»it->next();
133                                 if(call_user_func_array($pred, array($x))) {
134                                         $n++;
135                                 }
136                         }
137                 }
138                 return $n;
139         }
140         static function hempty($it) {
141                 return !$it->iterator()->hasNext();
142         }
143         static function indexOf($it, $v) {
144                 $i = 0;
145                 if(null == $it) throw new HException('null iterable');
146                 $»it = $it->iterator();
147                 while($»it->hasNext()) {
148                         $v2 = $»it->next();
149                         if($v == $v2) {
150                                 return $i;
151                         }
152                         $i++;
153                 }
154                 return -1;
155         }
156         static function concat($a, $b) {
157                 $l = new HList();
158                 if(null == $a) throw new HException('null iterable');
159                 $»it = $a->iterator();
160                 while($»it->hasNext()) {
161                         $x = $»it->next();
162                         $l->add($x);
163                 }
164                 if(null == $b) throw new HException('null iterable');
165                 $»it = $b->iterator();
166                 while($»it->hasNext()) {
167                         $x = $»it->next();
168                         $l->add($x);
169                 }
170                 return $l;
171         }
172         function __toString() { return 'Lambda'; }
173 }