RosettaCodeData/Task/File-size-distribution/Sidef/file-size-distribution.sidef

30 lines
626 B
Plaintext
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

func traverse(Block callback, Dir dir) {
dir.open(\var dir_h) || return nil
 
for entry in (dir_h.entries) {
if (entry.kind_of(Dir)) {
traverse(callback, entry)
} else {
callback(entry)
}
}
}
 
var dir = (ARGV ? Dir(ARGV[0]) : Dir.cwd)
var group = Hash()
var files_num = 0
var total_size = 0
traverse({ |file|
group{file.size+1 -> log10.round} := 0 += 1
total_size += file.size
files_num += 1
}, dir)
for k,v in (group.sort_by { |k,_| Num(k) }) {
say "log10(size) ~~ #{k} -> #{v} files"
}
say "Total: #{total_size} bytes in #{files_num} files"