Page 1 of 1

lua_switch, a shorthand for creating switch statements in lua

Posted: Mon Jul 04, 2022 1:28 am
by jordan4ibanez
Someone in the discord was surprised that there was no internal method to create a switch statement in Lua.

So being half awake, I thought to myself: "I can fix that."

Then this thing was made. If it's useful to you, I'm glad. Have fun with it.

https://github.com/jordan4ibanez/lua_switch

usage example: (the tutorial from the repo)

Code: Select all

require("switch")

-- a basic switch statement definition
local test_switch = switch:new({
    test = function()
        print("test worked")
    end,

    [5] = function()
        print("math is cool")
    end
})

-- we utilize it
test_switch:match("test")
test_switch:match(5)

-- another one defined
local name_switch = switch:new({
    fred = function()
        print("wow fred is awesome!")
    end,
    jonny = function()
        print("yeah that's jonny")
    end
})

print("\n")

-- we utilize it like so
local name_repo = {
    "frank", "fred", "john", "jonny", "zoop"
}

for _,name in ipairs(name_repo) do
    name_switch:match(name)
end

print("\n")

-- we can also inline this whole thing
local my_functional_switch = switch:new({
    test = function()
        print("YEAH THIS WORKS BOI")
    end,
    [100] = function()
        print("that's a pretty big number")
    end,
    [false] = function()
        print("yeah that's wrong")
    end
}):match(false)
this will print out:

Code: Select all

test worked
math is cool


wow fred is awesome!
yeah that's jonny


yeah that's wrong

Re: lua_switch, a shorthand for creating switch statements in lua

Posted: Mon Jul 04, 2022 1:37 am
by pgimeno
Just because you can, doesn't mean you should ;)

Re: lua_switch, a shorthand for creating switch statements in lua

Posted: Mon Jul 04, 2022 1:40 am
by jordan4ibanez
pgimeno wrote: Mon Jul 04, 2022 1:37 am Just because you can, doesn't mean you should ;)
Instructions unclear, create more insanely mundane libraries for people to utilize confirmed

Re: lua_switch, a shorthand for creating switch statements in lua

Posted: Mon Jul 04, 2022 4:03 pm
by keharriso
Here's a really simple example with syntax that's more C-like.

Code: Select all

local function switch(value)
  return function (cases)
    cases[value]()
  end
end

local value = "hello"

switch (value) {
  hello = function ()
    print "Hello, world!"
  end,
  
  goodbye = function ()
    print "Goodbye, world!"
  end
}
Nice thing about Lua, there's like 10 different ways to do everything! :nyu:

Re: lua_switch, a shorthand for creating switch statements in lua

Posted: Tue Jul 05, 2022 3:26 am
by jordan4ibanez
keharriso wrote: Mon Jul 04, 2022 4:03 pm Here's a really simple example with syntax that's more C-like.

Code: Select all

local function switch(value)
  return function (cases)
    cases[value]()
  end
end

local value = "hello"

switch (value) {
  hello = function ()
    print "Hello, world!"
  end,
  
  goodbye = function ()
    print "Goodbye, world!"
  end
}
Nice thing about Lua, there's like 10 different ways to do everything! :nyu:
Yes, this is very true. I added on a simple logic routine to allow you to input data into the switch and receive it back out even when doing an inline switch, it's neat

Re: lua_switch, a shorthand for creating switch statements in lua

Posted: Tue Jul 05, 2022 8:08 am
by Gunroar:Cannon()
I like this one... I like this one very much :joker:
Seems useful to me.