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.
-
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
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
> That result is not the requested one:
> [(10,"aa"), (30,"bb"), (50,"cc")]
Sorry, the last tuple is (30,"cc").
Bye,
bearophile
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:
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
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
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
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
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
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
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
12 matches
Mail list logo