Learning to use ranges instead of arrays

Bob Tolbert via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 20 09:02:04 PDT 2014


I find myself writing this code too much and i'm curious what D
idiom I am missing.

given a list of files (or any strings) and then maybe I want to
sort them and maybe I don't.

      string [] fileList;

      ... fill list

      if (sort) {
          string [] tempList;
          foreach(filename; sort(fileList)) {
              tempList ~= filename;
          }
          fileList = tempList;
      }

      foreach(filename, fileList) {
          ... do something;
      }

but I know this is ugly code, so I'm curious as to how to make it 
better. I suspect it is something very simple, about switching 
everything I do to 'ranges', but I can't see it yet.

Thanks for any input,
Bob


More information about the Digitalmars-d-learn mailing list