cons 1.1.0 → 1.1.1

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cons.rb +32 -1
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3af72cf8eb7e47ce6719d57dfbc24d454ca16d57
4
- data.tar.gz: c533cff0121f20fb4b3e813e1eca7325543157ed
3
+ metadata.gz: d152ce3a7027460c8a1cc73acc6d7e3aa1d4eb66
4
+ data.tar.gz: 759e54ec5cd5d6b7ec4fb4400671c2ae0937e335
5
5
  SHA512:
6
- metadata.gz: 62799c1404609bb90d35bdec58e5d9eaa9c642f8711d928503b32c8440d0acb7e7116ad66dd95593d5b976a10dc1642b56934fdaf076aa00bffd140051ee0905
7
- data.tar.gz: e309acf9d74f46169764c6b934ea21cabc9685a76c6421a50eaa018d629268ab78f5804fbcd03d5485313af2f131d2cfdcdcfe763259063f9484577b7d423784
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
- # TODO - append - http://clhs.lisp.se/Body/f_append.htm
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
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.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Mark Gore