Default Dictionary
Posted: Sun Jul 29, 2012 8:59 pm
See if you like it...
https://github.com/jaegs/MultiList/blob/master/DefaultDict.cobra
https://github.com/jaegs/MultiList/blob/master/DefaultDict.cobra
Discussion about the Cobra programming language.
http://cobra-language.com/forums/
/# Should modify get(key as TKey, default as TValue) in ExtendDictionary.cobra
# to throw NotImplementedException() if the type is a DefaultDict
#/
A possibility is to have a "pro willRecordDefaults from var as bool".
def get(key as TKey, default as TValue) as TValue
#removeAll
test
bag = Bag<of char>('mississippi')
bag.removeAll(c'i')
assert not bag.contains(c'i')
# -->
test removeAll
bag = Bag<of char>('mississippi')
bag.removeAll(c'i')
assert not bag.contains(c'i')
jaegs wrote:I changed the class name and added more constructors. When I try to test the commented out code, the tester crashes. Perhaps you could take a look.
$ cobra -detailed-stack-trace DefaultDictionary.cobra
Cobra detected stack overflow:
Last 20 frames:
480. def DefaultDictionary<of,>.tryGetValue at line 50
481. def DefaultDictionary<of,>.[].get at line 41
482. def DefaultDictionary<of,>.tryGetValue at line 50
483. def DefaultDictionary<of,>.[].get at line 41
484. def DefaultDictionary<of,>.tryGetValue at line 50
485. def DefaultDictionary<of,>.[].get at line 41
486. def DefaultDictionary<of,>.tryGetValue at line 50
487. def DefaultDictionary<of,>.[].get at line 41
488. def DefaultDictionary<of,>.tryGetValue at line 50
489. def DefaultDictionary<of,>.[].get at line 41
490. def DefaultDictionary<of,>.tryGetValue at line 50
491. def DefaultDictionary<of,>.[].get at line 41
492. def DefaultDictionary<of,>.tryGetValue at line 50
493. def DefaultDictionary<of,>.[].get at line 41
494. def DefaultDictionary<of,>.tryGetValue at line 50
495. def DefaultDictionary<of,>.[].get at line 41
496. def DefaultDictionary<of,>.tryGetValue at line 50
497. def DefaultDictionary<of,>.[].get at line 41
498. def DefaultDictionary<of,>.tryGetValue at line 50
499. def DefaultDictionary<of,>.[].get at line 41
500. def DefaultDictionary<of,>.tryGetValue at line 48
Fail fast: Stack Overflow
Exiting with -1
pro [item as T] as int
get
return _data.get(item, 0)
set
require value > 0
_data[item] = this[item] + value
def removeAll(item as T) as bool
return _data.remove(item)