4 ms·
Works nicely in Ruby 1.9: #!/usr/bin/ruby -Ku # encoding: utf-8 module Enumerable def sum inject { |acc, val| acc + val } end def Σ(&b
by judofyr 17y ago
Works nicely in Ruby 1.9:
#!/usr/bin/ruby -Ku
# encoding: utf-8
module Enumerable
def sum
inject { |acc, val| acc + val }
end
def Σ(&blk)
map(&blk).sum
end
def entropy(&prob)
- Σ { |i| prob[i] * Math.log(prob[i]) }
end
end
EDIT: And in 1.8 if you pass -Ku.
- jrockway 17y agoThis also works fine in Perl, Common Lisp, and Haskell... and probably every other programming language that was designed after 1970.