Can someone make it an async function?
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
Returning 10 instead of x when x finally ends up being 10 really ties it together.
I'm glad you noticed. That was my favorite part too.
Probably Microsoft: You’re hired! Go work on GitHub
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
It would get you promoted at Twitter, where lines of code is the productivity metric.
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.
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
I would love to see the implementaion.
The implementation is not very exciting, I capture a variable in python. It could have been done more cleanly.

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

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.
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.
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"
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)
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.
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.
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.
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.
Multiplication is just repeated addition :) glad it worked for the kid, despite the... inefficiency.
How about
x=x-x
x++
x++
x++
x++
x++
x++
x++
x++
x++
x++
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.
This is actually a valid brainf*ck program, but it results in 19, not 10.
The compiler will optimize it anyway. /s
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.
Not sure about the last one though. The other two are trivial to optimize away.
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.
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()...
Oddly enough, out of all of these the one the compiler has the best chance of optimizing out is the last one
Not if Random writes to global state, that's a side effect that must be preserved
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.
What is the value of x in the Good example before the loop?
Doesn't matter, it's 10 after the loop.
x = (function(){
return 10
})
Or something like that
Wants to be Pro but doesn't even do it recursive...
function foo() {
x = new Random();
case (x = 10):
return (x);
default:
foo()
}
Programmer Humor
Welcome to Programmer Humor!
This is a place where you can post jokes, memes, humor, etc. related to programming!
For sharing awful code theres also Programming Horror.
Rules
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics