Skip to content

Instantly share code, notes, and snippets.

@etale
Last active March 5, 2018 02:31
Show Gist options
  • Save etale/9f9218f85a96d4017f45d538530dfe3a to your computer and use it in GitHub Desktop.
Save etale/9f9218f85a96d4017f45d538530dfe3a to your computer and use it in GitHub Desktop.
Sometimes I want to split an integer.
(({ defineProperty }, { prototype }) => (
defineProperty(prototype, 'divmod', {
value(a) {
return (
a === 0 ? [0, this.valueOf()] :
((r) => (
((q) => (
[q, r]
))((this - r) / a)
))(this % a)
)
}
}),
defineProperty(prototype, 'split', {
value(a) {
return (
this < a ? [ this.valueOf() ] :
(([q, r]) => (
q.split(a).concat(r)
))(this.divmod(a))
)
}
})
))(Reflect, Number)
@etale
Copy link
Author

etale commented Mar 1, 2018

Reflect.defineProperty(String.prototype, 'inv', { get() { return this.split('').reverse().join('') } })
String(2**52).inv.match(/(.{1,4})/g).map(a => a.inv).reverse()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment