Hacker News new | ask | show | jobs
by e12e 3398 days ago
I was going to comment along these lines - while functools exists, where appropriate I'd say iterators and/or list comprehensions are more pythonic. Note that if one wants to work with simple reduce functions, there's the operator module to help:

  from functools import reduce
  from operator import add

  # I'd also say this makes for more readable code,
  # documenting intent - but many will probably say that
  # basic arithmetic should be clear enough:

  def is_even(n): return n % 2 == 0
  def square(n): return n*n

  # Don't do this for summing integers, just use "sum":
  reduce(add,
    (square(n) for n in
      range(1,5) if is_even(n)))
  > 20
Note the use of range() rather than literal list - if you have a list, that should probably be passed in by name.

Anyway, the point wasn't so much bikeshedding or code golfing - just expanding on what I think is "more pythonic" take on it.

And to be clear, I'd probably prefer:

  sum(x * x for x in range(1,5) if x % 2 == 0)
for this particular example. And for more complex "real world" cases, I'd probably prefer to define my "reduce" function directly ie:

  def my_sum(iterator): return reduce(add, iterator)
  int_sequence = range(1,5)

  my_sum(square(n)
    for n in int_sequence
      if is_even(n))
  > 20
Note that python has map and filter as built-ins, so it's also possible to do:

  sum(map(lambda x:x*x,filter(lambda n:n%2==0,range(1,5))))
  # Which I find rather unreadable, but gets a bit better
  # without lambdas:
  sum(map(square, filter(is_even,int_sequence)))

(Phew, please excuse my personal dive into new/old list-comprehension/functional python -- but at least now I'm more clear on why everyone keep adding "threading" macros/syntax to their functional languages :-)

[ed: And if one wants threading-like syntax along with some parallelization and lazy-ness in python, there's a module for that: https://github.com/EntilZha/PyFunctional

  from functional import seq
  (seq(range(1,5))
    .map(square)
    .filter(is_even)
    .reduce(add)
  )
Just FYI.]