On Tuesday, 21 February 2017 at 22:43:15 UTC, H. S. Teoh wrote:

Parsing strings at program startup is ugly and slow. What about parsing at compile-time with CTFE into an array literal and transforming that into an AA at startup, which should be a lot faster?

        // Warning: untested code
        pure string[2][] parseTwoColumnCsv(string inputCsv)
        {
                import std.csv;
                import std.typecons;

                string[2][] result;
foreach (record; csvReader!(Tuple!(string,string))(inputCsv)) {
                        result ~= [record[0], record[1]];
                }
                return result;
        }

        immutable string[string] dataLookup;
        static this()
        {
enum halfCookedData = parseTwoColumnCsv(import("some_data.csv"));
                foreach (p; halfCookedData) {
                        dataLookup[p[0]] = p[1];
                }
        }


T

Hi,

That makes a lot of sense and it had crossed my mind.

In my case the data sets are super small, so I'm probably going to be lazy/productive and leave it the way it is.

Anyone else from the internet copying these examples: try to just do it H.S. Teoh's way from the start ;)

Thanks for the suggestion.
- Chad

Reply via email to