Created
December 20, 2017 16:27
-
-
Save tsukkee/0d253f77faa066894384a800d7eb2f90 to your computer and use it in GitHub Desktop.
Generator?
This file contains 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! Generator() | |
function! Inner1(value) closure | |
let i = a:value | |
function! Inner2(value) closure | |
let i += a:value | |
function! Inner3(value) closure | |
let i += a:value | |
function! Inner4(value) closure | |
let i += a:value | |
return {'value': i, 'done': v:true} | |
endfunction | |
return {'value': i, 'done': v:false} | |
endfunction | |
return {'value': i, 'done': v:false} | |
endfunction | |
return {'value': i, 'done': v:false} | |
endfunction | |
let counter = 0 | |
function! Call(value) closure | |
let counter += 1 | |
return Inner{counter}(a:value) | |
endfunction | |
return {'next': {value -> Call(value)}} | |
endfunction | |
let g = Generator() | |
echo g.next(1) | |
" -> {'done': v:false, 'next': 1} | |
echo g.next(2) | |
" -> {'done': v:false, 'next': 3} | |
echo g.next(3) | |
" -> {'done': v:false, 'next': 6} | |
echo g.next(4) | |
" -> {'done': v:true, 'next': 10} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment