cohi 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,403 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- compile-command: "rake test" -*-
3
+
4
+ require File.dirname(__FILE__) + '/test_helper.rb'
5
+
6
+ class TestCohi < Test::Unit::TestCase
7
+
8
+ def setup
9
+ end
10
+
11
+ def test_match_pattern?
12
+ f = Function.new(:foo)
13
+ assert_equal([], f.match_pattern?([[]], [[]]))
14
+ assert_equal(nil, f.match_pattern?([[]], [[1]]))
15
+
16
+ assert_equal([1], f.match_pattern?([X], [1]))
17
+ assert_equal(nil, f.match_pattern?([X], []))
18
+
19
+ assert_equal([1, [2, 3]], f.match_pattern?([X_XS], [[1, 2, 3]]))
20
+ assert_equal([1, []], f.match_pattern?([X_XS], [[1]]))
21
+ assert_equal(nil, f.match_pattern?([X_XS], []))
22
+
23
+ assert_equal([2], f.match_pattern?([[1, X]], [[1, 2]]))
24
+ assert_equal([2, [3, 4]], f.match_pattern?([[1, X, XS]], [[1, 2, 3, 4]]))
25
+
26
+ assert_equal([1, 2, [3, 4]], f.match_pattern?([X, X_XS], [1, [2, 3, 4]]))
27
+ end
28
+
29
+ def test_apply
30
+ assert_equal([4, 6, 8], map[(curry(mult, 2) * succ), [1, 2, 3]])
31
+ assert_equal(4, curry(mult, 2) ** succ ** 1)
32
+ assert_equal(4, curry(mult, 2) ** succ[1])
33
+ end
34
+
35
+ def test_add
36
+ assert_equal(3, add[1, 2])
37
+ end
38
+
39
+ def test_fun
40
+ assert_equal(-1, fun(:minus)[1, 2])
41
+ end
42
+
43
+ def test_and_or_not_otherwise
44
+ assert_equal(true, and_[true, true])
45
+ assert_equal(false, and_[false, true])
46
+
47
+ assert_equal(true, or_[true, true])
48
+ assert_equal(true, or_[false, true])
49
+
50
+ assert_equal(false, not_[true])
51
+ assert_equal(true, not_[false])
52
+
53
+ assert_equal(true, otherwise[])
54
+ end
55
+
56
+ def test_fst_snd
57
+ assert_equal(1, fst[[1, 2]])
58
+ assert_equal(2, snd[[1, 2]])
59
+ end
60
+
61
+ def test_curry
62
+ define(:minus2) do |x|
63
+ fst[x] - snd[x]
64
+ end
65
+ assert_equal(-1, minus2[[2, 3]])
66
+ assert_equal(-1, curry_[minus2, 2, 3])
67
+ end
68
+
69
+ def test_uncurry
70
+ assert_equal(-1, minus[2, 3])
71
+ assert_equal(-1, uncurry[minus, [2, 3]])
72
+ end
73
+
74
+ def test_eq_etc
75
+ assert_equal(true, eq[1, 1])
76
+ assert_equal(false, eq[1, 2])
77
+
78
+ assert_equal(true, ne[1, 2])
79
+ assert_equal(false, ne[1, 1])
80
+
81
+ assert_equal(true, lt[1, 2])
82
+ assert_equal(false, lt[1, 1])
83
+
84
+ assert_equal(true, le[1, 2])
85
+ assert_equal(true, le[1, 1])
86
+ assert_equal(false, le[2, 1])
87
+
88
+ assert_equal(true, gt[2, 1])
89
+ assert_equal(false, gt[1, 1])
90
+
91
+ assert_equal(true, ge[2, 1])
92
+ assert_equal(true, ge[1, 1])
93
+ assert_equal(false, ge[1, 2])
94
+ end
95
+
96
+ def test_max_min
97
+ assert_equal(2, max[1, 2])
98
+ assert_equal(2, max[2, 1])
99
+
100
+ assert_equal(1, min[1, 2])
101
+ assert_equal(1, min[2, 1])
102
+ end
103
+
104
+ def test_succ
105
+ assert_equal('2', succ['1'])
106
+ assert_equal(2, succ[1])
107
+ end
108
+
109
+ def test_from_enum
110
+ assert_equal(1, from_enum['1'])
111
+ end
112
+
113
+ def test_add_minus_mult
114
+ assert_equal(3, add[1, 2])
115
+ assert_equal(-1, minus[1, 2])
116
+ assert_equal(6, mult[2, 3])
117
+ end
118
+
119
+ def test_negate_abs_signum
120
+ assert_equal(-1, negate[1])
121
+ assert_equal(1, negate[-1])
122
+
123
+ assert_equal(1, abs[1])
124
+ assert_equal(1, abs[-1])
125
+
126
+ assert_equal(0, signum[0])
127
+ assert_equal(1, signum[1])
128
+ assert_equal(-1, signum[-1])
129
+ end
130
+
131
+ def test_to_rational
132
+ assert_kind_of(Rational, to_rational[1])
133
+ assert_equal(1, to_rational[1])
134
+ end
135
+
136
+ def test_quot_div_rem_mod
137
+ assert_equal(2, quot[5, 2])
138
+ assert_equal(2, div[5, 2])
139
+ assert_equal(-2, quot[-5, 2])
140
+ assert_equal(-3, div[-5, 2])
141
+
142
+ assert_equal(2, rem[5, 3])
143
+ assert_equal(2, mod[5, 3])
144
+ assert_equal(-2, rem[-5, 3])
145
+ assert_equal(1, mod[-5, 3])
146
+
147
+ assert_equal([2, 1], quot_rem[5, 2])
148
+ assert_equal([2, 1], div_mod[5, 2])
149
+ assert_equal([-2, -1], quot_rem[-5, 2])
150
+ assert_equal([-3, 1], div_mod[-5, 2])
151
+ end
152
+
153
+ def test_to_integer
154
+ assert_equal(1, to_integer['1'])
155
+ end
156
+
157
+ def test_fractional
158
+ assert_equal(Rational(2, 1), divr[Rational(8, 2), Rational(2, 1)])
159
+
160
+ assert_equal(Rational(5, 3), recip[Rational(3, 5)])
161
+ end
162
+
163
+ def test_math
164
+ assert_equal(Math::PI, pi[])
165
+ assert_equal(Math::exp(1), exp[1])
166
+ assert_equal(Math::log(1), log[1])
167
+ assert_equal(Math::sqrt(1), sqrt[1])
168
+ assert_equal(8, power[2, 3])
169
+ assert_equal(2, log_base[3, 9])
170
+ assert_equal(Math::sin(1), sin[1])
171
+ assert_equal(Math::cos(1), cos[1])
172
+ assert_equal(Math::tan(1), tan[1])
173
+ assert_equal(Math::asin(1), asin[1])
174
+ assert_equal(Math::acos(1), acos[1])
175
+ assert_equal(Math::atan(1), atan[1])
176
+ assert_equal(Math::sinh(1), sinh[1])
177
+ assert_equal(Math::cosh(1), cosh[1])
178
+ assert_equal(Math::tanh(1), tanh[1])
179
+ assert_equal(Math::asinh(1), asinh[1])
180
+ assert_equal(Math::acosh(1), acosh[1])
181
+ assert_equal(Math::atanh(1), atanh[1])
182
+ end
183
+
184
+ def test_truncate
185
+ assert_equal(1, truncate[1.9])
186
+ assert_equal(1, truncate[1.1])
187
+ assert_equal(-1, truncate[-1.1])
188
+ assert_equal(-1, truncate[-1.9])
189
+
190
+ assert_equal(2, round[1.9])
191
+ assert_equal(1, round[1.1])
192
+ assert_equal(-1, round[-1.1])
193
+ assert_equal(-2, round[-1.9])
194
+
195
+ assert_equal(2, ceil[1.9])
196
+ assert_equal(2, ceil[1.1])
197
+ assert_equal(-1, ceil[-1.1])
198
+ assert_equal(-1, ceil[-1.9])
199
+
200
+ assert_equal(1, floor[1.9])
201
+ assert_equal(1, floor[1.1])
202
+ assert_equal(-2, floor[-1.1])
203
+ assert_equal(-2, floor[-1.9])
204
+ end
205
+
206
+ def test_atan2
207
+ assert_equal(Math.atan2(1, 2), atan2[1, 2])
208
+ end
209
+
210
+ def test_subtract
211
+ assert_equal(-1, subtract[1, 2])
212
+ end
213
+
214
+ def test_even_odd
215
+ assert_equal(true, even[2])
216
+ assert_equal(false, even[1])
217
+ assert_equal(false, odd[2])
218
+ assert_equal(true, odd[1])
219
+ end
220
+
221
+ def test_gcd_lcm
222
+ assert_equal(8, gcd[32, 24])
223
+ assert_equal(8, gcd[24, 32])
224
+
225
+ assert_equal(24, lcm[12, 8])
226
+ assert_equal(24, lcm[8, 12])
227
+ end
228
+
229
+ def test_id_const_as_type_of
230
+ assert_equal(1, id_[1])
231
+ assert_equal(1, const[1, 2])
232
+ assert_equal(1, as_type_of[1, 2])
233
+ end
234
+
235
+ def test_flip
236
+ assert_equal(-1, flip[minus, 2, 1])
237
+ end
238
+
239
+ def test_until
240
+ assert_equal(3, until_[lambda {|i| i == 3 }, lambda {|i| i + 1 }, 0])
241
+ end
242
+
243
+ def test_map
244
+ assert_equal([11, 12, 13], map[curry(add, 10), [1, 2, 3]])
245
+ assert_equal([], map[curry(add, 10), []])
246
+ end
247
+
248
+ def test_append
249
+ assert_equal([1, 2, 3, 4, 5, 6], append[[1, 2, 3], [4, 5, 6]])
250
+ end
251
+
252
+ def test_filter
253
+ assert_equal([1, 2, 3], filter[lambda {|a| a > 0 }, [1, 0, 2, -4, 3, -5]])
254
+ assert_equal([], filter[lambda {|a| a > 0 }, []])
255
+ end
256
+
257
+ def test_head
258
+ assert_equal(1, head[[1, 2, 3]])
259
+ end
260
+
261
+ def test_last
262
+ assert_equal(3, last[[1, 2, 3]])
263
+ end
264
+
265
+ def test_tail
266
+ assert_equal([2, 3], tail[[1, 2, 3]])
267
+ end
268
+
269
+ def test_init
270
+ assert_equal([1, 2], init[[1, 2, 3]])
271
+ end
272
+
273
+ def test_null
274
+ assert_equal(true, null[[]])
275
+ assert_equal(false, null[[1]])
276
+ end
277
+
278
+ def test_length
279
+ assert_equal(3, length[[1, 2, 3]])
280
+ end
281
+
282
+ def test_at
283
+ assert_equal(3, at[[1, 2, 3], 2])
284
+ end
285
+
286
+ def test_reverse
287
+ assert_equal([3, 2, 1], reverse[[1, 2, 3]])
288
+ end
289
+
290
+ def test_foldl
291
+ assert_equal(16, foldl[add, 10, [1, 2, 3]])
292
+ assert_equal(6, foldl1[add, [1, 2, 3]])
293
+ end
294
+
295
+ def test_foldr
296
+ assert_equal(16, foldr[add, 10, [1, 2, 3]])
297
+ assert_equal(6, foldr1[add, [1, 2, 3]])
298
+ end
299
+
300
+ def test_anda_ora
301
+ assert_equal(true, anda[[true, true, true]])
302
+ assert_equal(false, anda[[true, false, true]])
303
+
304
+ assert_equal(true, ora[[false, true, false]])
305
+ assert_equal(false, ora[[false, false, false]])
306
+ end
307
+
308
+ def test_any_all
309
+ assert_equal(true, any[lambda {|a| a > 0}, [-1, 0, 1]])
310
+ assert_equal(false, any[lambda {|a| a > 2}, [-1, 0, 1]])
311
+
312
+ assert_equal(false, all[lambda {|a| a > 0}, [-1, 0, 1]])
313
+ assert_equal(true, all[lambda {|a| a > -2}, [-1, 0, 1]])
314
+ end
315
+
316
+ def test_sum_product
317
+ assert_equal(6, sum[[1, 2, 3]])
318
+ assert_equal(24, product[[1, 2, 3, 4]])
319
+ end
320
+
321
+ def test_concat
322
+ assert_equal([1, 2, 3, 4, 5, 6], concat[[[1, 2, 3], [4, 5], [6]]])
323
+ end
324
+
325
+ def test_concat_map
326
+ assert_equal([2, 3, 4], concat_map[lambda {|a| [a + 1] }, [1, 2, 3]])
327
+ end
328
+
329
+ def test_maximum
330
+ assert_equal(5, maximum[[1, 3, 5, 2, 4]])
331
+ assert_equal(1, minimum[[1, 3, 5, 2, 4]])
332
+ end
333
+
334
+ def test_scan
335
+ assert_equal([10, 11, 13, 16], scanl[add, 10, [1, 2, 3]])
336
+ assert_equal([1, 3, 6], scanl1[add, [1, 2, 3]])
337
+
338
+ assert_equal([16, 15, 13, 10], scanr[add, 10, [1, 2, 3]])
339
+ assert_equal([6, 5, 3], scanr1[add, [1, 2, 3]])
340
+ end
341
+
342
+ def test_replicate
343
+ assert_equal([1, 1, 1], replicate[3, 1])
344
+ end
345
+
346
+ def test_take_drop_split_at
347
+ assert_equal([1, 2, 3], take[3, [1, 2, 3, 4, 5]])
348
+ assert_equal([4, 5], drop[3, [1, 2, 3, 4, 5]])
349
+ assert_equal([[1, 2, 3], [4, 5]], split_at[3, [1, 2, 3, 4, 5]])
350
+ end
351
+
352
+ def test_take_while_drop_while
353
+ assert_equal([1, 2, 3], take_while[lambda {|a| a < 4 }, [1, 2, 3, 4, 5]])
354
+ assert_equal([4, 5], drop_while[lambda {|a| a < 4 }, [1, 2, 3, 4, 5]])
355
+ end
356
+
357
+ def test_span_break
358
+ assert_equal([[1, 2, 3], [4, 5]], span[lambda {|a| a < 4 }, [1, 2, 3, 4, 5]])
359
+ assert_equal([[1, 2, 3], [4, 5]], break_[lambda {|a| a >= 4 }, [1, 2, 3, 4, 5]])
360
+ end
361
+
362
+ def test_elem_not_elem
363
+ assert_equal(true, elem[1, [1, 2, 3, 4, 5]])
364
+ assert_equal(false, elem[0, [1, 2, 3, 4, 5]])
365
+
366
+ assert_equal(false, not_elem[1, [1, 2, 3, 4, 5]])
367
+ assert_equal(true, not_elem[0, [1, 2, 3, 4, 5]])
368
+ end
369
+
370
+ def test_zip_zip3
371
+ assert_equal([[1, 4], [2, 5], [3, 6]], zip[[1, 2, 3], [4, 5, 6]])
372
+ assert_equal([[1, 4, 7], [2, 5, 8], [3, 6, 9]],
373
+ zip3[[1, 2, 3], [4, 5, 6], [7, 8, 9]])
374
+ end
375
+
376
+ def test_zip_with_zip_with3
377
+ assert_equal([5, 7, 9], zip_with[add, [1, 2, 3], [4, 5, 6]])
378
+ assert_equal([12, 15, 18],
379
+ zip_with3[lambda {|a, b, c| a + b + c },
380
+ [1, 2, 3], [4, 5, 6], [7, 8, 9]])
381
+ end
382
+
383
+ def test_unzip_unzip3
384
+ assert_equal([[1, 2, 3], [4, 5, 6]], unzip[[[1, 4], [2, 5], [3, 6]]])
385
+ assert_equal([[1, 2, 3], [4, 5, 6], [7, 8, 9]],
386
+ unzip3[[[1, 4, 7], [2, 5, 8], [3, 6, 9]]])
387
+ end
388
+
389
+ def test_lines_words_unlines_unwords
390
+ assert_equal(['aaa', 'bbb', 'ccc'], lines["aaa\nbbb\nccc\n"])
391
+ assert_equal(['aaa', 'bbb', 'ccc'], words["aaa bbb ccc"])
392
+
393
+ assert_equal("aaa\nbbb\nccc\n", unlines[['aaa', 'bbb', 'ccc']])
394
+ assert_equal('', unlines[[]])
395
+
396
+ assert_equal("aaa bbb ccc", unwords[['aaa', 'bbb', 'ccc']])
397
+ assert_equal('', unwords[[]])
398
+ end
399
+
400
+ def test_show
401
+ assert_equal('[1, 2, 3]', show[[1, 2, 3]])
402
+ end
403
+ end
@@ -0,0 +1,3 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/cohi'
3
+ require File.dirname(__FILE__) + '/../lib/cohi/prelude.rb'
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: cohi
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2007-07-01 00:00:00 +09:00
8
+ summary: Cohi is a tiny library for aiding functional programming.
9
+ require_paths:
10
+ - lib
11
+ email: tanaka.shinya@gmail.com
12
+ homepage: http://cohi.rubyforge.org
13
+ rubyforge_project: cohi
14
+ description: Cohi is a tiny library for aiding functional programming.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - TANAKA Shin-ya(id:ha-tan)
31
+ files:
32
+ - History.txt
33
+ - License.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ - Rakefile
37
+ - example/fact.rb
38
+ - example/fib.rb
39
+ - lib/cohi.rb
40
+ - lib/cohi/prelude.rb
41
+ - lib/cohi/version.rb
42
+ - setup.rb
43
+ - test/test_cohi.rb
44
+ - test/test_helper.rb
45
+ test_files:
46
+ - test/test_cohi.rb
47
+ - test/test_helper.rb
48
+ rdoc_options:
49
+ - --main
50
+ - README.txt
51
+ extra_rdoc_files:
52
+ - History.txt
53
+ - License.txt
54
+ - Manifest.txt
55
+ - README.txt
56
+ executables: []
57
+
58
+ extensions: []
59
+
60
+ requirements: []
61
+
62
+ dependencies: []
63
+