cons 1.0.1 → 1.1.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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cons.rb +88 -21
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d83965d38be86303de5909d238c26a01b3b56b20
4
- data.tar.gz: 6b36c974feeacafedcbc28bebb8a619b69461585
3
+ metadata.gz: 3af72cf8eb7e47ce6719d57dfbc24d454ca16d57
4
+ data.tar.gz: c533cff0121f20fb4b3e813e1eca7325543157ed
5
5
  SHA512:
6
- metadata.gz: f4bfe703f2453a943042864b4bb50a9d288d47e19f5125dbb66ce36735e7e8e3a89e9248f57d9234610444f7c08b22c38094a9e90fcef2baef1691057eb1233b
7
- data.tar.gz: a8a168c469442db0d8a86b12132207f0b5e5cbf2bb880ad98274ecf83bbd6f4be00e11aafd9c6fbffeb9ab25f00b6c1e71253ecc8c1459619b18b1abb674dfc8
6
+ metadata.gz: 62799c1404609bb90d35bdec58e5d9eaa9c642f8711d928503b32c8440d0acb7e7116ad66dd95593d5b976a10dc1642b56934fdaf076aa00bffd140051ee0905
7
+ data.tar.gz: e309acf9d74f46169764c6b934ea21cabc9685a76c6421a50eaa018d629268ab78f5804fbcd03d5485313af2f131d2cfdcdcfe763259063f9484577b7d423784
data/lib/cons.rb CHANGED
@@ -58,17 +58,29 @@ class Cons
58
58
  rhs.kind_of? Cons and car == rhs.car and cdr == rhs.cdr
59
59
  end
60
60
 
61
+ def length
62
+ result = 0
63
+ current = self
64
+ while current and current.car and not current.car.nil?
65
+ result += 1
66
+ current = current.cdr
67
+ end
68
+ result
69
+ end
70
+
61
71
  alias first car
62
72
  alias first= car=
73
+ alias rplaca car=
63
74
  alias rest cdr
64
75
  alias rest= cdr=
76
+ alias rplacd cdr=
65
77
 
66
78
  def nthcdr n
67
79
  if not n.kind_of? Integer or n < 0
68
80
  raise ArgumentError, "n for nthcdr must be a non-negative integer"
69
- elsif n == 1
81
+ elsif n == 0
70
82
  self
71
- elsif n > 1
83
+ elsif n > 0
72
84
  if cdr.nil?
73
85
  nil
74
86
  else
@@ -86,8 +98,11 @@ class Cons
86
98
  end
87
99
  end
88
100
 
101
+ def [] n
102
+ nth n
103
+ end
104
+
89
105
  def nth_eq(n, value)
90
- puts "nth= n=#{n} value=#{value}"
91
106
  i = nthcdr(n)
92
107
  if i.nil?
93
108
  raise RuntimeError
@@ -96,6 +111,10 @@ class Cons
96
111
  end
97
112
  end
98
113
 
114
+ def []= n, value
115
+ nth_eq n, value
116
+ end
117
+
99
118
  def first
100
119
  car
101
120
  end
@@ -104,15 +123,15 @@ class Cons
104
123
  @car = value
105
124
  end
106
125
 
107
- [[:second, 2],
108
- [:third, 3],
109
- [:fourth, 4],
110
- [:fifth, 5],
111
- [:sixth, 6],
112
- [:seventh, 7],
113
- [:eighth, 8],
114
- [:ninth, 9],
115
- [:tenth, 10]].each do |fname, i|
126
+ [[:second, 1],
127
+ [:third, 2],
128
+ [:fourth, 3],
129
+ [:fifth, 4],
130
+ [:sixth, 5],
131
+ [:seventh, 6],
132
+ [:eighth, 7],
133
+ [:ninth, 8],
134
+ [:tenth, 9]].each do |fname, i|
116
135
  class_eval %{
117
136
  def #{fname}
118
137
  nth #{i}
@@ -175,18 +194,66 @@ class Cons
175
194
  end
176
195
  end
177
196
 
197
+ def copy_tree
198
+ new_car = if car.kind_of? Cons
199
+ car.copy_tree
200
+ else
201
+ car
202
+ end
203
+ new_cdr = if cdr.kind_of? Cons
204
+ cdr.copy_tree
205
+ else
206
+ cdr
207
+ end
208
+ Cons[new_car,new_cdr]
209
+ end
210
+
211
+ alias tree_copy copy_tree
212
+
213
+ ## Lots of TODOs from the CLHS
214
+ # TODO - append - http://clhs.lisp.se/Body/f_append.htm
215
+ # TODO - (n)butlast - http://clhs.lisp.se/Body/f_butlas.htm
216
+ # TODO - copy-alist - http://clhs.lisp.se/Body/f_cp_ali.htm
217
+ # TODO - copy-list - http://clhs.lisp.se/Body/f_cp_lis.htm
218
+ # TODO - endp - http://clhs.lisp.se/Body/f_endp.htm
219
+ # TODO - last - http://clhs.lisp.se/Body/f_last.htm
220
+ # TODO - ldiff, tailp - http://clhs.lisp.se/Body/f_ldiffc.htm
221
+ # TODO - list* - http://clhs.lisp.se/Body/f_list_.htm
222
+ # TODO - list-length - http://clhs.lisp.se/Body/f_list_l.htm
223
+ # TODO - member, member-if, member-if-not - http://clhs.lisp.se/Body/f_mem_m.htm
224
+ # TODO - nconc - http://clhs.lisp.se/Body/f_nconc.htm
225
+ # TODO - revappend, nreconc - http://clhs.lisp.se/Body/f_revapp.htm
226
+ # TODO - pop - http://clhs.lisp.se/Body/m_pop.htm
227
+ # TODO - push - http://clhs.lisp.se/Body/m_push.htm
228
+ # TODO - pushnew - http://clhs.lisp.se/Body/m_pshnew.htm
229
+ # TODO - (n)subst, (n)subst-if, (n)subst-if-not - http://clhs.lisp.se/Body/f_substc.htm
230
+ # TODO - (n)sublis - http://clhs.lisp.se/Body/f_sublis.htm
231
+
232
+ ## Alist stuff
233
+ # TODO - acons - http://clhs.lisp.se/Body/f_acons.htm
234
+ # TODO - assoc, assoc-if, assoc-if-not - http://clhs.lisp.se/Body/f_assocc.htm
235
+ # TODO - pairlis - http://clhs.lisp.se/Body/f_pairli.htm
236
+ # TODO - rassoc, rassoc-if, rassoc-if-not - http://clhs.lisp.se/Body/f_rassoc.htm
237
+
238
+ ## Lists as sets stuff
239
+ # TODO - adjoin - http://clhs.lisp.se/Body/f_adjoin.htm
240
+ # TODO - (n)intersection - http://clhs.lisp.se/Body/f_isec_.htm
241
+ # TODO - (n)set-difference - http://clhs.lisp.se/Body/f_set_di.htm
242
+ # TODO - (n)set-exclusive-or - http://clhs.lisp.se/Body/f_set_ex.htm
243
+ # TODO - (n)union - http://clhs.lisp.se/Body/f_unionc.htm
244
+ # TODO - subsetp - http://clhs.lisp.se/Body/f_subset.htm
245
+
178
246
  class << self
179
- def from_array array, initial=true
247
+ def from_array array
180
248
  car, *cdr = array
181
- if car.nil?
182
- if initial
183
- self[nil, nil]
184
- else
185
- nil
186
- end
187
- else
188
- self[car, from_array(cdr, false)]
249
+ result = current = Cons.new
250
+ while car and not car.nil?
251
+ current.car = car
252
+ current.cdr = Cons.new if cdr and not cdr.empty?
253
+ current = current.cdr
254
+ car, *cdr = cdr
189
255
  end
256
+ result
190
257
  end
191
258
 
192
259
  def [] car=nil, cdr=nil
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cons
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Mark Gore
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-18 00:00:00.000000000 Z
11
+ date: 2016-09-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -53,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
53
  version: '0'
54
54
  requirements: []
55
55
  rubyforge_project:
56
- rubygems_version: 2.4.5.1
56
+ rubygems_version: 2.5.1
57
57
  signing_key:
58
58
  specification_version: 4
59
59
  summary: Lisp-style cons cells for Ruby.