prelude 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +5 -2
- data/README +3 -3
- data/Rakefile +44 -25
- data/doc/classes/Kernel.html +198 -0
- data/doc/classes/Prelude.html +110 -3
- data/doc/classes/Prelude/EmptyListError.html +113 -0
- data/doc/classes/Prelude/List.html +1210 -1067
- data/doc/classes/Prelude/MissingFunctionError.html +113 -0
- data/doc/classes/Prelude/Monad.html +114 -57
- data/doc/classes/Prelude/Tuple.html +18 -18
- data/doc/classes/Proc.html +51 -26
- data/doc/classes/Symbol.html +65 -12
- data/doc/created.rid +1 -1
- data/doc/files/CHANGELOG.html +8 -1
- data/doc/files/README.html +3 -3
- data/doc/files/lib/prelude_rb.html +1 -1
- data/doc/fr_class_index.html +3 -0
- data/doc/fr_method_index.html +113 -97
- data/lib/prelude.rb +84 -10
- data/lib/prelude/list.rb +244 -212
- data/lib/prelude/monad.rb +88 -11
- data/test/tc_higher.rb +5 -5
- data/test/tc_list.rb +259 -268
- data/test/tc_monad.rb +145 -0
- data/test/ts_prelude.rb +2 -1
- metadata +6 -2
data/lib/prelude/monad.rb
CHANGED
@@ -23,29 +23,106 @@
|
|
23
23
|
|
24
24
|
module Prelude
|
25
25
|
|
26
|
-
# $Id: monad.rb
|
26
|
+
# $Id: monad.rb 13 2006-09-11 05:19:16Z prelude $
|
27
27
|
#
|
28
|
-
|
29
|
-
|
28
|
+
module Monad
|
29
|
+
State = {}
|
30
30
|
|
31
|
-
def wrap
|
32
|
-
[
|
31
|
+
def wrap
|
32
|
+
State[object_id] = self
|
33
|
+
self
|
34
|
+
end
|
35
|
+
|
36
|
+
def unwrap
|
37
|
+
State[object_id]
|
33
38
|
end
|
34
39
|
|
35
40
|
def empty
|
36
|
-
[]
|
41
|
+
State[object_id] = nil
|
42
|
+
self
|
37
43
|
end
|
38
44
|
|
39
|
-
def join
|
45
|
+
def Monad.join(arr)
|
40
46
|
r = []
|
41
|
-
each {|a| r.push *a}
|
47
|
+
arr.each {|a| r.push *a}
|
42
48
|
r
|
43
49
|
end
|
44
50
|
|
45
|
-
def bind(
|
46
|
-
|
47
|
-
|
51
|
+
def bind(f=nil)
|
52
|
+
case
|
53
|
+
when f.kind_of?(Symbol) || f.kind_of?(Method) || f.kind_of?(Proc) :
|
54
|
+
State[object_id] = f.to_proc.call(State[object_id])
|
55
|
+
self
|
56
|
+
|
57
|
+
# when f.kind_of?(Proc) :
|
58
|
+
# State[object_id] = f.call(State[object_id])
|
59
|
+
# self
|
60
|
+
|
61
|
+
else
|
62
|
+
f
|
63
|
+
end # case
|
64
|
+
end # bind
|
65
|
+
|
66
|
+
alias << bind
|
48
67
|
|
49
68
|
end # Monad
|
50
69
|
|
70
|
+
# class Maybe < Monad
|
71
|
+
|
72
|
+
# # maybe -- :: b -> (a -> b) -> Maybe a -> b
|
73
|
+
# def maybe
|
74
|
+
# warn "Method 'maybe' is not implemented yet." if $VERBOSE
|
75
|
+
# return []
|
76
|
+
# end
|
77
|
+
|
78
|
+
# # isJust -- :: Maybe a -> Bool
|
79
|
+
# def isJust
|
80
|
+
# warn "Method 'isJust' is not implemented yet." if $VERBOSE
|
81
|
+
# return []
|
82
|
+
# end
|
83
|
+
|
84
|
+
# # isNothing -- :: Maybe a -> Bool
|
85
|
+
# def isNothing
|
86
|
+
# warn "Method 'isNothing' is not implemented yet." if $VERBOSE
|
87
|
+
# return []
|
88
|
+
# end
|
89
|
+
|
90
|
+
# # fromJust -- :: Maybe a -> a
|
91
|
+
# def fromJust
|
92
|
+
# warn "Method 'fromJust' is not implemented yet." if $VERBOSE
|
93
|
+
# return []
|
94
|
+
# end
|
95
|
+
|
96
|
+
# # fromMaybe -- :: a -> Maybe a -> a
|
97
|
+
# def fromMaybe
|
98
|
+
# warn "Method 'fromMaybe' is not implemented yet." if $VERBOSE
|
99
|
+
# return []
|
100
|
+
# end
|
101
|
+
|
102
|
+
# # listToMaybe -- :: [a] -> Maybe a
|
103
|
+
# def listToMaybe
|
104
|
+
# warn "Method 'listToMaybe' is not implemented yet." if $VERBOSE
|
105
|
+
# return []
|
106
|
+
# end
|
107
|
+
|
108
|
+
# # maybeToList -- :: Maybe a -> [a]
|
109
|
+
# def maybeToList
|
110
|
+
# warn "Method 'maybeToList' is not implemented yet." if $VERBOSE
|
111
|
+
# return []
|
112
|
+
# end
|
113
|
+
|
114
|
+
# # catMaybes -- :: [Maybe a] -> [a]
|
115
|
+
# def catMaybes
|
116
|
+
# warn "Method 'catMaybes' is not implemented yet." if $VERBOSE
|
117
|
+
# return []
|
118
|
+
# end
|
119
|
+
|
120
|
+
# # mapMaybe -- :: (a -> Maybe b) -> [a] -> [b]
|
121
|
+
# def mapMaybe
|
122
|
+
# warn "Method 'mapMaybe' is not implemented yet." if $VERBOSE
|
123
|
+
# return []
|
124
|
+
# end
|
125
|
+
|
126
|
+
# end # Maybe
|
127
|
+
|
51
128
|
end # Prelude
|
data/test/tc_higher.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# $Id: tc_higher.rb
|
2
|
+
# $Id: tc_higher.rb 17 2006-09-17 18:03:15Z prelude $
|
3
3
|
#
|
4
4
|
#
|
5
5
|
# This file is part of the Prelude library that provides tools to
|
@@ -65,22 +65,22 @@ class TestHigher < Test::Unit::TestCase
|
|
65
65
|
assert_equal(expect, result)
|
66
66
|
end
|
67
67
|
|
68
|
-
def
|
68
|
+
def test_composition
|
69
69
|
add5 = proc {|x| x+5}
|
70
70
|
add6 = proc {|x,y| x+y+6}
|
71
|
-
add11 = add5
|
71
|
+
add11 = add5 ** add6
|
72
72
|
|
73
73
|
result = add11[1, 2]
|
74
74
|
expect = 14
|
75
75
|
|
76
76
|
assert_equal(expect, result)
|
77
77
|
|
78
|
-
result = add5
|
78
|
+
result = add5 ** 1
|
79
79
|
expect = 6
|
80
80
|
|
81
81
|
assert_equal(expect, result)
|
82
82
|
|
83
|
-
result = add5
|
83
|
+
result = add5 ** add6 ** [1, 2]
|
84
84
|
expect = 14
|
85
85
|
|
86
86
|
assert_equal(expect, result)
|
data/test/tc_list.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# $Id: tc_list.rb
|
2
|
+
# $Id: tc_list.rb 17 2006-09-17 18:03:15Z prelude $
|
3
3
|
#
|
4
4
|
#
|
5
5
|
# This file is part of the Prelude library that provides tools to
|
@@ -26,12 +26,16 @@
|
|
26
26
|
|
27
27
|
class TestList < Test::Unit::TestCase
|
28
28
|
|
29
|
+
include List
|
30
|
+
|
31
|
+
NOT_IMPLEMENTED_YET = []
|
32
|
+
|
29
33
|
def setup
|
30
34
|
|
31
|
-
@a =
|
32
|
-
@true_list =
|
33
|
-
@false_list =
|
34
|
-
@mixed_list =
|
35
|
+
@a = [1, 2, 3, 4, 5]
|
36
|
+
@true_list = [true, true, true]
|
37
|
+
@false_list = [false, false, false]
|
38
|
+
@mixed_list = [false, true, false]
|
35
39
|
|
36
40
|
end # setup
|
37
41
|
|
@@ -39,114 +43,90 @@ class TestList < Test::Unit::TestCase
|
|
39
43
|
# Nothing
|
40
44
|
end # teardown
|
41
45
|
|
46
|
+
# head -- :: [a] -> a
|
42
47
|
def test_head
|
43
|
-
result = @a
|
48
|
+
result = ~head(@a)
|
44
49
|
expect = 1
|
45
|
-
|
46
|
-
assert_equal(expect, result)
|
47
|
-
end
|
48
|
-
|
49
|
-
# Compatibility with the Array class
|
50
|
-
def test_array_plus
|
51
|
-
b = [6, 7]
|
52
|
-
|
53
|
-
result = @a+b
|
54
|
-
expect = [1, 2, 3, 4, 5, 6, 7]
|
55
|
-
|
56
|
-
assert_instance_of(List, result)
|
57
|
-
assert_equal(expect, result)
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_array_assignment
|
61
|
-
@a[1] = 10
|
62
|
-
result = @a
|
63
|
-
expect = [1, 10, 3, 4, 5]
|
64
50
|
|
65
|
-
assert_instance_of(List, result)
|
66
51
|
assert_equal(expect, result)
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_array_shift
|
70
|
-
result = @a << 100 << 101
|
71
|
-
expect = [1, 2, 3, 4, 5, 100, 101]
|
72
52
|
|
73
|
-
|
74
|
-
assert_equal(expect, result)
|
53
|
+
assert_raise(EmptyListError) { ~head([]) }
|
75
54
|
end
|
76
|
-
|
77
|
-
#
|
55
|
+
|
56
|
+
# last -- :: [a] -> a
|
78
57
|
def test_last
|
79
|
-
result = @a
|
58
|
+
result = ~last(@a)
|
80
59
|
expect = 5
|
81
60
|
|
82
61
|
assert_equal(expect, result)
|
62
|
+
|
63
|
+
assert_raise(EmptyListError) { last [] }
|
83
64
|
end
|
84
65
|
|
85
|
-
#
|
66
|
+
# tail -- :: [a] -> [a]
|
86
67
|
def test_tail
|
87
|
-
result = @a
|
68
|
+
result = ~tail(@a)
|
88
69
|
expect = [2, 3, 4, 5]
|
89
70
|
|
90
71
|
assert_equal(expect, result)
|
72
|
+
|
73
|
+
assert_raise(EmptyListError) { ~tail([]) }
|
91
74
|
end
|
92
75
|
|
93
|
-
#
|
76
|
+
# init -- :: [a] -> [a]
|
94
77
|
def test_init
|
95
|
-
result = @a
|
78
|
+
result = ~init(@a)
|
96
79
|
expect = [1, 2, 3, 4]
|
97
80
|
|
98
81
|
assert_equal(expect, result)
|
82
|
+
|
83
|
+
assert_equal( ~init([1]), [])
|
84
|
+
assert_raise(EmptyListError) { ~init([]) }
|
99
85
|
end
|
100
86
|
|
101
|
-
#
|
87
|
+
# null -- :: [a] -> Bool
|
102
88
|
def test_null
|
103
|
-
result = @a
|
89
|
+
result = ~null(@a)
|
104
90
|
expect = false
|
105
91
|
|
106
92
|
assert_equal(expect, result)
|
107
93
|
end
|
108
94
|
|
109
|
-
#
|
95
|
+
# length -- :: [a] -> Int
|
110
96
|
def test_length
|
111
|
-
result = @a
|
97
|
+
result = ~length(@a)
|
112
98
|
expect = 5
|
113
99
|
|
114
100
|
assert_equal(expect, result)
|
115
101
|
end
|
116
102
|
|
117
|
-
#
|
103
|
+
# map -- :: (a -> b) -> [a] -> [b]
|
118
104
|
def test_map
|
119
|
-
result = @a.map{|x| x+1}
|
120
|
-
expect = [2, 3, 4, 5, 6]
|
121
|
-
|
122
|
-
assert_equal(expect, result)
|
123
|
-
|
124
|
-
p = proc {|x| x+1}
|
125
|
-
result = @a.map(&p)
|
126
105
|
expect = [2, 3, 4, 5, 6]
|
127
106
|
|
107
|
+
result = ~map(proc {|x| x+1}, @a)
|
128
108
|
assert_equal(expect, result)
|
129
109
|
end
|
130
110
|
|
131
|
-
#
|
111
|
+
# reverse -- :: [a] -> [a]
|
132
112
|
def test_reverse
|
133
|
-
result = @a
|
113
|
+
result = ~reverse(@a)
|
134
114
|
expect = [5, 4, 3, 2, 1]
|
135
115
|
|
136
116
|
assert_equal(expect, result)
|
137
117
|
end
|
138
118
|
|
139
|
-
#
|
119
|
+
# intersperse -- :: a -> [a] -> [a]
|
140
120
|
def test_intersperse
|
141
|
-
result = @a
|
142
|
-
expect =
|
121
|
+
result = ~intersperse(@a)
|
122
|
+
expect = NOT_IMPLEMENTED_YET
|
143
123
|
|
144
124
|
assert_equal(expect, result)
|
145
125
|
end
|
146
126
|
|
147
|
-
#
|
127
|
+
# transpose -- :: [[a]] -> [[a]]
|
148
128
|
def test_transpose
|
149
|
-
result =
|
129
|
+
result = ~transpose([@a, @a, @a])
|
150
130
|
expect = [[1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4], [5, 5, 5] ]
|
151
131
|
|
152
132
|
assert_equal(expect, result)
|
@@ -154,173 +134,178 @@ class TestList < Test::Unit::TestCase
|
|
154
134
|
|
155
135
|
# -- * Reducing lists (folds)
|
156
136
|
|
157
|
-
#
|
137
|
+
# foldl -- :: (a -> b -> a) -> a -> [b] -> a
|
158
138
|
|
159
|
-
|
160
|
-
# arrays of any usable size... So don't use it, it's here for completeness
|
161
|
-
def test_f_foldl
|
162
|
-
result = @a.f_foldl(0){ |x, y| x+y}
|
139
|
+
def test_foldl
|
163
140
|
expect = 15
|
164
141
|
|
142
|
+
result = ~foldl(proc{ |x, y| x+y}, 0, @a )
|
165
143
|
assert_equal(expect, result)
|
166
|
-
end
|
167
|
-
|
168
|
-
# This is a more pedestrian iterative version.
|
169
|
-
def test_foldl
|
170
|
-
result = @a.foldl(0){ |x, y| x+y}
|
171
|
-
expect = 15
|
172
144
|
|
145
|
+
result = ~foldl(-:+, 0, @a )
|
173
146
|
assert_equal(expect, result)
|
174
147
|
end
|
175
148
|
|
176
|
-
#
|
149
|
+
# foldl' -- :: (a -> b -> a) -> a -> [b] -> a
|
177
150
|
def test_foldl_
|
178
|
-
result = @a
|
179
|
-
expect =
|
151
|
+
result = ~foldl_(@a)
|
152
|
+
expect = NOT_IMPLEMENTED_YET
|
180
153
|
|
181
154
|
assert_equal(expect, result)
|
182
155
|
end
|
183
156
|
|
184
|
-
#
|
157
|
+
# foldl1 -- :: (a -> a -> a) -> [a] -> a
|
185
158
|
def test_foldl1
|
186
|
-
|
187
|
-
expect = 120
|
159
|
+
expect = 15
|
188
160
|
|
161
|
+
result = ~foldl1( proc{ |x, y| x+y}, @a )
|
162
|
+
assert_equal(expect, result)
|
163
|
+
|
164
|
+
result = ~foldl1(-:+, @a )
|
189
165
|
assert_equal(expect, result)
|
190
166
|
end
|
191
167
|
|
192
|
-
#
|
168
|
+
# foldl1' -- :: (a -> a -> a) -> [a] -> a
|
193
169
|
def test_foldl1_
|
194
|
-
result = @a
|
195
|
-
expect =
|
170
|
+
result = ~foldl1_(@a)
|
171
|
+
expect = NOT_IMPLEMENTED_YET
|
196
172
|
|
197
173
|
assert_equal(expect, result)
|
198
174
|
end
|
199
175
|
|
200
|
-
#
|
176
|
+
# foldr -- :: (a -> b -> b) -> b -> [a] -> b
|
201
177
|
def test_foldr
|
202
|
-
result = @a.foldr(0){ |x, y| x+y}
|
203
178
|
expect = 15
|
204
179
|
|
180
|
+
result = ~foldr(proc{ |x, y| x+y}, 0, @a)
|
181
|
+
assert_equal(expect, result)
|
182
|
+
|
183
|
+
result = ~foldr(-:+, 0, @a )
|
205
184
|
assert_equal(expect, result)
|
206
185
|
end
|
207
186
|
|
208
|
-
#
|
187
|
+
# foldr1 -- :: (a -> a -> a) -> [a] -> a
|
209
188
|
def test_foldr1
|
210
|
-
|
211
|
-
expect = 120
|
189
|
+
expect = 15
|
212
190
|
|
191
|
+
result = ~foldr1(proc{ |x, y| x+y}, @a)
|
192
|
+
assert_equal(expect, result)
|
193
|
+
|
194
|
+
result = ~foldr1(-:+, @a)
|
213
195
|
assert_equal(expect, result)
|
214
196
|
end
|
215
197
|
|
216
198
|
# -- ** Special folds
|
217
199
|
|
218
|
-
#
|
200
|
+
# concat -- :: [[a]] -> [a]
|
219
201
|
def test_concat
|
220
|
-
result =
|
221
|
-
expect =
|
202
|
+
result = ~concat( [[1, 2, [1, 2]], [1, 2]] )
|
203
|
+
expect = [1, 2, [1, 2], 1, 2]
|
222
204
|
|
223
205
|
assert_equal(expect, result)
|
224
206
|
end
|
225
207
|
|
226
|
-
#
|
208
|
+
# concatMap -- :: (a -> [b]) -> [a] -> [b]
|
227
209
|
def test_concatMap
|
228
|
-
result = @a
|
229
|
-
expect =
|
210
|
+
result = ~concatMap(@a)
|
211
|
+
expect = NOT_IMPLEMENTED_YET
|
230
212
|
|
231
213
|
assert_equal(expect, result)
|
232
214
|
end
|
233
215
|
|
234
|
-
#
|
216
|
+
# and -- :: [Bool] -> Bool
|
235
217
|
def test_and
|
236
|
-
result = @true_list
|
218
|
+
result = ~and_(@true_list)
|
237
219
|
expect = true
|
238
220
|
|
239
221
|
assert_equal(expect, result)
|
240
222
|
|
241
|
-
result = @mixed_list
|
223
|
+
result = ~and_(@mixed_list)
|
242
224
|
expect = false
|
243
225
|
|
244
226
|
assert_equal(expect, result)
|
245
227
|
|
246
|
-
result = @false_list
|
228
|
+
result = ~and_(@false_list)
|
247
229
|
expect = false
|
248
230
|
|
249
231
|
assert_equal(expect, result)
|
250
232
|
end
|
251
233
|
|
252
|
-
#
|
234
|
+
# or -- :: [Bool] -> Bool
|
253
235
|
def test_or
|
254
|
-
result = @true_list
|
236
|
+
result = ~or_(@true_list)
|
255
237
|
expect = true
|
256
238
|
|
257
239
|
assert_equal(expect, result)
|
258
240
|
|
259
|
-
result = @mixed_list
|
241
|
+
result = ~or_(@mixed_list)
|
260
242
|
expect = true
|
261
243
|
|
262
244
|
assert_equal(expect, result)
|
263
245
|
|
264
|
-
result = @false_list
|
246
|
+
result = ~or_(@false_list)
|
265
247
|
expect = false
|
266
248
|
|
267
249
|
assert_equal(expect, result)
|
268
250
|
end
|
269
251
|
|
270
|
-
#
|
252
|
+
# any -- :: (a -> Bool) -> [a] -> Bool
|
271
253
|
def test_any
|
272
|
-
|
254
|
+
f1 = proc {|x| x>5}
|
255
|
+
f2 = proc {|x| x<5}
|
256
|
+
|
257
|
+
result = ~any(f1, @a)
|
273
258
|
expect = false
|
274
259
|
|
275
260
|
assert_equal(expect, result)
|
276
261
|
|
277
|
-
result = @a
|
262
|
+
result = ~any(f2, @a)
|
278
263
|
expect = true
|
279
264
|
|
280
265
|
assert_equal(expect, result)
|
281
266
|
end
|
282
267
|
|
283
|
-
#
|
268
|
+
# all -- :: (a -> Bool) -> [a] -> Bool
|
284
269
|
def test_all
|
285
|
-
result =
|
270
|
+
result = ~all(proc {|x| x>3}, @a)
|
286
271
|
expect = false
|
287
272
|
|
288
273
|
assert_equal(expect, result)
|
289
274
|
|
290
|
-
result =
|
275
|
+
result = ~all(proc {|x| x>0}, @a)
|
291
276
|
expect = true
|
292
277
|
|
293
278
|
assert_equal(expect, result)
|
294
279
|
end
|
295
280
|
|
296
|
-
#
|
281
|
+
# sum -- :: (Num a) => [a] -> a
|
297
282
|
def test_sum
|
298
|
-
result = @a
|
299
|
-
expect =
|
283
|
+
result = ~sum(@a)
|
284
|
+
expect = NOT_IMPLEMENTED_YET
|
300
285
|
|
301
286
|
assert_equal(expect, result)
|
302
287
|
end
|
303
288
|
|
304
|
-
#
|
289
|
+
# product -- :: (Num a) => [a] -> a
|
305
290
|
def test_product
|
306
|
-
result = @a
|
307
|
-
expect =
|
291
|
+
result = ~product(@a)
|
292
|
+
expect = NOT_IMPLEMENTED_YET
|
308
293
|
|
309
294
|
assert_equal(expect, result)
|
310
295
|
end
|
311
296
|
|
312
|
-
#
|
297
|
+
# maximum -- :: (Ord a) => [a] -> a
|
313
298
|
def test_maximum
|
314
|
-
result = @a
|
315
|
-
expect =
|
299
|
+
result = ~maximum(@a)
|
300
|
+
expect = NOT_IMPLEMENTED_YET
|
316
301
|
|
317
302
|
assert_equal(expect, result)
|
318
303
|
end
|
319
304
|
|
320
|
-
#
|
305
|
+
# minimum -- :: (Ord a) => [a] -> a
|
321
306
|
def test_minimum
|
322
|
-
result = @a
|
323
|
-
expect =
|
307
|
+
result = ~minimum(@a)
|
308
|
+
expect = NOT_IMPLEMENTED_YET
|
324
309
|
|
325
310
|
assert_equal(expect, result)
|
326
311
|
end
|
@@ -329,96 +314,96 @@ class TestList < Test::Unit::TestCase
|
|
329
314
|
# -- * Building lists
|
330
315
|
|
331
316
|
# -- ** Scans
|
332
|
-
#
|
317
|
+
# scanl -- :: (a -> b -> a) -> a -> [b] -> [a]
|
333
318
|
def test_scanl
|
334
|
-
result =
|
335
|
-
expect = [0, 1, 3, 6, 10, 15]
|
319
|
+
result = ~scanl(lambda{ |x, y| x+y}, 0, @a)
|
320
|
+
expect = NOT_IMPLEMENTED_YET #[0, 1, 3, 6, 10, 15]
|
336
321
|
|
337
322
|
assert_equal(expect, result)
|
338
323
|
end
|
339
324
|
|
340
|
-
#
|
325
|
+
# scanl1 -- :: (a -> a -> a) -> [a] -> [a]
|
341
326
|
def test_scanl1
|
342
|
-
result =
|
343
|
-
expect = [1, 3, 6, 10, 15]
|
327
|
+
result = ~scanl1( lambda { |x, y| x+y }, @a)
|
328
|
+
expect = NOT_IMPLEMENTED_YET #[1, 3, 6, 10, 15]
|
344
329
|
|
345
330
|
assert_equal(expect, result)
|
346
331
|
end
|
347
332
|
|
348
|
-
#
|
333
|
+
# scanr -- :: (a -> b -> b) -> b -> [a] -> [b]
|
349
334
|
def test_scanr
|
350
|
-
result =
|
351
|
-
expect = [0, 1, 3, 6, 10, 15]
|
335
|
+
result = ~scanr(lambda { |x, y| x+y }, 0, @a)
|
336
|
+
expect = NOT_IMPLEMENTED_YET #[0, 1, 3, 6, 10, 15]
|
352
337
|
|
353
338
|
assert_equal(expect, result)
|
354
339
|
end
|
355
340
|
|
356
|
-
#
|
341
|
+
# scanr1 -- :: (a -> a -> a) -> [a] -> [a]
|
357
342
|
def test_scanr1
|
358
|
-
result =
|
359
|
-
expect = [1, 3, 6, 10, 15]
|
343
|
+
result = ~scanr1(lambda { |x, y| x+y }, @a)
|
344
|
+
expect = NOT_IMPLEMENTED_YET #[1, 3, 6, 10, 15]
|
360
345
|
|
361
346
|
assert_equal(expect, result)
|
362
347
|
end
|
363
348
|
|
364
349
|
|
365
350
|
# -- ** Accumulating maps
|
366
|
-
#
|
351
|
+
# mapAccumL -- :: (a -> b -> (a,c)) -> a -> [b] -> (a,[c])
|
367
352
|
def test_mapAccumL
|
368
|
-
result = @a
|
369
|
-
expect =
|
353
|
+
result = ~mapAccumL(@a)
|
354
|
+
expect = NOT_IMPLEMENTED_YET
|
370
355
|
|
371
356
|
assert_equal(expect, result)
|
372
357
|
end
|
373
358
|
|
374
|
-
#
|
359
|
+
# mapAccumR -- :: (a -> b -> (a,c)) -> a -> [b] -> (a,[c])
|
375
360
|
def test_mapAccumR
|
376
|
-
result = @a
|
377
|
-
expect =
|
361
|
+
result = ~mapAccumR(@a)
|
362
|
+
expect = NOT_IMPLEMENTED_YET
|
378
363
|
|
379
364
|
assert_equal(expect, result)
|
380
365
|
end
|
381
366
|
|
382
367
|
|
383
368
|
# -- ** Infinite lists
|
384
|
-
#
|
369
|
+
# iterate -- :: (a -> a) -> a -> [a]
|
385
370
|
def test_iterate
|
386
|
-
result = @a
|
387
|
-
expect =
|
371
|
+
result = ~iterate(@a)
|
372
|
+
expect = NOT_IMPLEMENTED_YET
|
388
373
|
|
389
374
|
assert_equal(expect, result)
|
390
375
|
end
|
391
376
|
|
392
|
-
#
|
377
|
+
# repeat -- :: a -> [a]
|
393
378
|
def test_repeat
|
394
|
-
result = @a
|
395
|
-
expect =
|
379
|
+
result = ~repeat(@a)
|
380
|
+
expect = NOT_IMPLEMENTED_YET
|
396
381
|
|
397
382
|
assert_equal(expect, result)
|
398
383
|
end
|
399
384
|
|
400
|
-
#
|
385
|
+
# replicate -- :: Int -> a -> [a]
|
401
386
|
def test_replicate
|
402
|
-
result = @a
|
403
|
-
expect =
|
387
|
+
result = ~replicate(@a)
|
388
|
+
expect = NOT_IMPLEMENTED_YET
|
404
389
|
|
405
390
|
assert_equal(expect, result)
|
406
391
|
end
|
407
392
|
|
408
|
-
#
|
393
|
+
# cycle -- :: [a] -> [a]
|
409
394
|
def test_cycle
|
410
|
-
result = @a
|
411
|
-
expect =
|
395
|
+
result = ~cycle(@a)
|
396
|
+
expect = NOT_IMPLEMENTED_YET
|
412
397
|
|
413
398
|
assert_equal(expect, result)
|
414
399
|
end
|
415
400
|
|
416
401
|
|
417
402
|
# -- ** Unfolding
|
418
|
-
#
|
403
|
+
# unfoldr -- :: (b -> Maybe (a, b)) -> b -> [a]
|
419
404
|
def test_unfoldr
|
420
|
-
result = @a
|
421
|
-
expect =
|
405
|
+
result = ~unfoldr(@a)
|
406
|
+
expect = NOT_IMPLEMENTED_YET
|
422
407
|
|
423
408
|
assert_equal(expect, result)
|
424
409
|
end
|
@@ -427,99 +412,99 @@ class TestList < Test::Unit::TestCase
|
|
427
412
|
# -- * Sublists
|
428
413
|
|
429
414
|
# -- ** Extracting sublists
|
430
|
-
#
|
415
|
+
# take -- :: Int -> [a] -> [a]
|
431
416
|
def test_take
|
432
|
-
result =
|
417
|
+
result = ~take(2, @a)
|
433
418
|
expect = [1, 2]
|
434
419
|
|
435
420
|
assert_equal(expect, result)
|
436
421
|
end
|
437
422
|
|
438
|
-
#
|
423
|
+
# drop -- :: Int -> [a] -> [a]
|
439
424
|
def test_drop
|
440
|
-
result =
|
425
|
+
result = ~drop(2, @a)
|
441
426
|
expect = [3, 4, 5]
|
442
427
|
|
443
428
|
assert_equal(expect, result)
|
444
429
|
end
|
445
430
|
|
446
|
-
#
|
431
|
+
# splitAt -- :: Int -> [a] -> ([a], [a])
|
447
432
|
def test_split_at
|
448
|
-
result =
|
433
|
+
result = ~split_at(2, @a)
|
449
434
|
expect = [[1, 2],[3, 4, 5]]
|
450
435
|
|
451
436
|
assert_equal(expect, result)
|
452
437
|
end
|
453
438
|
|
454
|
-
#
|
439
|
+
# takeWhile -- :: (a -> Bool) -> [a] -> [a]
|
455
440
|
def test_takeWhile
|
456
|
-
result =
|
441
|
+
result = ~takeWhile(lambda {|x| x<4}, @a)
|
457
442
|
expect = [1, 2, 3]
|
458
443
|
|
459
444
|
assert_equal(expect, result)
|
460
445
|
end
|
461
446
|
|
462
|
-
#
|
447
|
+
# dropWhile -- :: (a -> Bool) -> [a] -> [a]
|
463
448
|
def test_dropWhile
|
464
|
-
result =
|
449
|
+
result = ~dropWhile(lambda {|x| x<4}, @a)
|
465
450
|
expect = [4, 5]
|
466
451
|
|
467
452
|
assert_equal(expect, result)
|
468
453
|
end
|
469
454
|
|
470
|
-
#
|
455
|
+
# span -- :: (a -> Bool) -> [a] -> ([a], [a])
|
471
456
|
def test_span
|
472
|
-
result =
|
457
|
+
result = ~span(lambda {|x| x<3}, @a)
|
473
458
|
expect = [[1, 2], [3, 4, 5]]
|
474
459
|
|
475
460
|
assert_equal(expect, result)
|
476
461
|
end
|
477
462
|
|
478
|
-
#
|
479
|
-
def
|
480
|
-
result = @a
|
481
|
-
expect =
|
463
|
+
# break -- :: (a -> Bool) -> [a] -> ([a], [a])
|
464
|
+
def test_break_
|
465
|
+
result = ~break_(@a)
|
466
|
+
expect = NOT_IMPLEMENTED_YET
|
482
467
|
|
483
468
|
assert_equal(expect, result)
|
484
469
|
end
|
485
470
|
|
486
|
-
#
|
471
|
+
# group -- :: Eq a => [a] -> [[a]]
|
487
472
|
def test_group
|
488
|
-
result = @a
|
489
|
-
expect =
|
473
|
+
result = ~group(@a)
|
474
|
+
expect = NOT_IMPLEMENTED_YET
|
490
475
|
|
491
476
|
assert_equal(expect, result)
|
492
477
|
end
|
493
478
|
|
494
|
-
#
|
479
|
+
# inits -- :: [a] -> [[a]]
|
495
480
|
def test_inits
|
496
|
-
result = @a
|
497
|
-
expect =
|
481
|
+
result = ~inits(@a)
|
482
|
+
expect = NOT_IMPLEMENTED_YET
|
498
483
|
|
499
484
|
assert_equal(expect, result)
|
500
485
|
end
|
501
486
|
|
502
|
-
#
|
487
|
+
# tails -- :: [a] -> [[a]]
|
503
488
|
def test_tails
|
504
|
-
result = @a
|
505
|
-
expect =
|
489
|
+
result = ~tails(@a)
|
490
|
+
expect = NOT_IMPLEMENTED_YET
|
506
491
|
|
507
492
|
assert_equal(expect, result)
|
508
493
|
end
|
509
494
|
|
510
495
|
# -- ** Predicates
|
511
|
-
#
|
496
|
+
# isPrefixOf -- :: (Eq a) => [a] -> [a] -> Bool
|
512
497
|
def test_isPrefixOf
|
513
|
-
result = @a
|
514
|
-
expect =
|
498
|
+
result = ~isPrefixOf(@a)
|
499
|
+
expect = NOT_IMPLEMENTED_YET
|
515
500
|
|
516
501
|
assert_equal(expect, result)
|
517
502
|
end
|
518
503
|
|
519
|
-
#
|
504
|
+
# isSuffixOf -- :: (Eq a) => [a] -> [a] -> Bool
|
520
505
|
def test_isSuffixOf
|
521
|
-
result = @a
|
522
|
-
expect =
|
506
|
+
result = ~isSuffixOf(@a)
|
507
|
+
expect = NOT_IMPLEMENTED_YET
|
523
508
|
|
524
509
|
assert_equal(expect, result)
|
525
510
|
end
|
@@ -528,51 +513,51 @@ class TestList < Test::Unit::TestCase
|
|
528
513
|
# -- * Searching lists
|
529
514
|
|
530
515
|
# -- ** Searching by equality
|
531
|
-
#
|
516
|
+
# elem -- :: a -> [a] -> Bool
|
532
517
|
def test_elem
|
533
|
-
result = @a
|
534
|
-
expect =
|
518
|
+
result = ~elem(@a)
|
519
|
+
expect = NOT_IMPLEMENTED_YET
|
535
520
|
|
536
521
|
assert_equal(expect, result)
|
537
522
|
end
|
538
523
|
|
539
|
-
#
|
524
|
+
# notElem -- :: a -> [a] -> Bool
|
540
525
|
def test_notElem
|
541
|
-
result = @a
|
542
|
-
expect =
|
526
|
+
result = ~notElem(@a)
|
527
|
+
expect = NOT_IMPLEMENTED_YET
|
543
528
|
|
544
529
|
assert_equal(expect, result)
|
545
530
|
end
|
546
531
|
|
547
|
-
#
|
532
|
+
# lookup -- :: (Eq a) => a -> [(a,b)] -> Maybe b
|
548
533
|
def test_lookup
|
549
|
-
result = @a
|
550
|
-
expect =
|
534
|
+
result = ~lookup(@a)
|
535
|
+
expect = NOT_IMPLEMENTED_YET
|
551
536
|
|
552
537
|
assert_equal(expect, result)
|
553
538
|
end
|
554
539
|
|
555
540
|
# -- ** Searching with a predicate
|
556
|
-
#
|
541
|
+
# find -- :: (a -> Bool) -> [a] -> Maybe a
|
557
542
|
def test_find
|
558
|
-
result =
|
559
|
-
expect =
|
543
|
+
result = ~find(lambda {|x| x==3}, @a)
|
544
|
+
expect = NOT_IMPLEMENTED_YET
|
560
545
|
|
561
546
|
assert_equal(expect, result)
|
562
547
|
end
|
563
548
|
|
564
|
-
#
|
549
|
+
# filter -- :: (a -> Bool) -> [a] -> [a]
|
565
550
|
def test_filter
|
566
|
-
result = @a
|
567
|
-
expect =
|
551
|
+
result = ~filter(@a)
|
552
|
+
expect = NOT_IMPLEMENTED_YET
|
568
553
|
|
569
554
|
assert_equal(expect, result)
|
570
555
|
end
|
571
556
|
|
572
|
-
#
|
557
|
+
# partition -- :: (a -> Bool) -> [a] -> ([a], [a])
|
573
558
|
def test_partition
|
574
|
-
result =
|
575
|
-
expect =
|
559
|
+
result = ~partition(lambda {|x| x>3}, @a)
|
560
|
+
expect = NOT_IMPLEMENTED_YET
|
576
561
|
|
577
562
|
assert_equal(expect, result)
|
578
563
|
end
|
@@ -582,39 +567,39 @@ class TestList < Test::Unit::TestCase
|
|
582
567
|
# -- | These functions treat a list @xs@ as a indexed collection,
|
583
568
|
# -- with indices ranging from 0 to @'length' xs - 1@.
|
584
569
|
|
585
|
-
#
|
570
|
+
# (!!) -- :: [a] -> Int -> a
|
586
571
|
# Don't know how to implement it in Ruby
|
587
572
|
|
588
573
|
|
589
|
-
#
|
574
|
+
# elemIndex -- :: (Eq a) => a -> [a] -> Maybe Int
|
590
575
|
def test_elemIndex
|
591
|
-
result = @a
|
592
|
-
expect =
|
576
|
+
result = ~elemIndex(@a)
|
577
|
+
expect = NOT_IMPLEMENTED_YET
|
593
578
|
|
594
579
|
assert_equal(expect, result)
|
595
580
|
end
|
596
581
|
|
597
|
-
#
|
582
|
+
# elemIndices -- :: (Eq a) => a -> [a] -> [Int]
|
598
583
|
def test_elemIndices
|
599
|
-
result = @a
|
600
|
-
expect =
|
584
|
+
result = ~elemIndices(@a)
|
585
|
+
expect = NOT_IMPLEMENTED_YET
|
601
586
|
|
602
587
|
assert_equal(expect, result)
|
603
588
|
end
|
604
589
|
|
605
590
|
|
606
|
-
#
|
591
|
+
# findIndex -- :: (a -> Bool) -> [a] -> Maybe Int
|
607
592
|
def test_findIndex
|
608
|
-
result = @a
|
609
|
-
expect =
|
593
|
+
result = ~findIndex(@a)
|
594
|
+
expect = NOT_IMPLEMENTED_YET
|
610
595
|
|
611
596
|
assert_equal(expect, result)
|
612
597
|
end
|
613
598
|
|
614
|
-
#
|
599
|
+
# findIndices -- :: (a -> Bool) -> [a] -> [Int]
|
615
600
|
def test_findIndices
|
616
|
-
result = @a
|
617
|
-
expect =
|
601
|
+
result = ~findIndices(@a)
|
602
|
+
expect = NOT_IMPLEMENTED_YET
|
618
603
|
|
619
604
|
assert_equal(expect, result)
|
620
605
|
end
|
@@ -622,76 +607,76 @@ class TestList < Test::Unit::TestCase
|
|
622
607
|
|
623
608
|
# -- * Zipping and unzipping lists
|
624
609
|
|
625
|
-
#
|
610
|
+
# zip -- :: [a] -> [b] -> [(a,b)]
|
626
611
|
def test_zip
|
627
|
-
result =
|
628
|
-
expect = [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5] ]
|
612
|
+
result = ~zip(@a, @a)
|
613
|
+
expect = NOT_IMPLEMENTED_YET #[[1, 1], [2, 2], [3, 3], [4, 4], [5, 5] ]
|
629
614
|
|
630
615
|
assert_equal(expect, result)
|
631
616
|
end
|
632
617
|
|
633
|
-
#
|
618
|
+
# zip3
|
634
619
|
def test_zip3
|
635
|
-
result = @a
|
636
|
-
expect =
|
620
|
+
result = ~zip3(@a)
|
621
|
+
expect = NOT_IMPLEMENTED_YET
|
637
622
|
|
638
623
|
assert_equal(expect, result)
|
639
624
|
end
|
640
625
|
|
641
|
-
#
|
626
|
+
# zip4, zip5, zip6, zip7
|
642
627
|
def test_zip4
|
643
|
-
result = @a
|
644
|
-
expect =
|
628
|
+
result = ~zip4(@a)
|
629
|
+
expect = NOT_IMPLEMENTED_YET
|
645
630
|
|
646
631
|
assert_equal(expect, result)
|
647
632
|
end
|
648
633
|
|
649
634
|
|
650
|
-
#
|
635
|
+
# zipWith -- :: (a -> b -> c) -> [a] -> [b] -> [c]
|
651
636
|
def test_zipWith
|
652
|
-
result = @a
|
653
|
-
expect =
|
637
|
+
result = ~zipWith(@a)
|
638
|
+
expect = NOT_IMPLEMENTED_YET
|
654
639
|
|
655
640
|
assert_equal(expect, result)
|
656
641
|
end
|
657
642
|
|
658
|
-
#
|
643
|
+
# zipWith3
|
659
644
|
def test_zipWith3
|
660
|
-
result = @a
|
661
|
-
expect =
|
645
|
+
result = ~zipWith3(@a)
|
646
|
+
expect = NOT_IMPLEMENTED_YET
|
662
647
|
|
663
648
|
assert_equal(expect, result)
|
664
649
|
end
|
665
650
|
|
666
|
-
#
|
651
|
+
# zipWith4, zipWith5, zipWith6, zipWith7
|
667
652
|
def test_zipWith4
|
668
|
-
result = @a
|
669
|
-
expect =
|
653
|
+
result = ~zipWith4(@a)
|
654
|
+
expect = NOT_IMPLEMENTED_YET
|
670
655
|
|
671
656
|
assert_equal(expect, result)
|
672
657
|
end
|
673
658
|
|
674
659
|
|
675
|
-
#
|
660
|
+
# unzip -- :: [(a,b)] -> ([a],[b])
|
676
661
|
def test_unzip
|
677
|
-
result = @a
|
678
|
-
expect =
|
662
|
+
result = ~unzip(@a)
|
663
|
+
expect = NOT_IMPLEMENTED_YET
|
679
664
|
|
680
665
|
assert_equal(expect, result)
|
681
666
|
end
|
682
667
|
|
683
|
-
#
|
668
|
+
# unzip3
|
684
669
|
def test_unzip3
|
685
|
-
result = @a
|
686
|
-
expect =
|
670
|
+
result = ~unzip3(@a)
|
671
|
+
expect = NOT_IMPLEMENTED_YET
|
687
672
|
|
688
673
|
assert_equal(expect, result)
|
689
674
|
end
|
690
675
|
|
691
|
-
#
|
676
|
+
# unzip4, unzip5, unzip6, unzip7
|
692
677
|
def test_unzip4
|
693
|
-
result = @a
|
694
|
-
expect =
|
678
|
+
result = ~unzip4(@a)
|
679
|
+
expect = NOT_IMPLEMENTED_YET
|
695
680
|
|
696
681
|
assert_equal(expect, result)
|
697
682
|
end
|
@@ -700,34 +685,34 @@ class TestList < Test::Unit::TestCase
|
|
700
685
|
# -- * Special lists
|
701
686
|
|
702
687
|
# -- ** Functions on strings
|
703
|
-
#
|
688
|
+
# lines -- :: String -> [String]
|
704
689
|
def test_lines
|
705
|
-
result = @a
|
706
|
-
expect =
|
690
|
+
result = ~lines(@a)
|
691
|
+
expect = NOT_IMPLEMENTED_YET
|
707
692
|
|
708
693
|
assert_equal(expect, result)
|
709
694
|
end
|
710
695
|
|
711
|
-
#
|
696
|
+
# words -- :: String -> [String]
|
712
697
|
def test_words
|
713
|
-
result = @a
|
714
|
-
expect =
|
698
|
+
result = ~words(@a)
|
699
|
+
expect = NOT_IMPLEMENTED_YET
|
715
700
|
|
716
701
|
assert_equal(expect, result)
|
717
702
|
end
|
718
703
|
|
719
|
-
#
|
704
|
+
# unlines -- :: [String] -> String
|
720
705
|
def test_unlines
|
721
|
-
result = @a
|
722
|
-
expect =
|
706
|
+
result = ~unlines(@a)
|
707
|
+
expect = NOT_IMPLEMENTED_YET
|
723
708
|
|
724
709
|
assert_equal(expect, result)
|
725
710
|
end
|
726
711
|
|
727
|
-
#
|
712
|
+
# unwords -- :: [String] -> String
|
728
713
|
def test_unwords
|
729
|
-
result = @a
|
730
|
-
expect =
|
714
|
+
result = ~unwords(@a)
|
715
|
+
expect = NOT_IMPLEMENTED_YET
|
731
716
|
|
732
717
|
assert_equal(expect, result)
|
733
718
|
end
|
@@ -735,52 +720,58 @@ class TestList < Test::Unit::TestCase
|
|
735
720
|
|
736
721
|
# -- ** \"Set\" operations
|
737
722
|
|
738
|
-
#
|
723
|
+
# nub -- :: (Eq a) => [a] -> [a]
|
739
724
|
def test_nub
|
740
|
-
result = @a
|
741
|
-
expect =
|
725
|
+
result = ~nub(@a)
|
726
|
+
expect = NOT_IMPLEMENTED_YET
|
742
727
|
|
743
728
|
assert_equal(expect, result)
|
744
729
|
end
|
745
730
|
|
746
731
|
|
747
|
-
#
|
732
|
+
# delete -- :: (Eq a) => a -> [a] -> [a]
|
748
733
|
def test_delete
|
749
|
-
result =
|
750
|
-
expect =
|
734
|
+
result = ~delete(3, @a)
|
735
|
+
expect = NOT_IMPLEMENTED_YET
|
751
736
|
|
752
737
|
assert_equal(expect, result)
|
753
738
|
end
|
754
739
|
|
755
|
-
#
|
740
|
+
# (\\) -- :: (Eq a) => [a] -> [a] -> [a]
|
756
741
|
# Don't know how to implement it in Ruby
|
757
742
|
|
758
|
-
#
|
743
|
+
# union -- :: (Eq a) => [a] -> [a] -> [a]
|
759
744
|
def test_union
|
760
|
-
result = @a
|
761
|
-
expect =
|
745
|
+
result = ~union(@a)
|
746
|
+
expect = NOT_IMPLEMENTED_YET
|
762
747
|
|
763
748
|
assert_equal(expect, result)
|
764
749
|
end
|
765
750
|
|
766
|
-
#
|
751
|
+
# intersect -- :: (Eq a) => [a] -> [a] -> [a]
|
767
752
|
def test_intersect
|
768
|
-
result = @a
|
769
|
-
expect =
|
753
|
+
result = ~intersect(@a)
|
754
|
+
expect = NOT_IMPLEMENTED_YET
|
770
755
|
|
771
756
|
assert_equal(expect, result)
|
772
757
|
end
|
773
758
|
|
774
759
|
|
775
760
|
# -- ** Ordered lists
|
776
|
-
# , sort -- :: (Ord a) => [a] -> [a]
|
777
761
|
|
778
|
-
#
|
779
|
-
def
|
780
|
-
result = @a
|
781
|
-
expect =
|
762
|
+
# sort -- :: (Ord a) => [a] -> [a]
|
763
|
+
def test_sort
|
764
|
+
result = ~sort(@a)
|
765
|
+
expect = NOT_IMPLEMENTED_YET
|
782
766
|
|
783
767
|
assert_equal(expect, result)
|
784
768
|
end
|
785
769
|
|
770
|
+
# insert -- :: (Ord a) => a -> [a] -> [a]
|
771
|
+
def test_insert
|
772
|
+
result = ~insert(3.5, @a)
|
773
|
+
expect = NOT_IMPLEMENTED_YET
|
774
|
+
assert_equal(expect, result)
|
775
|
+
end
|
776
|
+
|
786
777
|
end # TestList
|