top | item 36857155

(no title)

di456 | 2 years ago

With python I'd decompose that one-liner into several variables for readability. That probably ends up using more memory than it would otherwise but I generally don't work on systems where that matters much.

Scala was really nice for this syntax when I used it for Spark.

discuss

order

Jtsummers|2 years ago

Map and filter don't actually consume anything until they're used later, they produce iterables. So if you pulled them into their own lines they wouldn't consume (much) extra memory. Taking the original:

  min(map(some_op, filter(some_filter, bar.baz()))).foo()
An alternative is also to use a generator comprehension that's identical to the inner part (in effect):

  min(some_op(item) for item in bar.baz() if some_filter(item)).foo()
Which could still be pulled out to a pair of lines for clarity:

  items = (some_op(item) for item in bar.baz() if some_filter(item)) # or some better name given a context
  min(items).foo()