Created
January 17, 2022 19:28
-
-
Save ochaton/09ea9dc83cb23740be3a81e570d1a8a5 to your computer and use it in GitHub Desktop.
Adds fun.yield to tarantool
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
| local fun = require 'fun' local iter = fun.iter local wrap = fun.wrap local sleep = require 'fiber'.sleep local function yield_gen_x(i, state_x, ...) if state_x == nil then return nil end return {i, state_x}, ... end local function yield_gen(param, state) local n, no, gen_x, param_x = param[1], param[2], param[3], param[4] local i, state_x = state[1], state[2] if i % n == 0 then sleep(no) end return yield_gen_x(i+1, gen_x(param_x, state_x)) end local function yield1(n, no, gen, param, state) assert(n >= 0, "invalid first argument to yield") assert(no >= 0, "invalid second argument to yield") return wrap(yield_gen, { n, no, gen, param }, { 0, state }) end local function method_yield(self, n, no) return yield1(n, no or 0, self.gen, self.param, self.state) end getmetatable(iter{}).__index.yield = method_yield getmetatable(iter{}).__index.sleep = method_yield local function export_yield(n, no, gen, param, state) return yield1(n, no or 0, iter(gen, param, state)) end fun.yield = export_yield fun.sleep = export_yield |
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
| local fun = require 'fun' | |
| local iter = fun.iter | |
| local wrap = fun.wrap | |
| local sleep = require 'fiber'.sleep | |
| local function yield_gen_x(i, state_x, ...) | |
| if state_x == nil then | |
| return nil | |
| end | |
| return {i, state_x}, ... | |
| end | |
| local function yield_gen(param, state) | |
| local n, no, gen_x, param_x = param[1], param[2], param[3], param[4] | |
| local i, state_x = state[1], state[2] | |
| if i % n == 0 then | |
| sleep(no) | |
| end | |
| return yield_gen_x(i+1, gen_x(param_x, state_x)) | |
| end | |
| local function yield1(n, no, gen, param, state) | |
| assert(n >= 0, "invalid first argument to yield") | |
| assert(no >= 0, "invalid second argument to yield") | |
| return wrap(yield_gen, { n, no, gen, param }, { 0, state }) | |
| end | |
| local function method_yield(self, n, no) | |
| return yield1(n, no or 0, self.gen, self.param, self.state) | |
| end | |
| getmetatable(iter{}).__index.yield = method_yield | |
| getmetatable(iter{}).__index.sleep = method_yield | |
| local function export_yield(n, no, gen, param, state) | |
| return yield1(n, no or 0, iter(gen, param, state)) | |
| end | |
| fun.yield = export_yield | |
| fun.sleep = export_yield |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment