Simplest way to create an array from an associative array which
its contains keys and values?
For example if i have an associative array like this:
["one":"1", "two":"2"]
What's the easiest way to create a dynamic array that looks like
this:
[&
On Friday, 3 January 2014 at 17:38:16 UTC, Gary Willoughby wrote:
I know it can be done via a loop, but is there a more idiomatic
way to achieve this?
import std.algorithm;
import std.range;
import std.stdio;
void main()
{
auto aa = ["one":"1", "two":"2"];
auto range = aa.byKey.ma
Gary Willoughby:
Simplest way to create an array from an associative array which
its contains keys and values?
For example if i have an associative array like this:
["one":"1", "two":"2"]
What's the easiest way to create a dynamic array that lo
On Friday, 3 January 2014 at 17:47:43 UTC, bearophile wrote:
string[] r = aa.byKey.map!(k => [k, aa[k]]).join;
However the [k, aa[k]] expression will allocate an array for each
key you iterate over regardless if you use join or joiner. I
posted a solution with "only", hope that works. :)
On Fri, 03 Jan 2014 17:49:28 +, Andrej Mitrovic wrote:
> On Friday, 3 January 2014 at 17:38:16 UTC, Gary Willoughby wrote:
>> I know it can be done via a loop, but is there a more idiomatic way to
>> achieve this?
>
>
> import std.algorithm;
> import std.range;
> import std.stdio;
>
> v
On 01/03/2014 09:47 AM, bearophile wrote:
> Unfortunately the D associative arrays specs don't specify this to be
> correct:
>
> zip(aa.byKey, aa.byValue)
I still like that solution. :) Even if it's not spelled out to be
correct in the spec, I can't imagine a hash table implementation where
by
Ali Çehreli:
I still like that solution. :) Even if it's not spelled out to
be correct in the spec, I can't imagine a hash table
implementation where byKey and byValue don't iterate in lock
step.
But in the long term ignoring what's present or missing in the
specs is a bad idea.
I wrote
On 01/03/2014 04:39 PM, bearophile wrote:
Ali Çehreli:
I still like that solution. :) Even if it's not spelled out to be
correct in the spec, I can't imagine a hash table implementation where
byKey and byValue don't iterate in lock step.
But in the long term ignoring what's present or missing
On Sat, Jan 04, 2014 at 12:39:32AM +, bearophile wrote:
> Ali Çehreli:
>
> >I still like that solution. :) Even if it's not spelled out to be
> >correct in the spec, I can't imagine a hash table implementation
> >where byKey and byValue don't iterate in lock step.
>
> But in the long term ign
H. S. Teoh:
I did an implementation of aa.byPair once, that iterates over
keys and
values simultaneously, but it was ultimately rejected:
A byPair will be needed in Phobos, in one way or another.
Bye,
bearophile
Andrej Mitrovic:
However the [k, aa[k]] expression will allocate an array for
each key you iterate over regardless if you use join or joiner.
I posted a solution with "only", hope that works. :)
Perhaps the "s" suffix (to define fixed-sized arrays) could avoid
that problem:
string[] r = aa
On Sat, Jan 4, 2014 at 1:08 AM, Ali Çehreli wrote:
> 3) The member rangeFront is needed because Tuple does not have opIndex for
> dynamic indexes. I can do range.front[0] but I cannot do
> range.front[currentIndex].
Is there any plan to add indexing on runtime indices to Tuple? It can
be done, b
On 1/5/14, bearophile wrote:
> Perhaps the "s" suffix (to define fixed-sized arrays) could avoid
> that problem:
>
> string[] r = aa.byKey.map!(k => [k, aa[k]]s).join;
I would prefer a prefix though, to make it immediately obvious you're
creating a static array.
string[] r = aa.byKey.map!(k => s
On 01/05/2014 03:55 PM, Philippe Sigaud wrote:
Is there any plan to add indexing on runtime indices to Tuple? It can
be done, by generating a specific runtime opIndex for Tuple, if the
types held in the tuple have a common type.
It would override the current index operator.
On Sun, Jan 5, 2014 at 4:49 PM, Timon Gehr wrote:
> On 01/05/2014 03:55 PM, Philippe Sigaud wrote:
>>
>> Is there any plan to add indexing on runtime indices to Tuple? It can
>> be done, by generating a specific runtime opIndex for Tuple, if the
>> types held in the tuple have a common type.
>
>
>
On Friday, 3 January 2014 at 17:38:16 UTC, Gary Willoughby wrote:
Simplest way to create an array from an associative array which
its contains keys and values?
For example if i have an associative array like this:
["one":"1", "two":"2"]
What's t
On Tue, Jan 07, 2014 at 08:38:10PM +, Craig Dillabaugh wrote:
[...]
> As someone with little experience with functional programming, I am
> just curious - having browsed through the thread - if the various
> solutions proposed here would really be considered more 'idiomatic'
> D. Or if they wer
On Tuesday, 7 January 2014 at 20:52:40 UTC, H. S. Teoh wrote:
On Tue, Jan 07, 2014 at 08:38:10PM +, Craig Dillabaugh
wrote:
[...]
As someone with little experience with functional programming,
I am
just curious - having browsed through the thread - if the
various
solutions proposed here wo
On 1/7/14, Craig Dillabaugh wrote:
> In other words while:
>
> auto range = aa.byKey.map!(a => chain(a.only, aa[a].only));
> string[] array = range.join;
>
> Saves a few lines of code, and looks cooler, it seems that the
> trivial foreach loop version is very easy:
>
> string[] array;
>
On Tuesday, 7 January 2014 at 20:38:11 UTC, Craig Dillabaugh
wrote:
Saves a few lines of code, and looks cooler, it seems that the
trivial foreach loop version is very easy:
string[] array;
foreach (key, value; aa) {
array ~= key;
array ~= value;
}
Assuming the return value o
string[] r = aa.byKey.map!(k => [k, aa[k]]s).join;
Do you remember the bug report number and/or pull request number
for that enhancement?
Bye,
bearophile
21 matches
Mail list logo