When i look at it i don't know if ever i will need it. probabely the function is useful but, i wonder if ever You've used it.
Pages 1
You are not logged in. Please login or register.
AudioGames.net Forum → Developers room → Python reduce function. Ever You needed it?
When i look at it i don't know if ever i will need it. probabely the function is useful but, i wonder if ever You've used it.
You'll probably never use it. It doesn't come up very often and, in Python, it doesn't look so nice as it does in languages with better iterator support. Also, in Python 3 you have to use functools.reduce instead.
Just use for loops. Unless it's something incredibly, incredibly simple for loops are easier to figure out.
@Kamlorn
Yea i use python3, it looks highly messy with lambdas imho. What is Your openion about that?
I don't know what you're asking. My opinion about lambdas in general?
@Kamlorn
No i was meaning "Reduce func" looks too messy when it's been used within Lambdas altogether.
yes. That's because Python doesn't let you write it left to right. In Rust, if I had a list:
let product_of_evens = mything.iter()
.filter(|x| x%2 == 0)
.fold(1, |x, y| x * y)
Finds all the even numbers in the list, then multiplies them together.
But you're never going to make it look this nice in Python, no.
reduce is basically the functional version of a for loop. as you've noticed and Camlorn pointed out, it really is not very nice to use in python, so better to stick to for loops and list comprehensions. generally, functional style programming is impractical in python, because its syntax for lambdas, makes them impractical except for one liner functions.
in other languages, with better lambda syntax, reduce is very useful and I use it all the time.
such languages include all functional languages under the sun (Scala, Haskell, Rust, etc.), but lambda syntax is also quite nice for JavaScript and Java, where I use reduce more often than a for loop.
AudioGames.net Forum → Developers room → Python reduce function. Ever You needed it?
Generated in 0.026 seconds (51% PHP - 49% DB) with 9 queries