rus3 0.1.2 → 0.2.0

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.
@@ -1,30 +1,34 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rus3::Procedure
4
- module Char
3
+ module Rus3
4
+ module Procedure
5
5
 
6
- # - procedure (R5RS): (char->integer char)
6
+ module Char
7
7
 
8
- def char_to_integer(char)
9
- Rus3::Char.char_to_integer(char)
10
- end
8
+ # - procedure (R5RS): (char->integer char)
11
9
 
12
- # - procedure (R5RS): (integer->char n)
10
+ def char_to_integer(char)
11
+ Rus3::Char.char_to_integer(char)
12
+ end
13
13
 
14
- def integer_to_char(n)
15
- Rus3::Char.integer_to_char(n)
16
- end
14
+ # - procedure (R5RS): (integer->char n)
17
15
 
18
- # - library procedure (R5RS): (char-upcase char)
16
+ def integer_to_char(n)
17
+ Rus3::Char.integer_to_char(n)
18
+ end
19
19
 
20
- def char_upcase(char)
21
- Rus3::Char.upcase(char)
22
- end
20
+ # - library procedure (R5RS): (char-upcase char)
21
+
22
+ def char_upcase(char)
23
+ Rus3::Char.upcase(char)
24
+ end
25
+
26
+ # - library procedure (R5RS): (char-downcase char)
23
27
 
24
- # - library procedure (R5RS): (char-downcase char)
28
+ def char_downcase(char)
29
+ Rus3::Char.downcase(char)
30
+ end
25
31
 
26
- def char_downcase(char)
27
- Rus3::Char.downcase(char)
28
32
  end
29
33
 
30
34
  end
@@ -1,34 +1,38 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rus3::Procedure
4
- module Control
3
+ module Rus3
4
+ module Procedure
5
5
 
6
- include Predicate
7
- include Rus3::EmptyList
6
+ module Control
8
7
 
9
- def map(prc, *lists)
10
- case lists.size
11
- when 0
12
- EMPTY_LIST
13
- when 1
14
- raise Rus3::ListRequiredError, lists[0] unless list?(lists[0])
15
- lists[0].map(&prc)
16
- else
17
- zip(*lists).map {|args|
18
- prc.call(*args)
19
- }
8
+ include Predicate
9
+ include Rus3::EmptyList
10
+
11
+ def map(prc, *lists)
12
+ case lists.size
13
+ when 0
14
+ EMPTY_LIST
15
+ when 1
16
+ raise Rus3::ListRequiredError, lists[0] unless list?(lists[0])
17
+ lists[0].map(&prc)
18
+ else
19
+ zip(*lists).map {|args|
20
+ prc.call(*args)
21
+ }
22
+ end
20
23
  end
21
- end
22
24
 
23
- def zip(*lists)
24
- case lists.size
25
- when 0
26
- EMPTY_LIST
27
- when 1
28
- lists[0]
29
- else
30
- lists[0].zip(*lists[1..-1])
25
+ def zip(*lists)
26
+ case lists.size
27
+ when 0
28
+ EMPTY_LIST
29
+ when 1
30
+ lists[0]
31
+ else
32
+ lists[0].zip(*lists[1..-1])
33
+ end
31
34
  end
35
+
32
36
  end
33
37
 
34
38
  end
@@ -1,272 +1,275 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Rus3::Procedure
3
+ module Rus3
4
+ module Procedure
4
5
 
5
- # The module holds list operation proceduress of Scheme. Most of
6
- # those procedures are defined in R(n)RS (the specification of
7
- # Scheme language).
8
- #
9
- # - R5RS 6.3.2 Pairs and lists
10
- # - R7RS 6.4 Pairs and lists
11
-
12
- module List
13
-
14
- include Utils
15
- include Predicate
16
- include Rus3::EmptyList
17
-
18
- # Constructs a Pair object with arguments.
6
+ # The module holds list operation proceduress of Scheme. Most of
7
+ # those procedures are defined in R(n)RS (the specification of
8
+ # Scheme language).
19
9
  #
