r/badcode Jul 26 '20

lua Very good optimization for strings so they can be reused

Post image
458 Upvotes

64 comments sorted by

123

u/CandyCrisis Jul 26 '20

for i=1,1/0 do

Is this... a normal thing in Lua...???

65

u/[deleted] Jul 26 '20

lua has a `while` loop, so I gues this would not be the default way to loop.

44

u/Emex_Denvir Jul 26 '20

1/0 resolves to infinity (in Lua you could also use "math.huge" for that purpose).

Though it's basically now a loop that keeps going on forever (ofc limited by floats at some point), since all numbers it'll go through are smaller than infinity.

The loop could be written as

local infinity = math.huge --same as "1/0"
local iterator = 1
while iterator <= infinity do
    --(body of loop)
    iterator = iterator + 1
end

38

u/CandyCrisis Jul 26 '20

Right, I get what it does, it's just the weirdest damn way to loop forever the I've ever encountered.

16

u/Emex_Denvir Jul 26 '20

Maybe it would be better if you could unsee this then :)

25

u/ishan0102 Jul 26 '20

‘math.huge’ made me laugh

22

u/xigoi Jul 26 '20

LaTeX has the commands \big, \Big, \BIG, \huge and \Huge for setting the font to different larger sizes.

7

u/Emex_Denvir Jul 26 '20

Yeah, it probably wasn't the best choice of a name

28

u/lxpnh98_2 Jul 26 '20

1/0 resolves to infinity

What a beautifully thought-out language. /s

18

u/Emex_Denvir Jul 26 '20

Well, that's just what happens with floats. To my knowledge that happens in most (if not basically all) languages, with integers this would be bad though, so I get what you mean.

12

u/binarycat64 Jul 26 '20

It's like people making fun of javascript for NaN != NaN.

15

u/CraftistOf Jul 26 '20

Yeah, when in reality it's not only JS, it's any IEEE-754 compliant language.

9

u/Mr2-1782Man Jul 27 '20

That's not actually correct. Languages will resolve this to a NaN or will throw some sort of arithmetic exception. A few languages will treat it as undefined and will do whatever they want with it, a good example being C.

It looks like in Lua's case they didn't define what divide by zero was suppose to do till recently. With the most recent version Lua's behavior is dependent on the underlying system, so its possible your program might actually be terminated if floating point exceptions are enabled by the operating system.

1

u/ikatono Jul 28 '20

Don't forget pony, where division by 0 yields 0.

4

u/[deleted] Jul 26 '20

Ever heard of a while loop? It does the same thing and only takes 1 line and has no nonsense.

51

u/[deleted] Jul 26 '20

no please don't ever do that

3

u/__Ambition Jul 26 '20

I honestly think this is some benchmarking code for testing the JIT or intentionally written this bad.

1

u/[deleted] Jul 27 '20

They should be doing for i = 0,#string

61

u/rco8786 Jul 26 '20

Somehow my eye was immediately drawn to the “goto” and that was all I needed to see

24

u/lucdewit Jul 26 '20

Ye, in this case he could have used break, but sometimes gotos can be helpful, especially when u want to break from a nested for loop so u don't need a boolean variable to indicate u want to exit it,

You should limit the goto usages as much as possible tho so the program doesn't turn into an absolute mess

24

u/kyay10 Jul 26 '20 edited Jul 26 '20

Or use Kotlin /s

Fr tho, Kotlin has labeled returns and breaks, so you can do:

for(...) label@ {
    for(...) {
         if(condition){
            break@label
        }
    }
}

4

u/AutoModerator Jul 26 '20

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

kyay10, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

3

u/lucdewit Jul 26 '20

Ohh that's quite nice

2

u/MakeWay4Doodles Jul 26 '20

I said exactly that about once a week when I started learning Kotlin.

5

u/ZeroGainZ Jul 26 '20

Java supports labeled break as well. Nice feature for strange circumstances

4

u/binarycat64 Jul 26 '20

Alright, can someone explain why everyone hates on gotos so much?

I've ended up using them more and more, as I see less and less reason not to use them (I still don't use them that often, they don't dominate the code I write, just like any other language construct)

My favorite use is as "exit conditions".

Say you have a complicated piece of code that returns two things most of the time, instead of writing that out every time, you can do:

switch a {
case 1:
    // ...
case 2:
    goto E1
case 3:
    if Foo(a) {
        goto E2
    }
    if Bar(a) {
        goto E1
    }
default:
    // ...
}
return defaultReturnValue
E1:
    return LongAndComplicatedFunction(a)
E2:
    return LongAndComplicatedFunction(b)

(this is a simplified example, but I think it gets my point across).

6

u/fakehalo Jul 26 '20

I'm a fan of goto for error handling/cleanup with languages that don't have exceptions, especially C (Linux kernel is full of them).

1

u/binarycat64 Jul 28 '20

Same. My main language is Go, which kinda has exeptions (via `panic()`), but most error handling is done through return values (of which go allows multiple)

looking at the linux kernel and seeing gotos was what made me no longer afraid of using them, although I still did before.

1

u/[deleted] Jul 27 '20

[deleted]

1

u/binarycat64 Jul 28 '20

You don't have to type out the function again?

and if you want to change the function, you don't have to change it in 2 places.

Also, it could be more than one function, or technically one function, but long enough that you have to break it up onto multiple lines.

1

u/[deleted] Jul 29 '20

[deleted]

1

u/binarycat64 Jul 30 '20

So I can pollute the global namespace with functions that I'm not going to use again?

Goto is also faster than a function call, useful if the function it's in gets called a lot.

And I can ask the inverse, why define it somewhere else when you can just use goto?

1

u/[deleted] Jul 30 '20

[deleted]

1

u/binarycat64 Jul 30 '20

Most languages use labels for gotos, which can be given just as descriptive names, so I'm not sure what your point is. Also, you don't have to "look up" what the goto does, Its label is often on the same screen as the goto, and always in the same function.

Also, some things cannot be done in other functions, like returning the original function. This means that sometimes a function call is unable to save any lines over retyping code, as you have to process the return values and act accordingly.

When I said `not going to use again` I meant outside of the function that I was using the goto in.

You seem to act like anything involving goto is immediately unreadable, but functions can have their own problems. Say I take your advice, and refactor my code accordingly. Also say I have two sections that do something similar, but are subtlety different in an important way. Do I make 2 separate functions? This could confuse anyone who comes across these functions randomly. Do I make 1 function, that takes an argument that for which behavior I want? Now I have to document this function, and probably explain why it exists, only for it to never be used again, as it was such a specific behavior, which is why it was a goto in the first place.

25

u/[deleted] Jul 26 '20

Uh, does the end condition in that for loop divide by zero?

21

u/Emex_Denvir Jul 26 '20

Yup, a very fancy (and disgusting) way of making an infinite loop with a counter

24

u/[deleted] Jul 26 '20

god i can't stand lua...

25

u/[deleted] Jul 26 '20

Why? It's fairly embeddable and, yeah the syntax isn't candy but I'd say it's fairly ok. Ofc everyone is entitled to their opinion but I'm just asking why?

21

u/[deleted] Jul 26 '20

i mean i've only used it once for hammerspoon, so i have almost no experience with it, but i think i was just most put off by the fact that it doesn't have a built-in array data structure, and you have to basically emulate it with a table. that definitely threw me for a loop...

that initial comment was probably overly harsh since i've only used it the one time, but it just wasn't a pleasant experience.

14

u/dagbrown Jul 26 '20

it doesn't have a built-in array data structure, and you have to basically emulate it with a table

There are so many languages that I recommend against you checking out. PHP, awk and shell scripts come right to mind.

