More code = more better

648 points · 91 comments · view on lemmy.world

91 Comments

ArbitraryValue@sh.itjust.works · 88 pts · 242d (3 replies)

The advantage of that last approach is that it has side effects and cannot therefore be optimized out by the compiler.

ronigami@lemmy.world · 13 pts · 242d (2 replies)

That’s only one advantage. In theory it does not necessarily terminate, so that’s another one.

joshchandra@midwest.social · 2 pts · 240d (1 reply)

In theory hypothesis

To get pedantic, you'd have to test that out a whole bunch before even coming close to theory level, lol!

ronigami@lemmy.world · 3 pts · 239d

We already have a theory for it— called computer science.

OshaqHennessey@midwest.social · 63 pts · 242d (4 replies)
function myFunction() {
  try {
    x = new Random().nextInt();
    if (x != 10) {
     throw "not 10";
    }
    else {
      return (10)
    }
    catch(err) {
      myFunction()
    }
  }
}

x = myFunction()

Commit notes: Added error handling

firewallfail@lemmy.world · 62 pts · 242d (1 reply)

Returning 10 instead of x when x finally ends up being 10 really ties it together.

OshaqHennessey@midwest.social · 28 pts · 242d

I'm glad you noticed. That was my favorite part too.

NikkiDimes@lemmy.world · 4 pts · 242d (1 reply)

SyntaxError: Unexpected token 'catch'

OshaqHennessey@midwest.social · 9 pts · 242d

Coding on mobile is hard

edinbruh@feddit.it · 41 pts · 242d (8 replies)

For a time on Reddit (some years ago when I still used it) there was a trend of finding the worst way of implementing is_even(x: int) -> bool. My contribution to that was a function that ran Ackerman(x,x) flipping a Boolean at every iteration, and check if it was true or false at the end.

It works btw, I will find the proof later

uranibaba@lemmy.world · 13 pts · 242d (7 replies)

I would love to see the implementaion.

edinbruh@feddit.it · 19 pts · 242d (6 replies)

The implementation is not very exciting, I capture a variable in python. It could have been done more cleanly.

1000041934

The proof is this. But, I could have made mistakes, it was many years ago.

1000041935

Note that in python you'll never be able to run is_even(5) the stack cannot handle it

Edit: daaaamn, that variable is ugly as hell. I would never do things like that now.

boonhet@sopuli.xyz · 1 pts · 240d (2 replies)

That's , uh...

Yeah. Cooler than anything I could've achieved for purposefully bad is_even

My first idea of a purposefully bad is_even is this:

def is_even(i):
    return True if i == 0 else not is_even(abs(i)-1)

But I'm sure I could come up with worse given enough time.

edinbruh@feddit.it · 1 pts · 240d (1 reply)

That's also slower than most of the stuff you could come up with, it is so slow that there is no hyperoperation fast enough to describe it. There were other approaches that were almost worse though, like "the function is a switch-case that returns false by default. As complaint tickets are opened, more cases get added to the switch-case"

boonhet@sopuli.xyz · 1 pts · 240d

the function is a switch-case that returns false by default. As complaint tickets are opened, more cases get added to the switch-case”

