Debian Codegraph @

src/core/List.nml

raw
1 /*
2 * Copyright (C)2005-2022 Haxe Foundation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 neko("
24 @make = function(s) {
25 if( s == null )
26 return @Core.@empty;
27 return @Core.@cons(s[0],@make(s[1]));
28 }
29
30 @rmake = function(s,x) {
31 if( s == null )
32 return x;
33 return @rmake(s[1],@Core.@cons(s[0],x));
34 }
35
36 $exports.@rmake = function(s) { @rmake(s,@Core.@empty) };
37 $exports.@make = @make;
38 ");
39
40 function rec length(l) {
41 match l {
42 | [] -> 0
43 | _ :: l -> 1 + length l
44 }
45 }
46
47 function hd(l) {
48 match l {
49 | [] -> invalid_arg()
50 | x :: _ -> x
51 }
52 }
53
54 function tl(l) {
55 match l {
56 | [] -> invalid_arg()
57 | _ :: l -> l
58 }
59 }
60
61 function rec map(f,l) {
62 match l {
63 | [] -> []
64 | x :: l -> f(x) :: map f l
65 }
66 }
67
68 function rec iter(f,l) {
69 match l {
70 | [] -> ()
71 | x :: l -> f x; iter f l
72 }
73 }
74
75 function rec iter2(f,l1,l2) {
76 match l1 {
77 | [] ->
78 match l2 {
79 | [] -> ()
80 | _ -> invalid_arg()
81 }
82 | x1 :: l1 ->
83 match l2 {
84 | [] -> invalid_arg()
85 | x2 :: l2 ->
86 f x1 x2;
87 iter2 f l1 l2
88 }
89 }
90 }
91
92 function rec split(l) {
93 match l {
94 | [] -> ([] , [])
95 | (a,b) :: l ->
96 var la , lb = split l;
97 (a :: la, b :: lb)
98 }
99 }
100
101 function rec exists(f,l) {
102 match l {
103 | [] -> false
104 | x :: l -> if f x then true else exists f l
105 }
106 }
107
108 function rec mem(v,l) {
109 match l {
110 | [] -> false
111 | x :: l -> if x == v then true else mem v l
112 }
113 }
114
115 function rec assoc(k,l) {
116 match l {
117 | [] -> throw Not_found
118 | (k2,v) :: l -> if k == k2 then v else assoc k l
119 }
120 }
121
122 function rec phys(k,l) {
123 match l {
124 | [] -> throw Not_found
125 | (k2,v) :: l -> if k === k2 then v else phys k l
126 }
127 }
128
129 function rec find(f,l) {
130 match l {
131 | [] -> throw Not_found
132 | k :: l -> if f k then k else find f l
133 }
134 }
135
136 function rec rev_rec(l,acc) {
137 match l {
138 | [] -> acc
139 | x :: l -> rev_rec l (x :: acc)
140 }
141 }
142
143 function rev(l) {
144 rev_rec l []
145 }
146
147 function rec fold(f,acc,l) {
148 match l {
149 | [] -> acc
150 | x :: l -> fold f (f acc x) l
151 }
152 }
153
154 function rec append(l1,l2) {
155 match l1 {
156 | [] -> l2
157 | x :: l1 -> x :: append l1 l2
158 }
159 }
160
161 function rec concat(ll) {
162 function rec loop(ll,acc) {
163 match ll {
164 | [] -> acc
165 | l :: ll ->
166 loop ll (append acc l)
167 }
168 };
169 loop ll []
170 }
171
172 function rec chop(n,l) {
173 if n == 0 then
174 l
175 else match l {
176 | [] -> invalid_arg()
177 | _ :: l -> chop (n -1) l
178 }
179 }
180
181 function rec all(f,l) {
182 match l {
183 | [] -> true
184 | x :: l ->
185 if f x then all(f,l) else false
186 }
187 }
188
189 function rec none(f,l) {
190 match l {
191 | [] -> true
192 | x :: l ->
193 if f x then false else none(f,l)
194 }
195 }
196
197 function nth(l,n) {
198 function rec loop(l,n) {
199 match l {
200 | [] -> invalid_arg()
201 | x :: l ->
202 if n == 0 then x else loop l (n-1)
203 }
204 }
205 if n < 0 then invalid_arg();
206 loop l n
207 }
208
209 function rec filter(f,l) {
210 match l {
211 | [] -> []
212 | x :: l -> if f x then x :: filter f l else filter f l
213 }
214 }
215
216 function array(l) {
217 match l {
218 | [] -> Array.create()
219 | x :: l ->
220 var a = Array.make (1+ List.length l) x;
221 function rec loop(p,l) {
222 match l {
223 | [] -> ()
224 | x :: l ->
225 a.[p] := x;
226 loop (p + 1) l
227 }
228 };
229 loop 1 l;
230 a
231 }
232 }
233
234 function sort(cmp,l) {
235 var a = array l;
236 Array.sort cmp a;
237 Array.list a
238 }