> The wins for shell, IMO, come from:
> * the lack of any actual variables anywhere
> * 'sort | uniq -c', which encapsulates what would be the second largest component of the processing if you did this in a proper language
> * 'factor', although it needs to be filtered by a regex to throw out the non-primes
What you are describing there is a mixture of functional and template/meta-programming aspects to shell.
The problem with this code is that it is impossible to read without knowing its purpose. This may have seemed trivial to you but it is beyond cryptic to anyone who does not know what it does:
> seq 1 100 |
> xargs -L1 factor |
I guess my xargs is weak (I seldom use it) had to look up -L1
> egrep '^([0-9]+): \1$' |
Dependent upon the output of factor which also must be looked up (I don't use factor).
Once you know what factor outputs you have to think a bit to understand how that relates to egrep.
> grep -o '[0-9]$' |
Something about the last character... but what is -o?
> sort |
> uniq -c | sort -n -r
c,n,r aren't terrible arguments but not everyone will know them.
I think this ruby is easier to follow except for the difficulty of knowing what group_by does: