News for hackers. Stuff that matters.
Re:Python: Uncovering the Overlooked Core Functionalities by gorgoiler on Monday July 24, 2023 @11:30PM (#36857522)
Your point about ordering and readability really rang true for me. My way around this in Python is to separate the map and the reduce: do the map in one part with a list comprehension and the reduce in a second part on a new line.

I’ll wrap the whole thing in a named function as a way of describing what I’m doing and make it a closure if it’s used only once:

  def f(bar):
    def smallest_baz():
      bazs = (
        some_op(b)
        for b in bar.baz()
        if some_filter(b)
      )
      return min(bazs)

    return smallest_baz().foo()

There are no comments on this comment yet.