www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Proposal: property 'fetch' for AA

reply eao197 <eao197 intervale.ru> writes:
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 =3D { :a =3D> 1 }
=3D> {:a=3D>1}
irb(main):002:0> a =3D h.fetch(:a, 0)
=3D> 1
irb(main):003:0> b =3D h.fetch(:b, 10)
=3D> 10

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

int[char] h;
h[ 'a' ] =3D 1;
auto a =3D h.fetch( 'a', 0 );
auto b =3D 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' ] =3D 1;
   auto a =3D h.fetch( 'a', 0 );
   auto b =3D h.fetch( 'b', 10 );
}

I think it is better to have 'fetch' implementation in the language or t=
he  =

standard library.

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

-- =

Regards,
Yauheni Akhotnikau
May 03 2007
parent reply Oskar Linde <oskar.lindeREM OVEgmail.com> writes:
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
parent Bill Baxter <dnewsgroup billbaxter.com> writes:
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
May 04 2007