Simplest way to create an array from an associative array which its contains keys and values?

2014-01-03 Thread 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 looks like this: [&

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-03 Thread Andrej Mitrovic
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

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-03 Thread bearophile
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

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-03 Thread Andrej Mitrovic
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. :)

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-03 Thread Justin Whear
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

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-03 Thread Ali Çehreli
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

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-03 Thread bearophile
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

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-03 Thread Ali Çehreli
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

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-03 Thread H. S. Teoh
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

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-03 Thread bearophile
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

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-05 Thread 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

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-05 Thread Philippe Sigaud
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

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-05 Thread Andrej Mitrovic
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

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-05 Thread Timon Gehr
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.

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-05 Thread Philippe Sigaud
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. > > >

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-07 Thread Craig Dillabaugh
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

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-07 Thread H. S. Teoh
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

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-07 Thread Craig Dillabaugh
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

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-07 Thread Andrej Mitrovic
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; >

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-07 Thread Jakob Ovrum
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

Re: Simplest way to create an array from an associative array which its contains keys and values?

2014-01-09 Thread bearophile
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