Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
retoor33151dIt's not much different than a variable. You would think function definition would be the same every iteration but it doesn't have to be. You could use an outside var that changes behavior that you don't want as parameter of the function. This could be resolved with functools.partial too of, but still within loop. But using the function after that outside the loop that would be criminal. I think python can do that.
But in general, do you really see people declaring functions in loops?
Language can do this probably by coincidence, not a real feature. -
Only if the anonymous inner function logic is mutated by the loop logic somehow.... and even then - FUCK That.
-
This is… genius! I mean I would spank the shit out of anybody who does this in prod but it tickles by brain in a funny way 😂
-
Like... Huh?!
Push an anonymous function into an array outside the loop?
Wha- Why even?! -
lorentz1553722hWhat you're creating is just a value that contains the lexical bindings, it's like creating any other value in a loop.
Relatedly, I think the biggest failure of "everything is an object" is the lengths users of these languages will go to in order to avoid creating objects. -
AlgoRythm5028622hThis is only possible in scripting languages and thank the good fucking lord because if I had to work with some c++ code that dynamically created a function each loop and - god forbid - I was also responsible for OPTIMIZING IT?
Imagine fucking debugging code where the floorboards are ripped out from under you each iteration of the loop.
This is the sort of code that would make me kill you, your wife, and them myself if I was forced to work on it. -
lorentz1553722hInstances of a closure don't involve
- code
- a heap allocation
- a virtual call
- locks
- a runtime value that uses memory
They are values with a compile-time associated function. Everything else is a consequence of how you use it and how much your language cares about optimizing it. -
lorentz1553722h@AlgoRythm C++ has better support for closures than most languages. You can create a closure every iteration, and odds are it will be basically free.
-
AlgoRythm5028622h@lorentz My interpretation of the rant wasn't a closure but literally returning a function from within a loop. In javascript you can accomplish this like this:
return new Function("return 123");
This certainly isn't possible in compiled languages as it would skip the whole "compiled" part. -
lorentz1553722h@AlgoRythm Well yes it is but it would involve a JIT compiler. I think LLVM will do it.
-
AlgoRythm5028622h@lorentz Sure, if you're shipping LLVM with your C++ then you can do whatever the fuck you want, realistically.
That's a big "if" though. It's certainly not possible in the same way JavaScript allows - as a built-in feature of the language. -
AlgoRythm5028622h@lorentz the idea of c++JIT piqued my interest. There's actually a library for it.
If anyone uses it for anything besides appropriate applications like a REPL, I will shoot them.
https://blog.quarkslab.com/easyjit-... -
Demolishun3591120hAdded to my list of making people not want to work here anymore list. I know I can create lambdas inside functions. I had not thought of creating them in if statements and loops...
Thanks for the tip!
Edit: Oh fuck! Recursively created lambdas! YES! -
retoor331512hPeople took my post too seriously I guess. I never said to actually do it. I'm just a fan of weird creative stuff.
Creating functions inside loops should be a felony
rant