Debian Codegraph @

pypy/module/__builtin__/app_functional.py

raw symbols SCIP
1 """
2 Plain Python definition of the builtin functions oriented towards
3 functional programming.
4 """
5
6 # ____________________________________________________________
7 def sorted(iterable, /, *, key=None, reverse=False):
8 "sorted(iterable, key=None, reverse=False) --> new sorted list"
9 sorted_lst = list(iterable)
10 sorted_lst.sort(key=key, reverse=reverse)
11 return sorted_lst
12
13 def any(seq):
14 """any(iterable) -> bool
15
16 Return True if bool(x) is True for any x in the iterable."""
17 for x in seq:
18 if x:
19 return True
20 return False
21
22 def all(seq):
23 """all(iterable) -> bool
24
25 Return True if bool(x) is True for all values x in the iterable."""
26 for x in seq:
27 if not x:
28 return False
29 return True
30
31 def sum(sequence, start=0):
32 """sum(sequence[, start]) -> value
33
34 Returns the sum of a sequence of numbers (NOT strings) plus the value
35 of parameter 'start' (which defaults to 0). When the sequence is
36 empty, returns start."""
37 if isinstance(start, str):
38 raise TypeError("sum() can't sum strings [use ''.join(seq) instead]")
39 if isinstance(start, bytes):
40 raise TypeError("sum() can't sum bytes [use b''.join(seq) instead]")
41 if isinstance(start, bytearray):
42 raise TypeError("sum() can't sum bytearray [use b''.join(seq) instead]")
43
44 # Avoiding isinstance here, since subclasses can override `+`
45 if type(start) is list:
46 return _list_sum(sequence, start)
47
48 if type(start) is tuple:
49 return _tuple_sum(sequence, start)
50
51 return _regular_sum(sequence, start)
52
53
54 def _regular_sum(sequence, start):
55 # Default implementation for sum (no specialization)
56 last = start
57 for x in sequence:
58 # Very intentionally *not* +=, that would have different semantics if
59 # start was a mutable type, such as a list
60 last = last + x
61 return last
62
63
64 def _list_sum(sequence, start):
65 # Specialization avoiding quadratic complexity for lists
66 iterator = iter(sequence)
67
68 try:
69 first = next(iterator)
70 except StopIteration:
71 return start
72
73 if type(first) is not list:
74 return _regular_sum(iterator, start + first)
75
76 last = start + first
77 for item in iterator:
78 if type(item) is list:
79 last += item
80 else:
81 # Non-trivial sum. Use _regular_sum.
82 return _regular_sum(iterator, last + item)
83
84 return last
85
86
87 def _tuple_sum(sequence, start):
88 # Specialization avoiding quadratic complexity for tuples
89 iterator = iter(sequence)
90
91 try:
92 first = next(iterator)
93 except StopIteration:
94 return start
95
96 if type(first) is not tuple:
97 return _regular_sum(iterator, start + first)
98
99 last = list(start)
100 last.extend(first)
101 for item in iterator:
102 if type(item) is tuple:
103 last.extend(item)
104 else:
105 # Non-trivial sum. Cast back to tuple and use regular_sum.
106 return _regular_sum(iterator, tuple(last) + item)
107
108 return tuple(last)
109
110 def filter(func, seq):
111 """filter(function or None, sequence) -> list, tuple, or string
112
113 Return those items of sequence for which function(item) is true. If
114 function is None, return the items that are true. If sequence is a tuple
115 or string, return the same type, else return a list."""
116 if func is None:
117 func = bool
118 return _filter(func, iter(seq))
119
120 def _filter(func, iterator):
121 for item in iterator:
122 if func(item):
123 yield item