3131 `==` (s, t) is bool
3232
3333 Collectable [T] = concept c, var w # # a collection of suitable items
34- T is Suitable
3534 contains (c, T) is bool
3635 len (c) is Ordinal
3736 for item in items (c): # ...that you can iterate
3837 item is T
38+ item is Suitable
3939 for index, item in pairs (c): # pairs iteration yields the index
4040 item is T
41+ item is Suitable
4142 del (w, index) # the index can be used for deletion
43+ T is Suitable
4244
43- #
44- # define some stuff for the Groupable concept
45- #
46- proc del * [T](group: var Collectable [T]; value: T) =
47- for index, item in pairs (group):
48- if item == value:
49- group.del index
50- break
51-
52- type
5345 Groupable [T] = concept g, var w # # a collection we can grow or shrink
46+ g is Collectable
5447 g is Collectable [T]
5548 add (w, T)
56- # del(w, T) # -- see tests below
49+ del (w, T)
5750
5851 Group [T] = concept g, var w # # add the concept of a unique index
5952 g is Groupable [T]
6053 incl (w, T) # do nothing if T's index exists
6154 excl (w, T) # do nothing if T's index does not exist
6255 for index, item in pairs (g): # pairs iteration yields the index
56+ item is T
57+ item is Suitable
6358 add (w, index, T) # it will raise if the index exists
6459 `[]` (w, index) is T # get via index
6560 `[]=` (w, index, T) # set via index
@@ -184,6 +179,12 @@ proc `[]`*[T](group: Group[T]; url: Uri): T =
184179proc `[]` * [T](group: Group [T]; name: PackageName ): T =
185180 result = group[newIdentity (name)]
186181
182+ proc del * [T](group: var Collectable [T]; value: T) =
183+ for index, item in pairs (group):
184+ if item == value:
185+ group.del index
186+ break
187+
187188when isMainModule :
188189 import testes
189190
@@ -201,12 +202,23 @@ when isMainModule:
201202 assert g is Collectable [string ]
202203 # # sure, fine, as expected.
203204 assert g is Collectable
205+ # # del() was written against Collectable
206+ g.del " pigs"
204207 # # ok, great. and this works, for now!
205208 assert g is Groupable [string ]
206209 # # this does not -- but why not?
207210 assert g is Groupable
208211 # # h is immutable, so isn't Groupable[string], right?
209212 assert h isnot Groupable [string ]
210- # # if you add `del(w, T)` to the Groupable,
211- # # then g is no longer a Groupable[string]!
212- assert g is Groupable [string ]
213+ # # so does that mean we can add to h?
214+ h.add " horses"
215+ # # but wait, i thought h was Collectable
216+ assert h is Collectable
217+ # # so we can iterate and delete, right?
218+ for index, item in pairs (h):
219+ h.del index
220+ break
221+ # # oh, but we can use our del(var w, T)?
222+ h.del " pigs"
223+ # # right, so, uh, how is h a Groupable[string]?
224+ assert h is Groupable [string ]
0 commit comments