Thread overview
Proposal: property 'fetch' for AA
May 04, 2007
eao197
May 04, 2007
Oskar Linde
May 04, 2007
Bill Baxter
May 04, 2007
Ruby's Hash has a handy method 'fetch'[1] which allows to extract value from Hash or, if value is missed, use some default value:

irb(main):001:0> h = { :a => 1 }
=> {:a=>1}
irb(main):002:0> a = h.fetch(:a, 0)
=> 1
irb(main):003:0> b = h.fetch(:b, 10)
=> 10

If D's AA would have property 'fetch' it would allow to write:

int[char] h;
h[ 'a' ] = 1;
auto a = h.fetch( 'a', 0 );
auto b = h.fetch( 'b', 10 );

instead of:

V fetch(K,V)( V[K] h, K key, V default_value )
{
  if( key in h )
    return h[key];
  return default_value;
}

void
main()
{
  int[char] h;
  h[ 'a' ] = 1;
  auto a = h.fetch( 'a', 0 );
  auto b = h.fetch( 'b', 10 );
}

I think it is better to have 'fetch' implementation in the language or the standard library.

[1] http://www.ruby-doc.org/core/classes/Hash.html#M002877

-- 
Regards,
Yauheni Akhotnikau
May 04, 2007
eao197 skrev:
> Ruby's Hash has a handy method 'fetch'[1] which allows to extract value from Hash or, if value is missed, use some default value:

> If D's AA would have property 'fetch' it would allow to write:
> 
> int[char] h;
> h[ 'a' ] = 1;
> auto a = h.fetch( 'a', 0 );
> auto b = h.fetch( 'b', 10 );
> 
> instead of:
> 
> V fetch(K,V)( V[K] h, K key, V default_value )
> {
>   if( key in h )
>     return h[key];
>   return default_value;
> }

A slightly different version I've been using:

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

T get(T,U, int dummy = 1)(T[U] aa, U key, lazy T defaultValue) {
        T* ptr = key in aa;
        if (ptr)
        	return *ptr;
        return defaultValue;
}

and

T getCached(T,U)(T[U] aa, U key, lazy T computedValue) {
	T* ptr = key in aa;
	if (ptr)
		return *ptr;
	T val = computedValue;
	aa[key] = val;
	return val;
}

The latter one could probly use a better name, but the idea is to conveniently be able to use an AA as a cache for expensive computations.

/Oskar
May 04, 2007
Oskar Linde wrote:
> eao197 skrev:
>> Ruby's Hash has a handy method 'fetch'[1] which allows to extract value from Hash or, if value is missed, use some default value:
> 
>> If D's AA would have property 'fetch' it would allow to write:
>>
>> int[char] h;
>> h[ 'a' ] = 1;
>> auto a = h.fetch( 'a', 0 );
>> auto b = h.fetch( 'b', 10 );
>>
>> instead of:
>>
>> V fetch(K,V)( V[K] h, K key, V default_value )
>> {
>>   if( key in h )
>>     return h[key];
>>   return default_value;
>> }
> 
> A slightly different version I've been using:
> 
> T get(T,U)(T[U] aa, U key) {
>         T* ptr = key in aa;
>         if (ptr)
>                 return *ptr;
>         return T.init;
> }
> 
> T get(T,U, int dummy = 1)(T[U] aa, U key, lazy T defaultValue) {
>         T* ptr = key in aa;
>         if (ptr)
>             return *ptr;
>         return defaultValue;
> }
> 
> and
> 
> T getCached(T,U)(T[U] aa, U key, lazy T computedValue) {
>     T* ptr = key in aa;
>     if (ptr)
>         return *ptr;
>     T val = computedValue;
>     aa[key] = val;
>     return val;
> }
> 
> The latter one could probly use a better name, but the idea is to conveniently be able to use an AA as a cache for expensive computations.
> 
> /Oskar

In python those two are called 'get' and 'setdefault'.
Not that I think those are the best names in the world.

--bb