20
- # - procedure (R5RS): (cons obj1 obj2)
21
-
22
- def cons(obj1, obj2)
23
- case obj2
24
- when Rus3::Pair
25
- if null?(obj2.cdr) # (foo . ())
26
- [obj1, obj2.car]
10
+ # - R5RS 6.3.2 Pairs and lists
11
+ # - R7RS 6.4 Pairs and lists
12
+
13
+ module List
14
+
15
+ include Utils
16
+ include Predicate
17
+ include Rus3::EmptyList
18
+
19
+ # Constructs a Pair object with arguments.
20
+ #
21
+ # - procedure (R5RS): (cons obj1 obj2)
22
+
23
+ def cons(obj1, obj2)
24
+ case obj2
25
+ when Rus3::Pair
26
+ if null?(obj2.cdr) # (foo . ())
27
+ [obj1, obj2.car]
28
+ else
29
+ Rus3::Pair.new(obj1, obj2)
30
+ end
31
+ when Array
32
+ [obj1].concat(obj2)
27
33
  else
28
34
  Rus3::Pair.new(obj1, obj2)
29
35
  end
30
- when Array
31
- [obj1].concat(obj2)
32
- else
33
- Rus3::Pair.new(obj1, obj2)
34
36
  end
35
- end
36
37
 
37
- # Returns the CAR part of the argument. If the arguemnt is not
38
- # a pair, raises PairRequiredError.
39
- #
40
- # - procedure (R5RS): (car pair)
41
-
42
- def car(pair)
43
- case pair
44
- when Rus3::Pair
45
- pair.car
46
- when Array
47
- raise Rus3::PairOrListRequiredError, pair if pair.empty?
48
- pair[0]
49
- else
50
- raise Rus3::PairOrListRequiredError, pair
51
- end
52
- end
38
+ # Returns the CAR part of the argument. If the arguemnt is not
39
+ # a pair, raises PairRequiredError.
40
+ #
41
+ # - procedure (R5RS): (car pair)
53
42
 
54
- # Returns the CDR part of the argument. If the arguemnt is not
55
- # a pair, raises PairRequiredError.
56
- #
57
- # - procedure (R5RS): (cdr pair)
58
-
59
- def cdr(pair)
60
- case pair
61
- when Rus3::Pair
62
- pair.cdr
63
- when Array
64
- raise Rus3::PairOrListRequiredError, pair if pair.empty?
65
- pair[1..-1]
66
- else
67
- raise Rus3::PairOrListRequiredError, pair
43
+ def car(pair)
44
+ case pair
45
+ when Rus3::Pair
46
+ pair.car
47
+ when Array
48
+ raise Rus3::PairOrListRequiredError, pair if pair.empty?
49
+ pair[0]
50
+ else
51
+ raise Rus3::PairOrListRequiredError, pair
52
+ end
68
53
  end
69
- end
70
54
 
71
- # Replaces the CAR part with the 2nd argument and returns UNDEF.
72
- # If the 1st arguemnt is not a pair, raises PairRequiredError.
73
- #
74
- # - procedure (R5RS): (set-car! pair obj)
75
-
76
- def set_car!(pair, obj)
77
- case pair
78
- when Rus3::Pair
79
- pair.set_car!(obj)
80
- when Array
81
- pair[0] = obj
82
- else
83
- raise Rus3::PairOrListRequiredError, pair
55
+ # Returns the CDR part of the argument. If the arguemnt is not
56
+ # a pair, raises PairRequiredError.
57
+ #
58
+ # - procedure (R5RS): (cdr pair)
59
+
60
+ def cdr(pair)
61
+ case pair
62
+ when Rus3::Pair
63
+ pair.cdr
64
+ when Array
65
+ raise Rus3::PairOrListRequiredError, pair if pair.empty?
66
+ pair[1..-1]
67
+ else
68
+ raise Rus3::PairOrListRequiredError, pair
69
+ end
84
70
  end
85
- Rus3::UNDEF
86
- end
87
71
 
88
- # Replaces the CDR part with the 2nd argument and returns UNDEF.
89
- # If the 1st arguemnt is not a pair, raises PairRequiredError.
90
- #
91
- # - procedure (R5RS): (set-cdr! pair obj)
92
-
93
- def set_cdr!(pair, obj)
94
- case pair
95
- when Rus3::Pair
96
- pair.set_cdr!(obj)
97
- when Array
98
- case obj
72
+ # Replaces the CAR part with the 2nd argument and returns UNDEF.
73
+ # If the 1st arguemnt is not a pair, raises PairRequiredError.
74
+ #
75
+ # - procedure (R5RS): (set-car! pair obj)
76
+
77
+ def set_car!(pair, obj)
78
+ case pair
79
+ when Rus3::Pair
80
+ pair.set_car!(obj)
99
81
  when Array
100
- pair.slice!(1, pair.size - 1)
101
- pair.concat(obj)
82
+ pair[0] = obj
102
83
  else
103
- # If `obj` was not a proper list of Scheme, the result of
104
- # `set_cdr!` would be a Pair instance. However, in this
105
- # case, the given `pair` is an Array instance, there is no
106
- # way to replace it with a new Pair instance.
107
- raise UnsupportedFeatureError.new("set_cdr!", obj)
84
+ raise Rus3::PairOrListRequiredError, pair
108
85
  end
109
- else
110
- raise Rus3::PairOrListRequiredError, pair
86
+ Rus3::UNDEF
111
87
  end
112
- Rus3::UNDEF
113
- end
114
88
 
115
- # Retrieves the CAR part of the CAR part of the given pair.
116
- #
117
- # - library procedure (R5RS): (caar pair)
118
- # - procedure (R7RS): (caar pair)
89
+ # Replaces the CDR part with the 2nd argument and returns UNDEF.
90
+ # If the 1st arguemnt is not a pair, raises PairRequiredError.
91
+ #
92
+ # - procedure (R5RS): (set-cdr! pair obj)
119
93
 
120
- def caar(pair)
121
- car(car(pair))
122
- end
94
+ def set_cdr!(pair, obj)
95
+ case pair
96
+ when Rus3::Pair
97
+ pair.set_cdr!(obj)
98
+ when Array
99
+ case obj
100
+ when Array
101
+ pair.slice!(1, pair.size - 1)
102
+ pair.concat(obj)
103
+ else
104
+ # If `obj` was not a proper list of Scheme, the result of
105
+ # `set_cdr!` would be a Pair instance. However, in this
106
+ # case, the given `pair` is an Array instance, there is no
107
+ # way to replace it with a new Pair instance.
108
+ raise UnsupportedFeatureError.new("set_cdr!", obj)
109
+ end
110
+ else
111
+ raise Rus3::PairOrListRequiredError, pair
112
+ end
113
+ Rus3::UNDEF
114
+ end
123
115
 
124
- # Retrieves the CAR part of the CDR part of the given pair.
125
- #
126
- # - library procedure (R5RS): (cadr pair)
127
- # - procedure (R7RS): (cadr pair)
116
+ # Retrieves the CAR part of the CAR part of the given pair.
117
+ #
118
+ # - library procedure (R5RS): (caar pair)
119
+ # - procedure (R7RS): (caar pair)
128
120
 
129
- def cadr(pair)
130
- car(cdr(pair))
131
- end
121
+ def caar(pair)
122
+ car(car(pair))
123
+ end
132
124
 
133
- # Retrieves the CDR part of the CAR part of the given pair.
134
- #
135
- # - library procedure (R5RS): (cdar pair)
136
- # - procedure (R7RS): (cdar pair)
125
+ # Retrieves the CAR part of the CDR part of the given pair.
126
+ #
127
+ # - library procedure (R5RS): (cadr pair)
128
+ # - procedure (R7RS): (cadr pair)
137
129
 
138
- def cdar(pair)
139
- cdr(car(pair))
140
- end
130
+ def cadr(pair)
131
+ car(cdr(pair))
132
+ end
141
133
 
142
- # Retrieves the CDR part of the CDR part of the given pair.
143
- #
144
- # - libbrary procedure (R5RS): (cddr pair)
145
- # - procedure (R7RS): (cddr pair)
134
+ # Retrieves the CDR part of the CAR part of the given pair.
135
+ #
136
+ # - library procedure (R5RS): (cdar pair)
137
+ # - procedure (R7RS): (cdar pair)
146
138
 
