withIterator => ( factory: function, a: any ) : object
const sum = function* allThatCameBefore() {
let i = 0
let n = 0
while (i < this.length) {
yield (n += this[i])
i++
}
}
const accumulated = withIterator(sum, [1, 2, 3, 4, 5])
Array.from(accumulated)
withIterator => ( factory: function ) : function => ( a: any ) : object
const hex = ['123456', 'cc9933', 'fedcba']
const rgb = withIterator(function*() {
const re = /../g
let next
while ((next = re.exec(this)))
yield Number.parseInt(next, 16)
})
hex.map(rgb).map(code =>
`hex: ${code} - rgb: (${[...code]})`
)
using prototypes
class Digits extends Number {}
withIterator(
function*(){
for(let d of getIterator(String).call(this))
yield Number(d)
},
Digits.prototype
)
const digits = new Digits(54321)
console.log(digits)
Array.from(digits)
explicitly resolving boxed values
const nul = withIterator(null)
const und = withIterator(undefined)
const num = withIterator(42)
const m = new Map([
[nul, valueOf(nul)],
[und, valueOf(und)],
[num, valueOf(num)]
])