Hello World
Near the end of the last post, I noted we would put the static site generator project aside for the time being. I decided th...
For further actions, you may consider blocking this person and/or reporting abuse
Steve, why do you sort inside the loop on every iteration?
And here's my take on it. You can sort by predicate. It's not exactly very efficient though, since the extension is recalculated every time. But come on, Go could be really annoying sometimes. Look at this verbosity:
In Ruby that would be:
Nah, no need for the boilerplate to define a custom sort. This would do sorting the map:
Edit: sorting the ˋ[]stringˋ within the ˋmap[string][]stringˋ. The map itself can't be sorted.
I don't have a map in my version. I sort an array by a predicate.
Yes, I have seen it. Your approach is different by just sorting a list of filenames. The orginal intent is to sort files by extension into different buckets.
Your use of filepath.Ext() is quit clever. Haven't thought of that.
This would make the example even shorter:
@detunized @dirkolbrich
Thanks for the replies!
filepath.Ext()
! Didn't occur to me to try that. It goes to show that the standard library really is pretty complete.Dirk, I like the
for ext := range m { sort.Strings(m[ext]) }
solution then I wouldn't need to have a separate sort in each "print" function, it's much clearer that way.Hah. You caught me! The
sort
should have been moved up into the print function(s) at the very least. It's a bad design decision - I put it there at first just for the sake of simplicity and never got around to cleaning it up. It doesn't really matter in a directory of a few files but would really impact performance in a larger directory. Something like the following might be fine, and still pretty simple to follow.I think I may update the article to make sure it's called out for clarity.
You don't mind?
use it with:
Hey Steve, fantastic start.
You've come up with some great solutions in there, so I thought I'd share my own.
I restricted myself to fitting a subset of what you've solved thus far, that is to say, get all the files organised by category, and print them out as JSON. I've ignored plain / nested output, since that is somewhat trivial/not business logic.
Here's my solution:
As you can see, I've drastically cut down on the number of operations needed to get there, as well as corrected for a few problems you weren't looking out for yet. These are mainly:
You should skip dotfiles or hidden files, which start with a
.
character (at least by default), as these are frequently config files or important somehow.You're expending a lot of effort sorting / printing your data, when really all you need is a map to handle the listing
output from my program (against a sample directory):
usage:
sorter -dir ./sample | jq
If I wanted plain output, with a map I could do something like this:
which would output like so:
This is somewhat trite and gross but you get the point, dealing with one map makes this much easier to handle!
Looking forward to seeing what you come up with next!
I think the core of my issue is I'm also not leaning on the standard library as much as I should. I didn't realize
filepath.Ext()
was a thing. :/ Yeah, I read "sorting files by ext" as just that sorting alphabetically, had I left that out I would have been done quite a bit quicker. I suppose that made me go off the rails a bit so to speak. The different printing methods were not needed at all but what are you gonna do lol.