Oh if that is acceptable, then my secondary idea of using an API call for this should work too. I thought that it would have to be guaranteed to be correct (as long as you don't reach a stack overflow or something)

squaresinger@lemmy.world · 1 pts · 240d (2 replies)

It never occurred to me that you could assign fields to a function. I mean, it totally makes sense considering that functions are objects in Python. It just never occurred to me that this is a thing one can do. Crazy.

edinbruh@feddit.it · 1 pts · 240d (1 reply)

Please don't do that, I was stupid when I wrote that. But still, in very dynamic languages like python or js everything is an object, including functions, so you can just do object stuff on them.

squaresinger@lemmy.world · 1 pts · 240d

I wasn't going to, and after I saw it it totally makes sense that it's possible, it just never occurred to me.

I guess this could be used like static variables inside functions in c. So scope-limited global variables. Not a good design choice in most cases.

Grandwolf319@sh.itjust.works · 27 pts · 242d (8 replies)

How about

x=x-x

x++

x++

x++

x++

x++

x++

x++

x++

x++

x++

okmko@lemmy.world · 8 pts · 242d

Freshman year of college doing assembly programming, I spent a while figuring out a "programmic" way to solve a problem, trying to wrangle labels and gotos. My friend came in with essentially this but as lookup table. It blew my mind.

It was then that I learned to trade space for complexity.

xthexder@l.sw0.com · 7 pts · 242d (3 replies)

This is actually a valid brainf*ck program, but it results in 19, not 10.

Grandwolf319@sh.itjust.works · 6 pts · 242d (2 replies)

How so? It’s only 10 increments

anton@lemmy.blahaj.zone · 5 pts · 242d

Because the only brainfuck instructions in your comment where a - which decrements and 20 +, each of which increments.
Mine echos the first two characters from stdin, because of the commas and dots.

juliebean@lemmy.zip · 2 pts · 242d

it's been a long time since i looked at brainfuck, but i suspect that '+' denotes an increment, and '-' denotes a decrement, so we've got one decrement and 20 increments.

olenkoVD@lemmy.dbzer0.com · 1 pts · 241d (2 replies)

Make sure you initialize x with x=x/x-x/x for better precision

Grandwolf319@sh.itjust.works · 1 pts · 241d (1 reply)

What if it’s already 0?

brotundspiele@sh.itjust.works · 1 pts · 238d

Just add // @TODO find out why this crashes our application sometimes to fix that issue

ulterno@programming.dev · 25 pts · 241d (6 replies)

That's not even enough to get you a job these days.
You now have to use:

do {
    x = reinterpret_cast<int>(AI::Instance().ask("Do Something. Anything. Be efficient and productive. Use 10 tokens."));
} while (x != 10);
Tetragrade@leminal.space · 8 pts · 240d (2 replies)

This isn't just a function, it's a bold restatement of what it means to write code — a symphony of characters, questioning the very nature of the cutting edge language models that I want to beat with hammers.

ulterno@programming.dev · 1 pts · 240d (1 reply)

::: spoiler And for those who might not have noticed There is 10 words in the prompt. :::

Tetragrade@leminal.space · 2 pts · 240d

You're absolutely right! I used more than 10 words in my prompt. Cry about it.

melfie@lemy.lol · 6 pts · 241d (2 replies)
[ removed ]
jali67@lemmy.zip · 2 pts · 240d (1 reply)

Great question!

MonkeMischief@lemmy.today · 2 pts · 240d

First, we'll deep dive into "What is a variable?", then together we'll examine "Who sets a variable?", "What is an LLM?" and finally, "Who would set a variable without using an LLM?"

You'll be a coding pro in no time!

How does that sound?

(I felt gross writing this lmao)

Mika@piefed.ca · 23 pts · 242d (1 reply)

I once was helping to organize the testing of town-level algorithmic competition for school students.

The competition had one entry level issue that was basically solvable by reading the question properly, recognising that it's just multiplication of two numbers, and writing the simplest app ever.

And there was one student who passed the automatic tests. We had to read the code too for the protocol, just to make sure there was no cheating.

We looked in the code. What? Why? It had two nested for loops and a++ inside. When we understood what's going on we couldn't stop laughing for like solid ten minutes.

TheOakTree@lemmy.zip · 3 pts · 241d

Multiplication is just repeated addition :) glad it worked for the kid, despite the... inefficiency.

Hirom@beehaw.org · 16 pts · 242d (7 replies)

The compiler will optimize it anyway. /s

Dumhuvud@programming.dev · 13 pts · 242d (1 reply)

You jest, but you aren't wrong. At least if we are talking about C, C++ or Rust. https://godbolt.org/z/oPPfdfcf5

