Another task

2011-01-19 Thread bearophile
Now and then I like to test Phobos with simple tasks, to see how it's going. This simple task is to create a dynamic array of pairs (tuples) like: [(10,"aa"), (30,"bb"), (50,"cc")] from the associative array: [1:'a', 2:'b', 3:'c'] If possible read things lazily from the associative array. -

Re: Another task

2011-01-19 Thread Simen kjaeraas
bearophile wrote: Now and then I like to test Phobos with simple tasks, to see how it's going. This simple task is to create a dynamic array of pairs (tuples) like: [(10,"aa"), (30,"bb"), (50,"cc")] from the associative array: [1:'a', 2:'b', 3:'c'] If possible read things lazily from the a

Re: Another task

2011-01-19 Thread bearophile
Simen kjaeraas: > Why use map()? The correct solution for this looks like so: > > > import std.range; > > void main( ) { > auto aa = [1:"a", 2:"b", 3:"c"]; > auto result = zip( aa.keys, aa.values ); > } That result is not the requested one: [(10,"aa"), (30,"bb"), (50,"cc")] And that

Re: Another task

2011-01-19 Thread bearophile
> That result is not the requested one: > [(10,"aa"), (30,"bb"), (50,"cc")] Sorry, the last tuple is (30,"cc"). Bye, bearophile

Re: Another task

2011-01-19 Thread Simen kjaeraas
On Thu, 20 Jan 2011 02:40:29 +0100, bearophile wrote: Simen kjaeraas: Why use map()? The correct solution for this looks like so: import std.range; void main( ) { auto aa = [1:"a", 2:"b", 3:"c"]; auto result = zip( aa.keys, aa.values ); } That result is not the requested one:

Re: Another task

2011-01-19 Thread Simen kjaeraas
On Thu, 20 Jan 2011 02:44:39 +0100, bearophile wrote: That result is not the requested one: [(10,"aa"), (30,"bb"), (50,"cc")] Sorry, the last tuple is (30,"cc"). And the second is (20,"bb"), I hope. :p -- Simen

Re: Another task

2011-01-19 Thread bearophile
Simen kjaeraas: > Soz, I read a bit too fast. It /is/ lazy, though perhaps not the way > you meant. This returns the right thing, but does not *read* lazily > from the AA, a task I am unsure how, if at all possible, one should > perform. In the task was written: > If possible read things lazily

Re: Another task

2011-01-19 Thread Simen kjaeraas
bearophile wrote: D AAs have byKey and byValue that return a lazy iterator. So if we add a "byItem" or "byPair" or "byKeyValue" you are able to read pairs lazily :-) byKey is essentially an opApply. You have to wrap it in a fiber to make it work with the range interface: import std.algor

Re: Another task

2011-01-20 Thread bearophile
Simen kjaeraas: > byKey is essentially an opApply. You have to wrap it in a fiber to make it > work with the range interface: Thank you for all your code and work. I have found a bug (it's not a bug of yours): if I compile your code with -release, DMD prints: test.d(45): Error: function D main i

Re: Another task

2011-01-20 Thread spir
On 01/20/2011 11:12 AM, bearophile wrote: > And I suggest to add a third associative array member function that returns a range of key,value tuples, as in Python3. This allows to solve the task like this: > auto r = map!q{ tuple(a[0]*10, a[1]~a[1]) }(aa.byPair()); Yes, this is the only nice lo

Re: Another task

2011-01-20 Thread bearophile
spir: > Yes, this is the only nice looking, high-level, and D-style solution. I have added: http://d.puremagic.com/issues/show_bug.cgi?id=5466 > While I by far prefer avoiding stringcode: > auto r = map!((p) (tuple(p[0]*10, p[1]~p[1])) (aa.byPair()); > where p means pair; should be corre

Re: Another task

2011-01-20 Thread Simen kjaeraas
bearophile wrote: I think for a newcomer the most difficult part is related to tuples: * find them (in std.typecons!!!) * catch after much time, pains, research, they should not even try to construct a tuple using Tuple!, but using the convenience tuple() func instead. I agree that like dynam