When should one use lambda over a for statement? | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
+ 1

When should one use lambda over a for statement?

I know lambda is slower than a for statement so is there a reason to use it over for statements

5th Mar 2017, 6:46 PM
Michael Borgfield
Michael Borgfield - avatar
2 Respuestas
+ 6
Check out what you can do with a lambda: https://code.sololearn.com/cJqu6Q00Gidt/?ref=app To solve this I might have used nested for loops - it would also work. But I would have my code five-six times bigger, while I am only interested to get the result.
6th Mar 2017, 2:04 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 2
The problem that a lambda aims to solve and the one a for statement aims to solve is inherently different. A lambda is an anonymous method, it's used to write a function inline without having to go through the trouble of writing the whole function declaration syntax. A for statement is a loop intended to run a piece of code a specific amount of times. A lambda could contain a for statement if you wanted it to. Their use isn't the same so you wouldn't really have a situation where you have to decide between them.
5th Mar 2017, 7:06 PM
Alexandre Sabourin
Alexandre Sabourin - avatar