Re: Reading a file of words line by line

2020-01-16 Thread mark via Digitalmars-d-learn
On Thursday, 16 January 2020 at 10:10:02 UTC, dwdv wrote: On 2020-01-16 04:54, Jesse Phillips via Digitalmars-d-learn wrote: [...] [...] isn't far off, but could also be (sans imports): return File(filename).byLine .map!(line => line.until!(not!isAlpha)) .filter!(word => word.count

Re: Reading a file of words line by line

2020-01-16 Thread dwdv via Digitalmars-d-learn
On 2020-01-16 04:54, Jesse Phillips via Digitalmars-d-learn wrote: [...] .map!(word => word.to!string.toUpper) .array .sort .uniq .map!(x => tuple (x, 0)) .assocArray ; .each!(word => words[word.to!string.toUpper] = 0); isn't far off, but could also be (sans imports): return

Re: Reading a file of words line by line

2020-01-15 Thread Jesse Phillips via Digitalmars-d-learn
On Wednesday, 15 January 2020 at 19:50:31 UTC, mark wrote: I really do need a set for the next part of the program, but taking your code and ideas I have now reduced the function to this: WordSet getWords(string filename, int wordsize) { WordSet words; File(filename).byLine

Re: Reading a file of words line by line

2020-01-15 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Jan 15, 2020 at 07:50:31PM +, mark via Digitalmars-d-learn wrote: [...] > Why did you use string.count rather than string.length? The .length of a `string` type is the number of bytes that it occupies, which is not necessarily the same thing as the number of characters in the string.

Re: Reading a file of words line by line

2020-01-15 Thread mark via Digitalmars-d-learn
I really do need a set for the next part of the program, but taking your code and ideas I have now reduced the function to this: WordSet getWords(string filename, int wordsize) { WordSet words; File(filename).byLine .map!(line => line.until!(not!isAlpha)) .filter!(word

Re: Reading a file of words line by line

2020-01-15 Thread dwdv via Digitalmars-d-learn
On 2020-01-15 16:34, mark via Digitalmars-d-learn wrote: Is this as compact as it _reasonably_ can be? How about this? auto uniqueWords(string filename, uint wordsize) { import std.algorithm, std.array, std.conv, std.functional, std.uni; return File(filename).byLine

Re: Reading a file of words line by line

2020-01-15 Thread mark via Digitalmars-d-learn
Thanks for the ideas, I've now reduced the size of the getWords() function (even allowing for moving the imports to the top of the file) to this: WordSet getWords(string filename, int wordsize) { string bareWord(string line) { auto rx = ctRegex!(r"^([a-z]+)", "i"); auto

Re: Reading a file of words line by line

2020-01-14 Thread mipri via Digitalmars-d-learn
On Tuesday, 14 January 2020 at 16:39:16 UTC, mark wrote: I can't help feeling that the foreach loop's block is rather more verbose than it could be? WordSet words; auto rx = ctRegex!(r"^[a-z]+", "i"); auto file = File(filename); foreach (line; file.byLine) { auto

Re: Reading a file of words line by line

2020-01-14 Thread mark via Digitalmars-d-learn
Should I have closed the file, i.e.,: auto file = File(filename); scope(exit) file.close(); // Add this?

Reading a file of words line by line

2020-01-14 Thread mark via Digitalmars-d-learn
As part of learning D I want to read a file that contains one word per line (plus optional junk after the word) and creates a set of all the unique words of a particular length (uppercased). D doesn't appear to have a set type so I'm faking using an associative array whose values are always