Opened 6 years ago

Closed 6 years ago

#28961 closed Cleanup/optimization (wontfix)

Convert result of predicate in the function django.utils.functional.partition to bool

Reported by: Vitaliy Yelnik Owned by: nobody
Component: Utilities Version: 1.11
Severity: Normal Keywords: functional partition
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

def partition(predicate, values):
    results = ([], [])
    for item in values:
        results[bool(predicate(item))].append(item)
    return results

This will make the partition function more convenient

>>> def idf(x): return x
>>> partition(idf, [True, False, '', 1, 2])
([False, ''], [True, 1, 2])

or make the behaviour of the function more similar to `Haskell partition`

The partition function takes a predicate a list and returns the pair of lists of elements which do and do not satisfy the predicate, respectively

def partition(predicate, values):
    results = ([], [])
    for item in values:
        results[not predicate(item)].append(item)
    return results

>>> def idf(x): return x
>>> partition(idf, [True, False, '', 1, 2])
([True, 1, 2], [False, ''])

Change History (2)

comment:1 by Vitaliy Yelnik, 6 years ago

Summary: Convert result of the predicate in the function django.utils.functional.partition to boolConvert result of predicate in the function django.utils.functional.partition to bool

comment:2 by Tim Graham, 6 years ago

Resolution: wontfix
Status: newclosed

partition() isn't a documented function and I don't see a strong reason to add logic to it that Django's usage doesn't require.

Note: See TracTickets for help on using tickets.
Back to Top