Skip to content

Instantly share code, notes, and snippets.

@tsukkee
Created December 20, 2017 16:27
Show Gist options
  • Save tsukkee/0d253f77faa066894384a800d7eb2f90 to your computer and use it in GitHub Desktop.
Save tsukkee/0d253f77faa066894384a800d7eb2f90 to your computer and use it in GitHub Desktop.
Generator?
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