functional: Added functional lib
[nit.git] / lib / functional / test_utils.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2019 Louis-Vincent Boudreault <lv.boudreault95@gmail.com>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # This module contains util classes and methods for `test_iter_extras`.
18 import functional_types
19
20 class SumFn
21 super Fun2[Int,Int,Int]
22
23 redef fun call(x, y)
24 do
25 return x + y
26 end
27 end
28
29 class MinFn[E: Comparable]
30 super Fun2[E,E,E]
31
32 redef fun call(x, y)
33 do
34 if x < y then
35 return x
36 end
37 return y
38 end
39 end
40
41 class InitArrayFn[E]
42 super Fun0[Array[E]]
43
44 var initial_val: nullable E
45
46 redef fun call
47 do
48 var xs = new Array[E]
49 if initial_val != null then
50 xs.push(initial_val)
51 end
52 return xs
53 end
54 end
55
56 class SnakeCaseFn
57 super Fun1[String,String]
58
59 redef fun call(x)
60 do
61 return x.to_snake_case
62 end
63 end
64
65 class UpperCaseFn
66 super Fun1[String, String]
67
68 redef fun call(x)
69 do
70 return x.to_upper
71 end
72 end
73
74 class IsLetterFn
75 super Fun1[Char, Bool]
76
77 redef fun call(c)
78 do
79 return c.is_letter
80 end
81 end
82
83 class AddOneFn
84 super Fun1[Int,Int]
85
86 redef fun call(x)
87 do
88 return x + 1
89 end
90 end
91
92 class CharsFn
93 super Fun1[String, Iterator[Char]]
94
95 redef fun call(str)
96 do
97 return str.chars.iterator
98 end
99 end
100
101 class LowerThanFn
102 super Fun1[Int, Bool]
103 var target: Int
104 redef fun call(x)
105 do
106 return x < target
107 end
108 end
109
110 class IdFn[E]
111 super Fun1[E,E]
112 redef fun call(x)
113 do
114 return x
115 end
116 end
117
118 class StrLenFn
119 super Fun1[String,Int]
120 redef fun call(x)
121 do
122 return x.length
123 end
124 end
125
126 class AddOneProc
127 super Proc1[Ref[Int]]
128 redef fun call(x)
129 do
130 x.item += 1
131 end
132 end
133
134 fun sum_fn: SumFn
135 do
136 return new SumFn
137 end
138
139 fun min_int_fn: MinFn[Int]
140 do
141 return new MinFn[Int]
142 end
143
144 fun new_int_arr(x: nullable Int): InitArrayFn[Int]
145 do
146 return new InitArrayFn[Int](x)
147 end
148
149 fun snake_case_fn: SnakeCaseFn
150 do
151 return new SnakeCaseFn
152 end
153
154 fun upper_fn: UpperCaseFn
155 do
156 return new UpperCaseFn
157 end
158
159 fun is_letter_fn: IsLetterFn
160 do
161 return new IsLetterFn
162 end
163
164 fun add_one: AddOneFn
165 do
166 return new AddOneFn
167 end
168
169 fun add_one_proc: AddOneProc
170 do
171 return new AddOneProc
172 end
173
174 fun chars_fn: CharsFn
175 do
176 return new CharsFn
177 end
178
179 fun lower_than_fn(x: Int): LowerThanFn
180 do
181 return new LowerThanFn(x)
182 end
183
184 fun id_int: IdFn[Int]
185 do
186 return new IdFn[Int]
187 end
188
189 fun id_str: IdFn[String]
190 do
191 return new IdFn[String]
192 end
193
194 fun str_len: StrLenFn
195 do
196 return new StrLenFn
197 end