147
- def cddr(pair)
148
- cdr(cdr(pair))
149
- end
139
+ def cdar(pair)
140
+ cdr(car(pair))
141
+ end
150
142
 
151
- # :stopdoc:
143
+ # Retrieves the CDR part of the CDR part of the given pair.
144
+ #
145
+ # - libbrary procedure (R5RS): (cddr pair)
146
+ # - procedure (R7RS): (cddr pair)
152
147
 
153
- # - procedure (R7RS): (make-list k)
154
- # - procedure (R7RS): (make-list k fill)
148
+ def cddr(pair)
149
+ cdr(cdr(pair))
150
+ end
155
151
 
156
- # :startdoc:
152
+ # :stopdoc:
157
153
 
158
- # Constructs a list from arguments in its order.
159
- #
160
- # - library procedure (R5RS): (list obj ...)
154
+ # - procedure (R7RS): (make-list k)
155
+ # - procedure (R7RS): (make-list k fill)
161
156
 
162
- def list(*objs)
163
- Array[*objs]
164
- end
157
+ # :startdoc:
165
158
 
166
- # Returns the length of the arguemnt. If the argument is not a
167
- # proper list, raises ListRequiredError.
168
- #
169
- # - library procedure (R5RS): (length list)
170
- # - procedure (R7RS): (length list)
159
+ # Constructs a list from arguments in its order.
160
+ #
161
+ # - library procedure (R5RS): (list obj ...)
171
162
 
172
- def length(lst)
173
- check_list(lst)
174
- lst.size
175
- end
163
+ def list(*objs)
164
+ Array[*objs]
165
+ end
176
166
 
177
- # Concatenates given lists into a single list. Each argument
178
- # must be a proper list, otherwise raises ListRequiredError.
179
- #
180
- # - library procedure (R5RS): (append list ...)
181
- # - procedure (R7RS): (append list ...)
167
+ # Returns the length of the arguemnt. If the argument is not a
168
+ # proper list, raises ListRequiredError.
169
+ #
170
+ # - library procedure (R5RS): (length list)
171
+ # - procedure (R7RS): (length list)
182
172
 
183
- def append(*lists)
184
- lists.each { |lst|
173
+ def length(lst)
185
174
  check_list(lst)
186
- }
187
- [].concat(*lists)
188
- end
175
+ lst.size
176
+ end
189
177
 
190
- # Returns a list of the same elements in reverse order.
191
- #
192
- # - library procedure (R5RS): (reverse list)
193
- # - procedure (R7RS): (reverse list)
178
+ # Concatenates given lists into a single list. Each argument
179
+ # must be a proper list, otherwise raises ListRequiredError.
180
+ #
181
+ # - library procedure (R5RS): (append list ...)
182
+ # - procedure (R7RS): (append list ...)
183
+
184
+ def append(*lists)
185
+ lists.each { |lst|
186
+ check_list(lst)
187
+ }
188
+ [].concat(*lists)
189
+ end
194
190
 
195
- def reverse(lst)
196
- check_list(lst)
197
- lst.sort {|a, b| b <=> a}
198
- end
191
+ # Returns a list of the same elements in reverse order.
192
+ #
193
+ # - library procedure (R5RS): (reverse list)
194
+ # - procedure (R7RS): (reverse list)
199
195
 
200
- # Returns the sublist of the arguemnt by omitting the first k
201
- # elements. The 2nd argument, k must be in 0..length(lst),
202
- # otherwise raises ExceedUppeerLimitError.
203
- #
204
- # This implementation logic comes from R5RS 6.3.2.
205
- #
206
- # - library procedure (R5RS): (list-tail list k)
207
- # - procedure (R7RS): (list-tail list k)
196
+ def reverse(lst)
197
+ check_list(lst)
198
+ lst.sort {|a, b| b <=> a}
199
+ end
208
200
 
209
- def list_tail(lst, k)
210
- check_list(lst)
211
- check_upper_limit(k, length(lst)+1)
201
+ # Returns the sublist of the arguemnt by omitting the first k
202
+ # elements. The 2nd argument, k must be in 0..length(lst),
203
+ # otherwise raises ExceedUppeerLimitError.
204
+ #
205
+ # This implementation logic comes from R5RS 6.3.2.
206
+ #
207
+ # - library procedure (R5RS): (list-tail list k)
208
+ # - procedure (R7RS): (list-tail list k)
212
209
 
213
- lst.drop(k)
214
- end
210
+ def list_tail(lst, k)
211
+ check_list(lst)
212
+ check_upper_limit(k, length(lst)+1)
215
213
 
216
- # Returns kth element of the argument. k must be less than the
217
- # length of the list, otherwise, raises ExceedUppeerLimitError.
218
- #
219
- # - library procedure (R5RS): (list-ref list k)
220
- # - procedure (R7RS): (list-ref list k)
214
+ lst.drop(k)
215
+ end
221
216
 
222
- def list_ref(lst, k)
223
- check_list(lst)
224
- check_upper_limit(k, length(lst))
217
+ # Returns kth element of the argument. k must be less than the
218
+ # length of the list, otherwise, raises ExceedUppeerLimitError.
219
+ #
220
+ # - library procedure (R5RS): (list-ref list k)
221
+ # - procedure (R7RS): (list-ref list k)
225
222
 
226
- lst[k]
227
- end
223
+ def list_ref(lst, k)
224
+ check_list(lst)
225
+ check_upper_limit(k, length(lst))
228
226
 
229
- # :stopdoc:
227
+ lst[k]
228
+ end
229
+
230
+ # :stopdoc:
230
231
 
231
- # - procedure (R7RS): (list-set! list k obj)
232
+ # - procedure (R7RS): (list-set! list k obj)
232
233
 
233
- # :startdoc:
234
+ # :startdoc:
234
235
 
235
- # :stopdpc:
236
+ # :stopdpc:
236
237
 
237
- # - library procedure (R5RS): (memq obj list)
238
- # - procedure (R7RS): (memq obj list)
238
+ # - library procedure (R5RS): (memq obj list)
239
+ # - procedure (R7RS): (memq obj list)
239
240
 
240
- # - library procedure (R5RS): (memv obj list)
241
- # - procedure (R7RS): (memv obj list)
241
+ # - library procedure (R5RS): (memv obj list)
242
+ # - procedure (R7RS): (memv obj list)
242
243
 
243
- # - library procedure (R5RS): (member obj list)
244
- # - procedure (R7RS): (member obj list)
244
+ # - library procedure (R5RS): (member obj list)
245
+ # - procedure (R7RS): (member obj list)
245
246
 
246
- # - procedure (R7RS): (member obj list compare)
247
+ # - procedure (R7RS): (member obj list compare)
247
248
 
248
- # :startdoc:
249
+ # :startdoc:
249
250
 
250
- # :stopdpc:
251
+ # :stopdpc:
251
252
 
252
- # - library procedure (R5RS): (assq obj alist)
253
- # - procedure (R7RS): (assq obj alist)
253
+ # - library procedure (R5RS): (assq obj alist)
254
+ # - procedure (R7RS): (assq obj alist)
254
255
 
255
- # - library procedure (R5RS): (assv obj alist)
256
- # - procedure (R7RS): (assv obj alist)
256
+ # - library procedure (R5RS): (assv obj alist)
257
+ # - procedure (R7RS): (assv obj alist)
257
258
 
258
- # - library procedure (R5RS): (assoc obj alist)
259
- # - procedure (R7RS): (assoc obj alist)
259
+ # - library procedure (R5RS): (assoc obj alist)
260
+ # - procedure (R7RS): (assoc obj alist)
260
261
 
261
- # - procedure (R7RS): (assoc obj alist compare)
262
+ # - procedure (R7RS): (assoc obj alist compare)
262
263
 
263
- # :startdoc:
264
+ # :startdoc:
264
265
 
265
- # :stopdoc:
266
+ # :stopdoc:
266
267
 
267
- # - procedure (R7RS): (list-copy obj)
268
+ # - procedure (R7RS): (list-copy obj)
268
269
 
269
- # :startdoc:
270
+ # :startdoc:
271
+
272
+ end
270
273
 
271
274
  end
272
275
  end