Created
July 26, 2010 15:10
-
-
Save eterps/490671 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DirTree < String | |
include Enumerable | |
def each | |
Dir.entries(self).reject{|n| %w[. ..].include? n}.each do |filename| | |
yield path = "#{self}/#{filename}" | |
DirTree.new(path).each{|n| yield n} if File.directory? path | |
end | |
end | |
end | |
class DiskUsage | |
def self.[](path) | |
DirTree.new(path).map{|n| File.lstat(n).size}.inject(0){|sum, size| sum + size} | |
end | |
end | |
p DiskUsage['.'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on: http://www.reddit.com/r/haskell/comments/cs54i/how_would_you_write_du_in_haskell/c0uu4ne