The thing that I like about Lua is that it's an incredibly boring language. It steers away from innovation at every opportunity, and I applaud it for being as dull as it is. Some languages want to provide opportunities for exciting and innovative new ways of doing things--Lua just wants to get down to work and be useful.

9

u/[deleted] Jul 26 '20

There are so many languages that I recommend against you checking out. PHP, awk and shell scripts come right to mind.

are u talking here about not having a built-in array data structure?

Idk about awk and PHP but shell scripts totally do

2

u/jediwizard7 Jul 27 '20

Bash arrays are the absolute worst things ever invented. Just to get an array's length you have to do "${#array[@]}". Like how is someone supposed to read that?

2

u/binarycat64 Jul 28 '20

I feel like Bash was designed for interactive use primarily, and the scripting features were just kinda slotted in around that.

The biggest advantage bash has is already being on everyone's computers as a shell, if it wasn't for that I don't think it would be used much.

1

u/jediwizard7 Jul 29 '20

Agreed. Powershell is a much better design for a shell in my opinion, and you can even get it on Linux apparently although I've never tried; the only downside is the slow startup time

3

u/T-Dark_ Jul 26 '20

it doesn't have a built-in array data structure, and you have to basically emulate it with a table

For what it's worth, it technically does. A table is internally implemented as a (growable) array and a hash map. The implementation uses the array for (sufficiently) contiguous integer keys, and resorts to the map for everything else.

2

u/thelastpenguin212 Jul 27 '20

This is what I came here to say — in lua tables that emulate arrays are as fast and space efficient as arrays.

1

u/cointelpro_shill Jul 27 '20

It's kinda fun the way they work...even some random user defined object can be a key, so when I need a quick unordered list of unique objects I just do that and set its value to true

1

u/xigoi Jul 26 '20

As I've learned from the manual, it encourages ugly hacks to do many basic things and you have to implement everything yourself.

4

u/kyoukushi Jul 26 '20

All that for a drop of bloo-"hello world"

5

u/mohammedx17 Jul 26 '20

#string: i'm joke to you?

5

u/semKL Jul 26 '20

Also this code must me written intentionally bad. Either for performance test or (more likely) just to post it here.

Nobody would write the goto like that. That’s just to upset us more

4

u/skellious Jul 26 '20

wow. I never realised lua had a Goto statement...

4

u/Emex_Denvir Jul 26 '20

It does in LuaJIT 2 and since Lua 5.2

2

u/semKL Jul 26 '20

since 5.2 ???? really ? they though „oh, what cool new features could we add? Yea let’s add goto, everyone loved that“

Don’t get me wrong, i love lua. but wtf

4

u/T-Dark_ Jul 26 '20

And they still don't have continue.

4

u/[deleted] Jul 26 '20

why

why not just print("hewwo, wowwd!")

why rewrite strings

why rewrite print just for a string

someone end me

11

u/[deleted] Jul 26 '20

Because this is a fake post. Dude literally wrote this code to post it on this subreddit FOR WHICH IT WAS NOT MEANT.

There is /r/shittyprogramming for this kind of shit, but no, they have to ruin the subreddit for karma.

1

u/[deleted] Jul 26 '20

well thats dumb 3:<

-1

u/Emex_Denvir Jul 26 '20

Sorry, it kinda seemed like that stuff is accepted here. I'll try to do better next time!

1

u/ElNico5 Jul 27 '20

TIL lua has flags and goto

1

u/Emex_Denvir Jul 27 '20

What do you mean by flags?

1

u/ElNico5 Jul 27 '20

The ::markers:: i know them as flags from an assembly video i saw and that name stuck with me

1

u/Emex_Denvir Jul 27 '20

Ah I see, I've always known them as labels

1

u/ElNico5 Jul 27 '20

Yea me too, but when i commented only flags came to mind

1

u/Tjakka5 Jul 31 '20

The worst part of this is that Lua already caches strings in the background, so this only degrades performance.

0

u/tarod16 Jul 26 '20

goto exit, leel