def unique(iterable, is_sorted=False): """Returns an iterable sequence of unique values from the given iterable. :param iterable: Iterable sequence. :param is_sorted: Whether the iterable has already been sorted. Works faster if it is. :returns: Iterable sequence of unique values. """ # If we used a "seen" set like the Python documentation implementation does, # we'd have to ensure that the elements are hashable. This implementation # does not have that problem. We can improve this implementation. if iterable: def _unique(memo, item): """Find uniques.""" cond = last(memo) != item if is_sorted else omits(memo, item) if cond: memo.append(item) return memo return builtins.reduce(_unique, itail(iterable), [head(iterable)]) else: return iterable
def _flatten(memo, item): """Flattener.""" if isinstance(item, (list, tuple)): return memo + builtins.reduce(_flatten, item, []) else: memo.append(item) return memo
def fitness_and_quality_parsed(mime_type, parsed_ranges): """Find the best match for a mime-type amongst parsed media-ranges. :param mime_type: The mime-type to compare. :param parsed_ranges: A list of media ranges that have already been parsed by parsed_media_range(). :return: A tuple of the fitness value and the value of the 'q' quality parameter of the best match, or (-1, 0) if no match was found. """ best_fitness = -1 best_fit_q = 0 (target_type, target_subtype, target_params) = parse_media_range(mime_type) for (main_type, subtype, params) in parsed_ranges: type_match = (main_type == target_type or main_type == ASTERISK_BYTE or target_type == ASTERISK_BYTE) subtype_match = (subtype == target_subtype or subtype == ASTERISK_BYTE or target_subtype == ASTERISK_BYTE) if type_match and subtype_match: param_matches = builtins.reduce(lambda x, y: x + y, [ 1 for (key, value) in target_params.iteritems() if key != "q" and params.has_key(key) and value == params[key] ], 0) fitness = (main_type == target_type) and 100 or 0 fitness += (subtype == target_subtype) and 10 or 0 fitness += param_matches if fitness > best_fitness: best_fitness = fitness best_fit_q = params["q"] return best_fitness, float(best_fit_q)
def compose(function, *functions): """Composes a sequence of functions such that:: compose(g, f, s) -> g(f(s())) :param functions: An iterable of functions. :returns: A composition function. """ def _composition(a_func, b_func): """Composition.""" def _wrap(*args, **kwargs): """Wrapper.""" return a_func(b_func(*args, **kwargs)) return _wrap return builtins.reduce(_composition, functions, function)
def partition(predicate, iterable): """Partitions an iterable into two iterables where for the elements of one iterable the predicate is true and for those of the other it is false. :param predicate: Function of the format:: f(x) -> bool :param iterable: Iterable sequence. :returns: Tuple (selected, rejected) """ def _partitioner(memo, item): """Partitioner.""" part = memo[0] if predicate(item) else memo[1] part.append(item) return memo return tuple(builtins.reduce(_partitioner, iterable, [[], []]))
def flatten(iterable): """Flattens nested iterables into a single iterable. Example:: flatten((1, (0, 5, ("a", "b")), (3, 4))) -> [1, 0, 5, "a", "b", 3, 4] :param iterable: Iterable sequence of iterables. :returns: Iterable sequence of items. """ def _flatten(memo, item): """Flattener.""" if isinstance(item, (list, tuple)): return memo + builtins.reduce(_flatten, item, []) else: memo.append(item) return memo return builtins.reduce(_flatten, iterable, [])
def reduce(transform, iterable, *args): """Aggregate a sequence of items into a single item. Python equivalent of Haskell's left fold. Please see Python documentation for reduce. There is no change in behavior. This is simply a wrapper function. If you need reduce_right (right fold):: reduce_right = foldr = lambda f, i: lambda s: reduce(f, s, i) :param transform: Function with signature:: f(x, y) :param iterable: Iterable sequence. :param args: Initial value. :returns: Aggregated item. """ return builtins.reduce(transform, iterable, *args)
def bytes_to_uint_simple(raw_bytes): """Simple bytes to uint converter.""" return builtins.reduce(lambda a, b: a << 8 | b, builtins.map(builtins.byte_ord, raw_bytes), 0)
def bitarray_to_integer(bitarray): return int(reduce((lambda a, b: (int(a) << 1) + int(b)), bitarray))
def bitarray_to_integer(bitarray): return int(builtins.reduce((lambda a, b: (int(a) << 1) + int(b)), bitarray))