cons 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cons.rb +25 -11
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d152ce3a7027460c8a1cc73acc6d7e3aa1d4eb66
4
- data.tar.gz: 759e54ec5cd5d6b7ec4fb4400671c2ae0937e335
3
+ metadata.gz: 03bf0fb5c5ddae8eb049455c8d4a66673563cdb9
4
+ data.tar.gz: 628c0f196d8fe9a0c819d7741969ff457c8f3d1d
5
5
  SHA512:
6
- metadata.gz: e328284155901e0e6140bc40b5a43a0a233347b31ba1701dbd6d95c104c77e2452d58dd962e45f85e268247fe5acd31cafb744839fb47d8ecdc17e00d3f488c0
7
- data.tar.gz: e268af7785ccc16db535a1dc92bce3575abdda46e175e53ac97f1a0f9b839570091678ab8376d2a2f5d4ecdd42cffc48050b1093347a1aed0ddb64e019953081
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 or n < 0
80
- raise ArgumentError, "n for nthcdr must be a non-negative integer"
81
- elsif n == 0
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 last_cons method returns the last cdr that is a cons.
214
- def last_cons
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.last_cons
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.last_cons.cdr = list.copy_tree
234
- else
235
- result.last_cons.cdr = list
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.last_cons.cdr = list.copy_tree
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cons
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Mark Gore