Created
February 16, 2018 18:29
-
-
Save pope/7e590437662da604323e90c1b8ae59a1 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/pelibij
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
function* subSequenceCycle(sequence, size) { | |
if (sequence.length === 0) { | |
throw new Error('cannot be empty'); | |
} | |
// OR | |
// asserts.assert(sequence.length !== 0); | |
const copy = sequence.slice(); | |
let cur = 0; | |
const res = []; | |
let rem = size; | |
while (true) { | |
const itemsLeft = copy.length - cur; | |
// Don't have enough to send back. Add what we have to `res` and | |
// start back over. | |
if (itemsLeft < rem) { | |
res.push(...copy.slice(cur)); | |
rem -= itemsLeft; | |
cur = 0; | |
continue; | |
} | |
const end = rem + cur | |
res.push(...copy.slice(cur, end)); | |
yield res.slice(); | |
// Reset for next | |
res.length = 0; | |
rem = size; | |
cur = end; | |
} | |
} | |
let gen = subSequenceCycle([1, 2, 3, 4, 5, 6], 4); | |
console.log(gen.next().value); | |
console.log(gen.next().value); | |
console.log(gen.next().value); | |
console.log(gen.next().value); | |
console.log(gen.next().value); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">function* subSequenceCycle(sequence, size) { | |
if (sequence.length === 0) { | |
throw new Error('cannot be empty'); | |
} | |
// OR | |
// asserts.assert(sequence.length !== 0); | |
const copy = sequence.slice(); | |
let cur = 0; | |
const res = []; | |
let rem = size; | |
while (true) { | |
const itemsLeft = copy.length - cur; | |
// Don't have enough to send back. Add what we have to `res` and | |
// start back over. | |
if (itemsLeft < rem) { | |
res.push(...copy.slice(cur)); | |
rem -= itemsLeft; | |
cur = 0; | |
continue; | |
} | |
const end = rem + cur | |
res.push(...copy.slice(cur, end)); | |
yield res.slice(); | |
// Reset for next | |
res.length = 0; | |
rem = size; | |
cur = end; | |
} | |
} | |
let gen = subSequenceCycle([1, 2, 3, 4, 5, 6], 4); | |
console.log(gen.next().value); | |
console.log(gen.next().value); | |
console.log(gen.next().value); | |
console.log(gen.next().value); | |
console.log(gen.next().value);</script></body> | |
</html> |
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
function* subSequenceCycle(sequence, size) { | |
if (sequence.length === 0) { | |
throw new Error('cannot be empty'); | |
} | |
// OR | |
// asserts.assert(sequence.length !== 0); | |
const copy = sequence.slice(); | |
let cur = 0; | |
const res = []; | |
let rem = size; | |
while (true) { | |
const itemsLeft = copy.length - cur; | |
// Don't have enough to send back. Add what we have to `res` and | |
// start back over. | |
if (itemsLeft < rem) { | |
res.push(...copy.slice(cur)); | |
rem -= itemsLeft; | |
cur = 0; | |
continue; | |
} | |
const end = rem + cur | |
res.push(...copy.slice(cur, end)); | |
yield res.slice(); | |
// Reset for next | |
res.length = 0; | |
rem = size; | |
cur = end; | |
} | |
} | |
let gen = subSequenceCycle([1, 2, 3, 4, 5, 6], 4); | |
console.log(gen.next().value); | |
console.log(gen.next().value); | |
console.log(gen.next().value); | |
console.log(gen.next().value); | |
console.log(gen.next().value); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment