cons 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cons.rb +32 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d152ce3a7027460c8a1cc73acc6d7e3aa1d4eb66
|
4
|
+
data.tar.gz: 759e54ec5cd5d6b7ec4fb4400671c2ae0937e335
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e328284155901e0e6140bc40b5a43a0a233347b31ba1701dbd6d95c104c77e2452d58dd962e45f85e268247fe5acd31cafb744839fb47d8ecdc17e00d3f488c0
|
7
|
+
data.tar.gz: e268af7785ccc16db535a1dc92bce3575abdda46e175e53ac97f1a0f9b839570091678ab8376d2a2f5d4ecdd42cffc48050b1093347a1aed0ddb64e019953081
|
data/lib/cons.rb
CHANGED
@@ -210,8 +210,39 @@ class Cons
|
|
210
210
|
|
211
211
|
alias tree_copy copy_tree
|
212
212
|
|
213
|
+
# The last_cons method returns the last cdr that is a cons.
|
214
|
+
def last_cons
|
215
|
+
if @cdr.kind_of? Cons
|
216
|
+
return @cdr.last_cons
|
217
|
+
else
|
218
|
+
return self
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
# The append method returns a new list that is the concatenation of the
|
223
|
+
# copies. lists are left unchanged; the list structure of each of lists except
|
224
|
+
# the last is copied. The last argument is not copied; it becomes the cdr of
|
225
|
+
# the final dotted pair of the concatenation of the preceding lists, or is
|
226
|
+
# returned directly if there are no preceding non-empty lists.
|
227
|
+
#
|
228
|
+
# Cf. <http://clhs.lisp.se/Body/f_append.htm>
|
229
|
+
def append list, *rest
|
230
|
+
result = self.copy_tree
|
231
|
+
if rest.empty? or rest.nil?
|
232
|
+
if list.kind_of? Cons
|
233
|
+
result.last_cons.cdr = list.copy_tree
|
234
|
+
else
|
235
|
+
result.last_cons.cdr = list
|
236
|
+
end
|
237
|
+
return result
|
238
|
+
else
|
239
|
+
result.last_cons.cdr = list.copy_tree
|
240
|
+
return result.append *rest
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
213
244
|
## Lots of TODOs from the CLHS
|
214
|
-
|
245
|
+
|
215
246
|
# TODO - (n)butlast - http://clhs.lisp.se/Body/f_butlas.htm
|
216
247
|
# TODO - copy-alist - http://clhs.lisp.se/Body/f_cp_ali.htm
|
217
248
|
# TODO - copy-list - http://clhs.lisp.se/Body/f_cp_lis.htm
|