Thread overview | |||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 03, 2004 Example code | ||||
---|---|---|---|---|
| ||||
Is there a read source of example code in D? Something a little more extensive that those that come with the compiler without being horribly complex. Just some worked examples of using the various new (relative to C) language features. My current block is with hashes (associative arrays). I see from the wc sample that you can iterate a hash by 1) retrieving an array of all the keys in the hash 2) iterating that array using a numeric index 3) using the result (key) from that to index into the hash and retrieve the corresponding value. That works fine, but it requires the duplication of the storage required for the keys, which is expensive for large hashes. I was looking for an iterator something like perl5's "each" keyword? I such a thing possible, planned, hiding or just out of the question? Regards, anon. |
March 03, 2004 Re: Example code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anon. | Anon. wrote: > Is there a read source of example code in D? Something a little more extensive > that those that come with the compiler without being horribly complex. Just some > worked examples of using the various new (relative to C) language features. > > My current block is with hashes (associative arrays). > > I see from the wc sample that you can iterate a hash by > > 1) retrieving an array of all the keys in the hash > 2) iterating that array using a numeric index > 3) using the result (key) from that to index into the hash and retrieve the > corresponding value. > > That works fine, but it requires the duplication of the storage required for the > keys, which is expensive for large hashes. > > I was looking for an iterator something like perl5's "each" keyword? I such a > thing possible, planned, hiding or just out of the question? > > > > Regards, anon. Please... pick at http://www.digitalmars.com/d/arrays.html and learn about Associative Arrays. (not require duplication)... read de wc2.d example :-) About D examples code, if you know about C, only read the language specifications there are a lot of examples (not full programs) but it's very easy to write your owns. More: pick at D links there are many sites with examples and tutorials regards Julio Jiménez |
March 03, 2004 Re: Example code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Julio Jiménez | In article <c258cm$1qu7$1@digitaldaemon.com>, =?ISO-8859-1?Q?Julio_Jim=E9nez?= says... > >Anon. wrote: >> >> I see from the wc sample that you can iterate a hash by >> >> 1) retrieving an array of all the keys in the hash >> 2) iterating that array using a numeric index >> 3) using the result (key) from that to index into the hash and retrieve the >> corresponding value. that's old. (from memory) char[][char[]] hash; foreach ( char[] value ; hash.values ) { printf("value is %.*s\n", value); } check http://www.digitalmars.com/d/statement.html#foreach it's a pity associative arrays are not supported with the other foreach version: foreach ( int|uint, type ; array ) it could be: foreach ( char[] key, char[] value ; hash ) // not valid Ant |
March 03, 2004 Re: Example code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ant | Ant wrote: > In article <c258cm$1qu7$1@digitaldaemon.com>, =?ISO-8859-1?Q?Julio_Jim=E9nez?= > says... > >>Anon. wrote: >> >>>I see from the wc sample that you can iterate a hash by >>> >>>1) retrieving an array of all the keys in the hash >>>2) iterating that array using a numeric index >>>3) using the result (key) from that to index into the hash and retrieve the >>>corresponding value. > > > that's old. > > (from memory) > char[][char[]] hash; > foreach ( char[] value ; hash.values ) > { > printf("value is %.*s\n", value); > } > > check http://www.digitalmars.com/d/statement.html#foreach > > it's a pity associative arrays are not supported with the other > foreach version: foreach ( int|uint, type ; array ) > it could be: > foreach ( char[] key, char[] value ; hash ) // not valid > > Ant > I foreach over associative arrays all the time. There's even an example http://www.digitalmars.com/d/statement.html#foreach : double[char[]] a; // index type is char[], value type is double ... foreach (char[] s, double d; a) { printf("a['%.*s'] = %g\n", s, d); } -- Christopher E. Miller |
March 03, 2004 Re: Example code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | In article <c25b2g$1vu0$1@digitaldaemon.com>, Vathix says... > > >I foreach over associative arrays all the time. There's even an example > http://www.digitalmars.com/d/statement.html#foreach : > > double[char[]] a; // index type is char[], value type is double > ... > foreach (char[] s, double d; a) > { > printf("a['%.*s'] = %g\n", s, d); > } It is on the manuals! I just check the manuals before posting... :( need to check my eyes thanks. Ant |
March 03, 2004 Re: Example code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Julio Jiménez | The wc2.d code: (from dmd examples) import std.c.stdio; import std.file; int main (char[][] args) { int w_total; int l_total; int c_total; int[char[]] dictionary; printf(" lines words bytes file\n"); foreach (char[] arg; args[1 .. args.length]) { char[] input; int w_cnt, l_cnt, c_cnt; int inword; int wstart; input = cast(char[])std.file.read(arg); for (int j = 0; j < input.length; j++) { char c; c = input[j]; if (c == '\n') ++l_cnt; if (c >= '0' && c <= '9') { } else if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') { if (!inword) { wstart = j; inword = 1; ++w_cnt; } } else if (inword) { char[] word = input[wstart .. j]; dictionary[word]++; inword = 0; } ++c_cnt; } if (inword) { char[] w = input[wstart .. input.length]; dictionary[w]++; } printf("%8lu%8lu%8lu %.*s\n", l_cnt, w_cnt, c_cnt, arg); l_total += l_cnt; w_total += w_cnt; c_total += c_cnt; } if (args.length > 2) { printf("--------------------------------------\n%8lu%8lu%8lu total", l_total, w_total, c_total); } printf("--------------------------------------\n"); foreach (char[] word1; dictionary.keys.sort) { printf("%3d %.*s\n", dictionary[word1], word1); } return 0; } |
March 04, 2004 Re: Example code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vathix | In article <c25b2g$1vu0$1@digitaldaemon.com>, Vathix says... > >Ant wrote: >> In article <c258cm$1qu7$1@digitaldaemon.com>, =?ISO-8859-1?Q?Julio_Jim=E9nez?= says... >> >>>Anon. wrote: >>> >>>>I see from the wc sample that you can iterate a hash by >>>> >>>>1) retrieving an array of all the keys in the hash >>>>2) iterating that array using a numeric index >>>>3) using the result (key) from that to index into the hash and retrieve the >>>>corresponding value. >> >> >> that's old. >> >> (from memory) >> char[][char[]] hash; >> foreach ( char[] value ; hash.values ) >> { >> printf("value is %.*s\n", value); >> } >> >> check http://www.digitalmars.com/d/statement.html#foreach >> >> it's a pity associative arrays are not supported with the other >> foreach version: foreach ( int|uint, type ; array ) >> it could be: >> foreach ( char[] key, char[] value ; hash ) // not valid >> >> Ant >> > >I foreach over associative arrays all the time. There's even an example > http://www.digitalmars.com/d/statement.html#foreach : > > double[char[]] a; // index type is char[], value type is double > ... > foreach (char[] s, double d; a) > { > printf("a['%.*s'] = %g\n", s, d); > } > Great! It seemed strange that a language that is so well thought through in almost every respect should require the contortions shown in the word count example at the end of the Arrays section of the manual. char[][] keys = dictionary.keys;// find all words in dictionary[] for (int i = 0; i < keys.length; i++) { char[] word; word = keys[i]; printf("%3d %.*s\n", dictionary[word], word); } I (now) see that the wc2.d in the samples/d directory uses foreach. Maybe this will act as a reminder for someone to update that page of the manual. FWIW. I still that that an iterator would make sense. Sometimes its isn't convenient to process all the keys of a hash in one loop. A pair of methods: aarray.first aarray.next maybe? Thanks all. Anon. > >-- >Christopher E. Miller |
March 04, 2004 Re: Example code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Julio Jiménez | In article <c258cm$1qu7$1@digitaldaemon.com>, =?ISO-8859-1?Q?Julio_Jim=E9nez?= says... > >Anon. wrote: >> Is there a read source of example code in D? Something a little more extensive that those that come with the compiler without being horribly complex. Just some worked examples of using the various new (relative to C) language features. >> >> My current block is with hashes (associative arrays). >> >> I see from the wc sample that you can iterate a hash by >> >> 1) retrieving an array of all the keys in the hash >> 2) iterating that array using a numeric index >> 3) using the result (key) from that to index into the hash and retrieve the >> corresponding value. >> >> That works fine, but it requires the duplication of the storage required for the keys, which is expensive for large hashes. >> >> I was looking for an iterator something like perl5's "each" keyword? I such a thing possible, planned, hiding or just out of the question? >> >> >> >> Regards, anon. > >Please... pick at http://www.digitalmars.com/d/arrays.html It was "Associative Array Example: word count" at the end of that very page that gave me my mistaken impression :) >and learn about Associative Arrays. (not require duplication)... read de wc2.d example :-) > >About D examples code, if you know about C, only read the language specifications there are a lot of examples (not full programs) but it's very easy to write your owns. > I've always found it quicker to learn a language by reading through code written by other people with a reasonable amount of expertise than to learn it by piecing together my own code by leaping all over the manula to pick up he pieces. The latter technique tends to lead to mistaken impressions and bad habits. > >More: pick at D links there are many sites with examples and tutorials > If you could point me at a few of tehse many sites, that would be really useful. > >regards > >Julio Jiménez > regards, Anion |
March 04, 2004 Re: Example code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anon. | Anon. wrote: ... > > > If you could point me at a few of tehse many sites, that would be really useful. > > >>regards >> >>Julio Jiménez >> > > > regards, Anion > > http://dui.sourceforge.net/ you can learn about classes and interfacing to C libraries http://int19h.tamb.ru/ some projects and examples http://jcc_7.tripod.com/d/tutor/ Simple D tutorial http://www.dprogramming.com/ Little tutorial and some projects http://www.codemoon.com/cm/dpage.php Programs in D About the wc.d example, remember it's only an example; not the better way to do that.... wc2.d is a little better, but an example too. D give you the freedom to do what you need in your own way. Look at dmd/src/phobos/internal/aaA.d (Implementation of associative array) you can study it and implement the functions you need ;-) No need to wait another one write it for you! Regards, Julio |
June 11, 2004 Re: Example code | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anon. | In article <c246jl$301q$1@digitaldaemon.com>, Anon. says... > >Is there a read source of example code in D? Something a little more extensive that those that come with the compiler without being horribly complex. Just some worked examples of using the various new (relative to C) language features. I would like to see a horribly complex problem solved with the full treatment in C++ and D side by side. When I say nontrivial, I mean, I've seen the text-processing stuff and it's some nice syntax, but I could use perl for that. I need to see something big that would require design patterns in C++. D seems to automatically solve some major problems, like header dependencies that would require specific techniques to handle when writing C++. Good job. but there are a lot of unexpected interactions among language features that have been found and the C++ standard has at least tried to address. I'm not giving up on C++ yet. If D can do better than the examples given in "Modern C++ Design", I'm listening. |
Copyright © 1999-2021 by the D Language Foundation