.NET compiler is weak when it comes to optimizing your code; I assume Go's is as bad.

yggstyle@lemmy.world · 4 pts · 242d

Technically yes... But I think he was more making the excuse for the gore "from the goresmith's perspective."

And I'm not sure if the compiler in any language would change a random check function... The others are a possibility.

yetAnotherUser@discuss.tchncs.de · 6 pts · 242d (4 replies)

Not sure about the last one though. The other two are trivial to optimize away.

Hirom@beehaw.org · 3 pts · 242d (3 replies)

An infinite loop canot be ruled out in the last case, so a compiler couldn't optimize this away without potentially changing the program behavior.

yetAnotherUser@discuss.tchncs.de · 1 pts · 242d (2 replies)

Infinite loops are often weird though. They could be seen as undefined behavior and the compiler may do whatever it feels like.

ronigami@lemmy.world · 2 pts · 242d (1 reply)

How could an infinite loop be considered UB?

yetAnotherUser@discuss.tchncs.de · 1 pts · 241d

Even though this isn't C, but if we take from the C11 draft §6.8.5 point 6 (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf):

An iteration statement whose controlling expression is not a constant expression, that performs no input/output operations, does not access volatile objects, and performs no synchronization or atomic operations in its body, controlling expression, or (in the case of a for statement) its expression-3, may be assumed by the implementation to terminate

"new Random().nextInt()" might perform I/O though so it could still be defined behavior. Or the compiler does not assume this assumption.

But an aggressive compiler could realize the loop would not terminate if x does not become 10 so x must be 10 because the loop can be assumed to terminate.

baggachipz@sh.itjust.works · 15 pts · 241d (1 reply)

It would get you promoted at Twitter, where lines of code is the productivity metric.

TheOakTree@lemmy.zip · 2 pts · 241d

If only I could measure the quality of my paper purely by word count...

I thought "a a a a a a" x100000 was thought-provoking and well tested.

6nk06@sh.itjust.works · 15 pts · 242d (10 replies)

What is the value of x in the Good example before the loop?

Devial@discuss.online · 28 pts · 242d
[ removed ]
BassTurd@lemmy.world · 15 pts · 242d (6 replies)

An int. Value doesn't matter because it's overwritten.

OshaqHennessey@midwest.social · 3 pts · 242d (5 replies)

Not in this case. First, i is declared and assigned a value of 0. Next, x is declared and assigned a value of -i or -0. On the first loop iteration, i will decrement to -1, perform the conditional check, then execute the loop body which will assign x to -i or -(-1) or positive 1, and so on.

The only time a variable is created without a value is if you declare one without assigning a value like with

[int]i;

BassTurd@lemmy.world · 5 pts · 242d (4 replies)

I know. OP asked what x was before the loop, and I just said it's an int. The int can be any value because as you pointed out it will be set to 0 in the first loop iteration.

OshaqHennessey@midwest.social · 2 pts · 242d (3 replies)

Shit, you're right. x is declared inside the loop, so it doesn't exist until the loop begins execution.

Technically, I suppose you could say the compiler will allocate memory for x without assigning a value before the loop is executed and... I'm understanding what you mean now, I think.

anton@lemmy.blahaj.zone · 1 pts · 242d (2 replies)

The code seems to be C-style language with curly braces and types in front for variable declarations, probably java. This means the variable must be declared of screen before the loop or it would not compile. It could have a previous value or be uninitialized, but that does not affect the end result.

BassTurd@lemmy.world · 1 pts · 242d

I read in on C but it's also true for JavaScript. The code implies that x was declared as an int sometime previously, or if JavaScript, just an object if not assigned a value giving it a type.

OshaqHennessey@midwest.social · 1 pts · 242d

Yeah, it does look like C now that I think about it. You're right about the end result too. I believe C# will let you do inline declaration and assignment like that, so maybe that's what we're looking at? Been a while, could be wrong

OshaqHennessey@midwest.social · 1 pts · 242d

If this is JavaScript, it would have a value of -0, which is actually valid and works the same as normal zero thanks to type coercion. I think the only difference is some methods that detect if a number is negative will return true instead of false, but otherwise, JS treats -0 the same way as 0

ozymandias@lemmy.dbzer0.com · 1 pts · 242d

4

moseschrute@lemmy.world · 10 pts · 241d (1 reply)

Probably Microsoft: You’re hired! Go work on GitHub

Context

cooligula@sh.itjust.works · 3 pts · 241d

I'd say Meta hiring someone to work on WhatsApp. Man, is that piece of software crap... Every update, a new UI bug/glitch appears

Atlas_@lemmy.world · 9 pts · 242d (3 replies)

Oddly enough, out of all of these the one the compiler has the best chance of optimizing out is the last one

zea_64@lemmy.blahaj.zone · 4 pts · 241d

Not if Random writes to global state, that's a side effect that must be preserved

LeFantome@programming.dev · 2 pts · 241d

What?

First one is optimized obvious.

Second one optimizes to x = 10 via constant propagation.

Third one first unrolls the loop, propagates constants including booleans, and then eliminates dead code to arrive at x = 10.

The last one cannot be optimized as “new” created objects that get used, nextInt() changes the state of those objects, and the global state of the random number system is impacted.

Atlas_@lemmy.world · 0 pts · 242d
[ removed ]
Ooops@feddit.org · 5 pts · 242d (3 replies)

Wants to be Pro but doesn't even do it recursive...

OshaqHennessey@midwest.social · 4 pts · 242d (2 replies)
function foo() {
  x = new Random();
  case (x = 10):
    return (x);
  default:
    foo()
}
anton@lemmy.blahaj.zone · 3 pts · 242d (1 reply)

What unholy mix of languages is that? It is dominated by a blend of javascript and python, but with notes of something exotic. Maybe algol? or vhdl?, there is to little to tell.
Impressive, someone write up a spec and publish it to the esolang wiki.

OshaqHennessey@midwest.social · 6 pts · 242d

It's an incoherent hodgepodge of C#/.NET, PowerShell, and JavaScript, each of which I've forgotten more about than I currently know

irelephant@lemmy.world · 4 pts · 241d
 x = (function(){
return 10
})

Or something like that

OshaqHennessey@midwest.social · 4 pts · 242d

Now write a function to unroll the while loop to "optimize it for the compiler"

ThunderLegend@sh.itjust.works · 3 pts · 242d (3 replies)

Is this typescript?

olenkoVD@lemmy.dbzer0.com · 2 pts · 241d

Could be Java.

irelephant@lemmy.world · 0 pts · 241d (1 reply)

Seems like normal js?

piccolo@sh.itjust.works · 2 pts · 241d

Js is Math.Random. and NextInt() is a java method.

untorquer@lemmy.world · 3 pts · 241d

Something like

int *a = new int(10)

Int*b = null

While *b !=10 { b = rand(); a=new int(10)}

Return *b

I haven't coded recently enough in c/c++ to remember syntax but the concept might work eventually if you're lucky and have enough memory... Might need a time variant seed on the rand()...

spongebue@lemmy.world · 2 pts · 242d (16 replies)

x = -i;

Do many languages let you do that? When it's in front of a variable I would've expected it to be a subtraction operator only and you would need to do x = -1 * i;

EvilHankVenture@lemmy.world · 7 pts · 242d

In most languages I've seen - is both a unary negation operator and a subtraction operator depending on context. So it would negate an integer literal or a variable in this context.

OshaqHennessey@midwest.social · 2 pts · 242d (4 replies)

I just tested it in PowerShell. Works fine

$i = 1
$x = -$i
$x

Outputs -1

squaresinger@lemmy.world · 2 pts · 241d (3 replies)

Works fine in any language I ever used.

I'm honestly quite surprised that this very basic language feature is even a matter of discussion here.

