Last active
August 29, 2015 14:08
-
-
Save epitron/86141fb2b5d41b65c345 to your computer and use it in GitHub Desktop.
Monkeypatching base JavaScript types (Array, Number) using CoffeeScript!
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
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