Last active
October 9, 2015 14:07
-
-
Save missinglink/4939c16ad1dba7ac3f75 to your computer and use it in GitHub Desktop.
emca6 infinitely cycle through values in Set using a generator and iterator
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
| "use strict"; | |
| // generator to infinitely loop over a set | |
| let infiniteCycleSet = function* ( set ){ | |
| let _seq = set.values() | |
| while( true ){ | |
| let _cur = _seq.next() | |
| if( _cur.done ){ | |
| _seq = set.values() | |
| _cur = _seq.next() | |
| } | |
| yield _cur.value | |
| } | |
| } | |
| // generator to infinitely loop over a set | |
| // starting from a specific value | |
| let infiniteCycleSetFrom = function* ( set, startAt ){ | |
| let _seq = set.values(), _target = startAt | |
| while( true ){ | |
| let _cur = _seq.next() | |
| if( _cur.done ){ | |
| _seq = set.values() | |
| _cur = _seq.next() | |
| } | |
| if( _cur.value === _target ){ | |
| let _next = _seq.next() | |
| if( _next.done ){ | |
| _seq = set.values() | |
| _next = _seq.next() | |
| } | |
| _target = _next.value | |
| yield _target | |
| } | |
| } | |
| } |
Author
missinglink
commented
Oct 9, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment