Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local swaa = {
- case = function(sw,case,func)
- sw.cases[case] = func; return sw
- end,
- case_list = function(sw,func,...)
- for _,v in next,{...} do sw:case(v,func) end
- return sw
- end,
- case_range = function(sw,caseA,caseB,func)
- sw.range_cases[#sw.range_cases+1] = { caseA,caseB,func }
- return sw
- end,
- close = function(sw)
- if tonumber(sw.v) then
- for _,v in next,sw.range_cases do
- if sw.v>=v[1] and sw.v<=v[2] then return v[3](sw.v) end
- end
- end
- return (sw.cases[sw.v or "default"] or sw.cases["default"])(sw.v)
- end
- }
- swaa.__index = swaa
- local function swaa_new(v)
- local cases = { default = function(v) end }
- local range_cases = {}
- return setmetatable({v=v,cases=cases,range_cases=range_cases,close=nil},swaa)
- end
- local switch = swaa_new
- print(
- switch(0)
- :case_range(0,7,function(v) print("between 0-7") return -1 end)
- :case_list(function(v) print("between 11-12") return v-8 end,11,12)
- :case(8,function(v) return 0 end)
- :case(9,function(v) return 1 end)
- :case(10,function(v) return 2 end)
- :close()
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement