I'm learning python by my own, so I don't know anybody to ask for help or ask my dumb questions...and I come here for that. Here the problem: I trained some kata on codewars, can't solve one, so I saw the solution and I can't even get how does this code work! Of course, I read about next()
and cycle()
, but why they're together there...I just can't get it. Please, could you explain this code, how does it work and why does it work in this way.
from functools import reduce
from itertools import cycle
def reduce_by_rules(lst, rules):
rs = cycle(rules)
return reduce(lambda x, y: next(rs)(x, y), lst)