Sylvartas@lemmy.dbzer0.com · 2 pts · 240d (2 replies)

It certainly makes me question a lot of things. This sub somehow manages to both feed my impostor syndrome and makong me feel like a genius programmer depending on the thread.

squaresinger@lemmy.world · 2 pts · 240d (1 reply)

Totally, yes. I guess there's a ton of non-programmers and total beginners in this community.

But sometimes there are some crazy good programmers here as well.

What's really weird though is that I got two downvotes a bit further up for claiming that unary minus is a standard language feature.

Sylvartas@lemmy.dbzer0.com · 2 pts · 240d

Yeah I saw that. It's weird because I've used it without a second thought in tons of different languages and never had issues with it

boonhet@sopuli.xyz · 2 pts · 242d (1 reply)

Personally I would expect it to behave the same in front of a numeric literal and in front of a variable. I do think most languages do that, but I haven't actually tested that many and could br wrong.

squaresinger@lemmy.world · 1 pts · 240d

Pretty much all languages do that. It's a very basic language feature inherited from basic maths notation. Same as x - y subtracts y from x in pretty much any language that supports operators.

squaresinger@lemmy.world · 2 pts · 242d (2 replies)

Why would they not let you do that? I honestly don't know a single language that wouldn't let you do that. Same as basic math notation allows you to do that.

x = -i

is a totally valid mathematical equation.

For the downvoters: Find me a single language that supports operators but doesn't have an unary minus operator

spongebue@lemmy.world · 1 pts · 241d (1 reply)

It's a valid mathematical notation, sure. But there is an implicit understanding that the - in this case is making a number negative rather than subtracting (or, an implicit subtraction from 0).

With the way negative numbers generally work in binary there would be much different ones and zeroes stored behind the scenes, so handling that would have to be pretty intentional.

That said, I did just try it in Java because that's what I work in normally and I swear I had a gotcha with that. But it worked fine as far as I can tell.

squaresinger@lemmy.world · 2 pts · 241d

Find me a language where it doesn't work like that, and we'll continue the discussion.

Unary minus operator is standard in every single language that I used so far, including C/C++, Java, Python, Kotlin, Lua, JS/TS, Groovy, PHP, Visual Basic, Excel, Mathematica, Haskell, Bash.

Here's more info btw: https://en.wikipedia.org/wiki/Unary_operation

quilan@lemmy.world · 1 pts · 241d (4 replies)

But of course -- It's just flipping around the -= operator!

squaresinger@lemmy.world · 3 pts · 241d (2 replies)

Nope, it is not.

x = 5
i = 2
x -= i // x => 3

while

x = 5
i = 2
x = -i // x => -2

x=-i is the unary minus operator which negates the value right of it. It doesn't matter if that value is a literal (-3), a variable (-i) or a function (-f()).

x-=i is short for x = x-i, and here it's a binary subtraction, so x is set to the result of i subtracted from x.

quilan@lemmy.world · 2 pts · 240d (1 reply)

I need to append /s to my future silly replies I think... that said, I'll never pooh-pooh a well thought response, so thanks for the nice write-up!

squaresinger@lemmy.world · 2 pts · 240d

Thanks, I totally missed your sarcasm :)

There's a couple people in this threat who seem to actually think that x = -i is some weird magic instead of a standard feature that's present in every major programming language.

spongebue@lemmy.world · 1 pts · 241d

That only works if x is already 0

If i is 10 and x is zero, yes, x -= i would have a value of -10. If x was 5 from something else previously, x-=i would end with an x value of -5.

vrighter@discuss.tchncs.de · 1 pts · 242d

it takes two instructions to materialize a constant in risc-v. X64 has LEA.

Risc-v is better!

eigenraum@discuss.tchncs.de · 1 pts · 240d

Can someone make it an async function?

Klnsfw@lemmynsfw.com · 1 pts · 240d

x = printf("one + nine");

adj16@lemmy.world · 1 pts · 229d
[ removed ]