Port of Python's difflib.SequenceMatcher class

Oskar Linde oskar.lindeREM at OVEgmail.com
Thu Dec 7 04:21:35 PST 2006


Bill Baxter wrote:
> Michael Butscher wrote:
> 
>> - One thing I really missed in D was the get() method for Python 
>> dictionaries with a default argument. Therefore I created inner 
>> functions like
>>
>>         IndexType j2lenget(IndexType i, IndexType def)
>>         {
>>             IndexType* result = i in j2len;
>>             if (result)
>>                 return *result;
>>             else
>>                 return def;
>>         }
>>
>> Probably this can be done more elegantly, but I personally think that
>> get() should be a standard method of AAs.
> 
> +1.  Me too.
> 
> If IFTI were smarter, something like this would do the trick:
> 
> V get(V,K)(V[K] dict, K key, V def = V.init)
> {
>     V* ptr = key in dict;
>     return ptr? *ptr: def;
> }

And what compiler do you use? The above code works perfectly. :)

The following two get functions have been part of my own standard 
imports for quite a while and I find them very handy.

T get(T,U)(T[U] aa, U key) {
         T* ptr = key in aa;
         return ptr ? *ptr : T.init;
}

bool get(T,U,int dummy=1)(T[U] aa, U key, out T val) {
         T* ptr = key in aa;
         if (!ptr)
                 return false;
         val = *ptr;
         return true;
}

/Oskar



More information about the Digitalmars-d-announce mailing list