cons 1.1.1 → 1.1.2
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 +25 -11
- 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: 03bf0fb5c5ddae8eb049455c8d4a66673563cdb9
|
4
|
+
data.tar.gz: 628c0f196d8fe9a0c819d7741969ff457c8f3d1d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 751c698c4b92cca312aa11abad0739ff10fdf20dc9a992fa86a35d4b7f70c04002a57044c19e32afa7cccd7ae6474833f1ac468a19e61cdd51efe45d59b0968e
|
7
|
+
data.tar.gz: 5ef764711df875cff7a415bc3c9a39045fdc40bcb4b47c0c41aada4841f3cd8110cc4cf18dc78ca5c5062dd019a6622bd56a2c918b0e09d36bd07ed666962c3c
|
data/lib/cons.rb
CHANGED
@@ -76,9 +76,9 @@ class Cons
|
|
76
76
|
alias rplacd cdr=
|
77
77
|
|
78
78
|
def nthcdr n
|
79
|
-
if not n.kind_of? Integer
|
80
|
-
raise ArgumentError, "n for nthcdr must be
|
81
|
-
elsif n
|
79
|
+
if not n.kind_of? Integer
|
80
|
+
raise ArgumentError, "n for nthcdr must be an integer"
|
81
|
+
elsif n <= 0
|
82
82
|
self
|
83
83
|
elsif n > 0
|
84
84
|
if cdr.nil?
|
@@ -210,15 +210,30 @@ class Cons
|
|
210
210
|
|
211
211
|
alias tree_copy copy_tree
|
212
212
|
|
213
|
-
# The
|
214
|
-
def
|
213
|
+
# The the_last method returns the last cdr that is a cons.
|
214
|
+
def the_last
|
215
215
|
if @cdr.kind_of? Cons
|
216
|
-
return @cdr.
|
216
|
+
return @cdr.last
|
217
217
|
else
|
218
218
|
return self
|
219
219
|
end
|
220
220
|
end
|
221
221
|
|
222
|
+
# last returns the last n conses (not the last n elements) of list). If n is
|
223
|
+
# zero, the atom that terminates list is returned. If n is greater than or
|
224
|
+
# equal to the number of cons cells in list, the result is list.
|
225
|
+
#
|
226
|
+
# Cf. <http://clhs.lisp.se/Body/f_last.htm>
|
227
|
+
def last n=nil
|
228
|
+
if n.nil? or n == 1
|
229
|
+
return the_last
|
230
|
+
elsif n <= 0
|
231
|
+
return Cons[]
|
232
|
+
else
|
233
|
+
return nthcdr length-n
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
222
237
|
# The append method returns a new list that is the concatenation of the
|
223
238
|
# copies. lists are left unchanged; the list structure of each of lists except
|
224
239
|
# the last is copied. The last argument is not copied; it becomes the cdr of
|
@@ -230,13 +245,13 @@ class Cons
|
|
230
245
|
result = self.copy_tree
|
231
246
|
if rest.empty? or rest.nil?
|
232
247
|
if list.kind_of? Cons
|
233
|
-
result.
|
234
|
-
else
|
235
|
-
result.
|
248
|
+
result.the_last.cdr = list.copy_tree
|
249
|
+
else # list is not a list
|
250
|
+
result.the_last.cdr = list
|
236
251
|
end
|
237
252
|
return result
|
238
253
|
else
|
239
|
-
result.
|
254
|
+
result.the_last.cdr = list.copy_tree
|
240
255
|
return result.append *rest
|
241
256
|
end
|
242
257
|
end
|
@@ -247,7 +262,6 @@ class Cons
|
|
247
262
|
# TODO - copy-alist - http://clhs.lisp.se/Body/f_cp_ali.htm
|
248
263
|
# TODO - copy-list - http://clhs.lisp.se/Body/f_cp_lis.htm
|
249
264
|
# TODO - endp - http://clhs.lisp.se/Body/f_endp.htm
|
250
|
-
# TODO - last - http://clhs.lisp.se/Body/f_last.htm
|
251
265
|
# TODO - ldiff, tailp - http://clhs.lisp.se/Body/f_ldiffc.htm
|
252
266
|
# TODO - list* - http://clhs.lisp.se/Body/f_list_.htm
|
253
267
|
# TODO - list-length - http://clhs.lisp.se/Body/f_list_l.htm
|