Partial copy of dicts useful?
Posted: Fri Aug 07, 2009 10:50 am
by Carl Ranson >> Sun, 16 Sep 2001 23:30:09 GMT
Hi all,
Just wanted to moot this idea and see if anyone though it had merit.
I've seen quite a few blocks of code that do an iteration on a dictionary based on *some* of its keys.
For instance you might have orders by Company, Date & OrderID and want to get all orders for a given company and date.
iter := dict.createIterator;
targetCompany := "Jade";
targetDate := "10/10/01".Date;
dict.startKeyGeq(targetCompany, targetDate, null, iter);
while iter.next(order) do
if order.myCompany.name <> targetCompany or order.date <> targetDate then
break;
endif;
// do stuff here.
endwhile;
The messy part is, of course, that you need to re-check those keys you're interested in each time through the loop.
This got me thinking about alternative approaches that would allow one to do a "Partial iteraton" on a dictionary...something along the lines of
foreach order in dict.whereKeys(targetCompany, targetDate) do
// do stuff.
endforeach;
The idea being that whereKeys takes a list of keys and matches to any entry that matches. If a key is not provided it matches all values. So whereKeys(targetCompany) is like saying
getAtKey(targetCompany, *any*, *any*)
perhaps this could also be a copy operation that works in a similar manner such as this:
dict.copyWhere(dict2, targetCompany) would copy all orders for that company.
I'd be keen to hear what people think of this one.
Cheers,
Carl Ranson
Unify Limited
Hi all,
Just wanted to moot this idea and see if anyone though it had merit.
I've seen quite a few blocks of code that do an iteration on a dictionary based on *some* of its keys.
For instance you might have orders by Company, Date & OrderID and want to get all orders for a given company and date.
iter := dict.createIterator;
targetCompany := "Jade";
targetDate := "10/10/01".Date;
dict.startKeyGeq(targetCompany, targetDate, null, iter);
while iter.next(order) do
if order.myCompany.name <> targetCompany or order.date <> targetDate then
break;
endif;
// do stuff here.
endwhile;
The messy part is, of course, that you need to re-check those keys you're interested in each time through the loop.
This got me thinking about alternative approaches that would allow one to do a "Partial iteraton" on a dictionary...something along the lines of
foreach order in dict.whereKeys(targetCompany, targetDate) do
// do stuff.
endforeach;
The idea being that whereKeys takes a list of keys and matches to any entry that matches. If a key is not provided it matches all values. So whereKeys(targetCompany) is like saying
getAtKey(targetCompany, *any*, *any*)
perhaps this could also be a copy operation that works in a similar manner such as this:
dict.copyWhere(dict2, targetCompany) would copy all orders for that company.
I'd be keen to hear what people think of this one.
Cheers,
Carl Ranson
Unify Limited