[Python-ideas] Haskell envy

Nick Coghlan ncoghlan at gmail.com
Mon Apr 23 03:26:38 CEST 2012

On Mon, Apr 23, 2012 at 11:07 AM, Nestor <nestornissen at gmail.com> wrote:
> I wonder if we should add a subsequences function to itertools or make
> the "r" parameter of combinations optional to return all combinations
> up to len(iterable).

Why? It just makes itertools using code that much harder to read,
since you'd have yet another variant to learn. If you need it, then
just define a separate "all_combinations()" that makes it clear what
is going on (replace yield from usage with itertools.chain() for
Python < 3.3):

from itertools import combinations

def all_combinations(data):
    for num_items in range(1, len(data)+1):
        yield from combinations(data, num_items)

def array_test(arr):
   biggest = max(arr)
   data = [x for x in arr if x != biggest]
   for combo in all_combinations(data):
       if sum(combo) == biggest:
           return True, combo
   return False, None

When a gain in brevity increases the necessary level of assumed
knowledge for future maintainers, it isn't a clear win from a language
design point of view.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list