Skip to content

Instantly share code, notes, and snippets.

@epitron
Last active August 29, 2015 14:08
Show Gist options
  • Save epitron/86141fb2b5d41b65c345 to your computer and use it in GitHub Desktop.
Save epitron/86141fb2b5d41b65c345 to your computer and use it in GitHub Desktop.
Monkeypatching base JavaScript types (Array, Number) using CoffeeScript!
Number::log = (base)-> Math.log(this)/Math.log(base)
Number::pow = (exp)-> Math.pow(this, exp)
Number::ceil = -> Math.ceil(this)
Number::floor = -> Math.floor(this)
Array::first = -> this[0]
Array::last = -> this[@length-1]
Array::min = -> @sort().first()
Array::max = -> @sort().last()
Array::minmax = -> sorted = @sort(); [sorted.first(), sorted.last()]
Number::bits = ->
highest_bit = this.log(2).ceil()
for bit in [0..highest_bit]
if this & 2.pow(bit)
1
else
0
console.log 42.bits()
console.log 255.bits()
console.log 256.bits()
console.log [9,1,2,3,4,5,7].minmax()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment