by MatmaRex · 12y ago · 0 comments
Here's a comparison of code to do the same thing written in Ruby:
Dir['**/*.py'].map{|f| File.readlines(f).length }.inject(:+)
find . -name '*.py' | xargs cat | wc -l
That's 60 characters versus 39 (most of these are just because the "commands" are longer – and I don't think anyone would even try to argue that 'cat' or 'wc' is more readable than 'File.readlines' or 'length').I ran them on Python 2.7's library directory and my version counted four lines more – I didn't investigate, but I think yours missed lines when cating files without trailing newline. I'd argue that's a bug in your code :)
xs.filter(p).map(f)
with filter p xs | map f
(Suppose filter and map are implemented such that they accept input either on the command line or from pipeline.) map f $ filter p xs
what, don't like the reverse order? That's fixable.
If we define # as reverse function application, it's xs # filter p # map f
or filter p xs # map f
which is quite similar to what you are looking for.