lua_switch, a shorthand for creating switch statements in lua

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
jordan4ibanez
Prole
Posts: 24
Joined: Sun Dec 30, 2012 7:22 pm

lua_switch, a shorthand for creating switch statements in lua

Post 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
Check out my Github: https://github.com/jordan4ibanez/
It's a dumpster
User avatar
pgimeno
Party member
Posts: 3655
Joined: Sun Oct 18, 2015 2:58 pm

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

Post by pgimeno »

Just because you can, doesn't mean you should ;)
User avatar
jordan4ibanez
Prole
Posts: 24
Joined: Sun Dec 30, 2012 7:22 pm

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

Post 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
Check out my Github: https://github.com/jordan4ibanez/
It's a dumpster
User avatar
keharriso
Party member
Posts: 109
Joined: Fri Nov 16, 2012 9:34 pm

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

Post 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:
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
User avatar
jordan4ibanez
Prole
Posts: 24
Joined: Sun Dec 30, 2012 7:22 pm

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

Post 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
Check out my Github: https://github.com/jordan4ibanez/
It's a dumpster
User avatar
Gunroar:Cannon()
Party member
Posts: 1139
Joined: Thu Dec 10, 2020 1:57 am

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

Post by Gunroar:Cannon() »

I like this one... I like this one very much :joker:
Seems useful to me.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest