pythonic-fp.containers 2.0.0__py3-none-any.whl → 3.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pythonic_fp/containers/__init__.py +13 -1
- pythonic_fp/containers/functional_tuple.py +56 -67
- pythonic_fp/containers/functional_tuple.pyi +58 -0
- pythonic_fp/containers/immutable_list.py +48 -56
- pythonic_fp/containers/immutable_list.pyi +72 -0
- pythonic_fp/containers/queues/__init__.py +31 -0
- pythonic_fp/containers/queues/de.py +178 -0
- pythonic_fp/containers/queues/de.pyi +45 -0
- pythonic_fp/containers/queues/fifo.py +143 -0
- pythonic_fp/containers/queues/fifo.pyi +44 -0
- pythonic_fp/containers/queues/lifo.py +134 -0
- pythonic_fp/containers/queues/lifo.pyi +40 -0
- pythonic_fp/containers/queues/py.typed +0 -0
- pythonic_fp_containers-3.0.1.dist-info/METADATA +62 -0
- pythonic_fp_containers-3.0.1.dist-info/RECORD +18 -0
- pythonic_fp/containers/box.py +0 -159
- pythonic_fp/containers/maybe.py +0 -226
- pythonic_fp/containers/xor.py +0 -314
- pythonic_fp_containers-2.0.0.dist-info/METADATA +0 -50
- pythonic_fp_containers-2.0.0.dist-info/RECORD +0 -11
- {pythonic_fp_containers-2.0.0.dist-info → pythonic_fp_containers-3.0.1.dist-info}/WHEEL +0 -0
- {pythonic_fp_containers-2.0.0.dist-info → pythonic_fp_containers-3.0.1.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# Copyright 2023-2025 Geoffrey R. Scheller
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""Stateful Double-Ended (DE) Queue data structure.
|
|
16
|
+
|
|
17
|
+
- O(1) pops each end
|
|
18
|
+
- O(1) amortized pushes each end
|
|
19
|
+
- O(1) length determination
|
|
20
|
+
- will automatically increase storage capacity when needed
|
|
21
|
+
- in a Boolean context, true if not empty, false if empty
|
|
22
|
+
- neither indexable nor sliceable by design
|
|
23
|
+
|
|
24
|
+
"""
|
|
25
|
+
from collections.abc import Callable, Iterable, Iterator
|
|
26
|
+
from typing import TypeVar
|
|
27
|
+
from pythonic_fp.circulararray.auto import CA
|
|
28
|
+
from pythonic_fp.fptools.maybe import MayBe
|
|
29
|
+
|
|
30
|
+
__all__ = ['DEQueue', 'de_queue']
|
|
31
|
+
|
|
32
|
+
D = TypeVar('D')
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class DEQueue[D]:
|
|
36
|
+
|
|
37
|
+
__slots__ = ('_ca',)
|
|
38
|
+
|
|
39
|
+
def __init__(self, *dss: Iterable[D]) -> None:
|
|
40
|
+
"""Initial data in FIFO order, newest to oldest, as if
|
|
41
|
+
pushed on from the right side.
|
|
42
|
+
|
|
43
|
+
:param dss: takes up to one iterable
|
|
44
|
+
:raises ValueError: if more than 1 iterable is given
|
|
45
|
+
|
|
46
|
+
"""
|
|
47
|
+
if (size := len(dss)) > 1:
|
|
48
|
+
msg = f'DEQueue expects at most 1 argument, got {size}'
|
|
49
|
+
raise ValueError(msg)
|
|
50
|
+
self._ca = CA(dss[0]) if size == 1 else CA()
|
|
51
|
+
|
|
52
|
+
def __bool__(self) -> bool:
|
|
53
|
+
return len(self._ca) > 0
|
|
54
|
+
|
|
55
|
+
def __len__(self) -> int:
|
|
56
|
+
return len(self._ca)
|
|
57
|
+
|
|
58
|
+
def __eq__(self, other: object) -> bool:
|
|
59
|
+
if not isinstance(other, DEQueue):
|
|
60
|
+
return False
|
|
61
|
+
return self._ca == other._ca
|
|
62
|
+
|
|
63
|
+
def __iter__(self) -> Iterator[D]:
|
|
64
|
+
return iter(list(self._ca))
|
|
65
|
+
|
|
66
|
+
def __reversed__(self) -> Iterator[D]:
|
|
67
|
+
return reversed(list(self._ca))
|
|
68
|
+
|
|
69
|
+
def __repr__(self) -> str:
|
|
70
|
+
if len(self) == 0:
|
|
71
|
+
return 'DEQueue()'
|
|
72
|
+
return 'DEQueue(' + ', '.join(map(repr, self._ca)) + ')'
|
|
73
|
+
|
|
74
|
+
def __str__(self) -> str:
|
|
75
|
+
return '>< ' + ' | '.join(map(str, self)) + ' ><'
|
|
76
|
+
|
|
77
|
+
def copy(self) -> 'DEQueue[D]':
|
|
78
|
+
"""Shallow copy.
|
|
79
|
+
|
|
80
|
+
:return: shallow copy of the DEQueue
|
|
81
|
+
|
|
82
|
+
"""
|
|
83
|
+
return DEQueue(self._ca)
|
|
84
|
+
|
|
85
|
+
def pushl(self, *ds: D) -> None:
|
|
86
|
+
"""Push data onto left side of DEQueue.
|
|
87
|
+
|
|
88
|
+
:param ds: items to be pushed onto DEQueue from the left
|
|
89
|
+
|
|
90
|
+
"""
|
|
91
|
+
self._ca.pushl(*ds)
|
|
92
|
+
|
|
93
|
+
def pushr(self, *ds: D) -> None:
|
|
94
|
+
"""Push data onto right side of DEQueue.
|
|
95
|
+
|
|
96
|
+
:param ds: items to be pushed onto DEQueue from the right
|
|
97
|
+
|
|
98
|
+
"""
|
|
99
|
+
self._ca.pushr(*ds)
|
|
100
|
+
|
|
101
|
+
def popl(self) -> MayBe[D]:
|
|
102
|
+
"""Pop next data item from left side DEQueue, if it exists.
|
|
103
|
+
|
|
104
|
+
:return: MayBe of popped item if queue was not empty, empty MayBe otherwise
|
|
105
|
+
|
|
106
|
+
"""
|
|
107
|
+
if self._ca:
|
|
108
|
+
return MayBe(self._ca.popl())
|
|
109
|
+
return MayBe()
|
|
110
|
+
|
|
111
|
+
def popr(self) -> MayBe[D]:
|
|
112
|
+
"""Pop next item off right side DEQueue.
|
|
113
|
+
|
|
114
|
+
:return: MayBe of popped item if queue was not empty, empty MayBe otherwise
|
|
115
|
+
|
|
116
|
+
"""
|
|
117
|
+
if self._ca:
|
|
118
|
+
return MayBe(self._ca.popr())
|
|
119
|
+
return MayBe()
|
|
120
|
+
|
|
121
|
+
def peakl(self) -> MayBe[D]:
|
|
122
|
+
"""Peak at data on left side of DEQueue.
|
|
123
|
+
|
|
124
|
+
:return: MayBe of leftmost data if queue not empty, empty MayBe otherwise
|
|
125
|
+
|
|
126
|
+
"""
|
|
127
|
+
if self._ca:
|
|
128
|
+
return MayBe(self._ca[0])
|
|
129
|
+
return MayBe()
|
|
130
|
+
|
|
131
|
+
def peakr(self) -> MayBe[D]:
|
|
132
|
+
"""Peak at right side of DEQueue. Does not consume item.
|
|
133
|
+
|
|
134
|
+
:return: MayBe of rightmost data if queue not empty, empty MayBe otherwise
|
|
135
|
+
|
|
136
|
+
"""
|
|
137
|
+
if self._ca:
|
|
138
|
+
return MayBe(self._ca[-1])
|
|
139
|
+
return MayBe()
|
|
140
|
+
|
|
141
|
+
def foldl[L](self, f: Callable[[L, D], L], start: L | None = None) -> MayBe[L]:
|
|
142
|
+
"""Reduces DEQueue left to right.
|
|
143
|
+
|
|
144
|
+
:param f: reducing function, first argument is for accumulator
|
|
145
|
+
:param start: optional starting value
|
|
146
|
+
:return: MayBe of reduced value with f, empty MayBe if queue empty and no starting value given
|
|
147
|
+
|
|
148
|
+
"""
|
|
149
|
+
if start is None:
|
|
150
|
+
if not self._ca:
|
|
151
|
+
return MayBe()
|
|
152
|
+
return MayBe(self._ca.foldl(f, start))
|
|
153
|
+
|
|
154
|
+
def foldr[R](self, f: Callable[[D, R], R], start: R | None = None) -> MayBe[R]:
|
|
155
|
+
"""Reduces DEQueue right to left.
|
|
156
|
+
|
|
157
|
+
:param f: reducing function, second argument is for accumulator
|
|
158
|
+
:param start: optional starting value
|
|
159
|
+
:return: MayBe of reduced value with f, empty MayBe if queue empty and no starting value given
|
|
160
|
+
|
|
161
|
+
"""
|
|
162
|
+
if start is None:
|
|
163
|
+
if not self._ca:
|
|
164
|
+
return MayBe()
|
|
165
|
+
return MayBe(self._ca.foldr(f, start))
|
|
166
|
+
|
|
167
|
+
def map[U](self, f: Callable[[D], U]) -> 'DEQueue[U]':
|
|
168
|
+
"""Map f over the DEQueue left to right, retain original order.
|
|
169
|
+
|
|
170
|
+
:return: new DEQueue instance
|
|
171
|
+
|
|
172
|
+
"""
|
|
173
|
+
return DEQueue(map(f, self._ca))
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def de_queue[D](*ds: D) -> DEQueue[D]:
|
|
177
|
+
"""DEQueue factory function."""
|
|
178
|
+
return DEQueue(ds)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Copyright 2023-2025 Geoffrey R. Scheller
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
from collections.abc import Callable, Iterable, Iterator
|
|
16
|
+
from typing import TypeVar
|
|
17
|
+
from pythonic_fp.fptools.maybe import MayBe
|
|
18
|
+
|
|
19
|
+
__all__ = ['DEQueue', 'de_queue']
|
|
20
|
+
|
|
21
|
+
D = TypeVar('D')
|
|
22
|
+
|
|
23
|
+
class DEQueue[D]:
|
|
24
|
+
__slots__ = ('_ca',)
|
|
25
|
+
|
|
26
|
+
def __init__(self, *dss: Iterable[D]) -> None: ...
|
|
27
|
+
def __bool__(self) -> bool: ...
|
|
28
|
+
def __len__(self) -> int: ...
|
|
29
|
+
def __eq__(self, other: object) -> bool: ...
|
|
30
|
+
def __iter__(self) -> Iterator[D]: ...
|
|
31
|
+
def __reversed__(self) -> Iterator[D]: ...
|
|
32
|
+
def __repr__(self) -> str: ...
|
|
33
|
+
def __str__(self) -> str: ...
|
|
34
|
+
def copy(self) -> DEQueue[D]: ...
|
|
35
|
+
def pushl(self, *ds: D) -> None: ...
|
|
36
|
+
def pushr(self, *ds: D) -> None: ...
|
|
37
|
+
def popl(self) -> MayBe[D]: ...
|
|
38
|
+
def popr(self) -> MayBe[D]: ...
|
|
39
|
+
def peakl(self) -> MayBe[D]: ...
|
|
40
|
+
def peakr(self) -> MayBe[D]: ...
|
|
41
|
+
def foldl[L](self, f: Callable[[L, D], L], start: L | None = None) -> MayBe[L]: ...
|
|
42
|
+
def foldr[R](self, f: Callable[[D, R], R], start: R | None = None) -> MayBe[R]: ...
|
|
43
|
+
def map[U](self, f: Callable[[D], U]) -> DEQueue[U]: ...
|
|
44
|
+
|
|
45
|
+
def de_queue[D](*ds: D) -> DEQueue[D]: ...
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# Copyright 2023-2024 Geoffrey R. Scheller
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""Stateful First-In-First-Out (FIFO) Queue data structure.
|
|
16
|
+
|
|
17
|
+
- O(1) pops
|
|
18
|
+
- O(1) amortized pushes
|
|
19
|
+
- in a Boolean context, true if not empty, false if empty
|
|
20
|
+
- will automatically increase storage capacity when needed
|
|
21
|
+
- neither indexable nor sliceable by design
|
|
22
|
+
- O(1) length determination
|
|
23
|
+
|
|
24
|
+
"""
|
|
25
|
+
from collections.abc import Callable, Iterable, Iterator
|
|
26
|
+
from typing import TypeVar
|
|
27
|
+
from pythonic_fp.circulararray.auto import CA
|
|
28
|
+
from pythonic_fp.fptools.maybe import MayBe
|
|
29
|
+
|
|
30
|
+
__all__ = ['FIFOQueue', 'fifo_queue']
|
|
31
|
+
|
|
32
|
+
D = TypeVar('D')
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class FIFOQueue[D]:
|
|
36
|
+
|
|
37
|
+
__slots__ = ('_ca',)
|
|
38
|
+
|
|
39
|
+
def __init__(self, *dss: Iterable[D]) -> None:
|
|
40
|
+
"""Initial data in natural FIFO order, newest to oldest.
|
|
41
|
+
|
|
42
|
+
:param dss: takes up to one iterable
|
|
43
|
+
:raises ValueError: if more than 1 iterable is given
|
|
44
|
+
|
|
45
|
+
"""
|
|
46
|
+
if (size := len(dss)) > 1:
|
|
47
|
+
msg = f'FIFOQueue expects at most 1 iterable argument, got {size}'
|
|
48
|
+
raise ValueError(msg)
|
|
49
|
+
self._ca = CA(dss[0]) if size == 1 else CA()
|
|
50
|
+
|
|
51
|
+
def __bool__(self) -> bool:
|
|
52
|
+
return len(self._ca) > 0
|
|
53
|
+
|
|
54
|
+
def __len__(self) -> int:
|
|
55
|
+
return len(self._ca)
|
|
56
|
+
|
|
57
|
+
def __eq__(self, other: object) -> bool:
|
|
58
|
+
if not isinstance(other, FIFOQueue):
|
|
59
|
+
return False
|
|
60
|
+
return self._ca == other._ca
|
|
61
|
+
|
|
62
|
+
def __iter__(self) -> Iterator[D]:
|
|
63
|
+
return iter(list(self._ca))
|
|
64
|
+
|
|
65
|
+
def __repr__(self) -> str:
|
|
66
|
+
if len(self) == 0:
|
|
67
|
+
return 'FIFOQueue()'
|
|
68
|
+
return 'FIFOQueue(' + ', '.join(map(repr, self._ca)) + ')'
|
|
69
|
+
|
|
70
|
+
def __str__(self) -> str:
|
|
71
|
+
return '<< ' + ' < '.join(map(str, self)) + ' <<'
|
|
72
|
+
|
|
73
|
+
def copy(self) -> 'FIFOQueue[D]':
|
|
74
|
+
"""Shallow copy.
|
|
75
|
+
|
|
76
|
+
:return: shallow copy of the FIFOQueue
|
|
77
|
+
|
|
78
|
+
"""
|
|
79
|
+
return FIFOQueue(self._ca)
|
|
80
|
+
|
|
81
|
+
def push(self, *ds: D) -> None:
|
|
82
|
+
"""Push data onto FIFOQueue.
|
|
83
|
+
|
|
84
|
+
:param ds: data items to be pushed onto FIFOQueue
|
|
85
|
+
|
|
86
|
+
"""
|
|
87
|
+
self._ca.pushr(*ds)
|
|
88
|
+
|
|
89
|
+
def pop(self) -> MayBe[D]:
|
|
90
|
+
"""Pop oldest data item off of FIFOQueue.
|
|
91
|
+
|
|
92
|
+
:return: MayBe of popped data item if queue was not empty, empty MayBe otherwise
|
|
93
|
+
|
|
94
|
+
"""
|
|
95
|
+
if self._ca:
|
|
96
|
+
return MayBe(self._ca.popl())
|
|
97
|
+
return MayBe()
|
|
98
|
+
|
|
99
|
+
def peak_last_in(self) -> MayBe[D]:
|
|
100
|
+
"""Peak at newest data item on queue.
|
|
101
|
+
|
|
102
|
+
:return: MayBe of newest data item on queue, empty MayBe if queue empty
|
|
103
|
+
|
|
104
|
+
"""
|
|
105
|
+
if self._ca:
|
|
106
|
+
return MayBe(self._ca[-1])
|
|
107
|
+
return MayBe()
|
|
108
|
+
|
|
109
|
+
def peak_next_out(self) -> MayBe[D]:
|
|
110
|
+
"""Peak at oldest data item on queue.
|
|
111
|
+
|
|
112
|
+
:return: MayBe of oldest data item on queue, empty MayBe if queue empty
|
|
113
|
+
|
|
114
|
+
"""
|
|
115
|
+
if self._ca:
|
|
116
|
+
return MayBe(self._ca[0])
|
|
117
|
+
return MayBe()
|
|
118
|
+
|
|
119
|
+
def fold[T](self, f: Callable[[T, D], T], start: T | None = None) -> MayBe[T]:
|
|
120
|
+
"""Reduces FIFOQueue in natural FIFO Order, oldest to newest.
|
|
121
|
+
|
|
122
|
+
:param f: reducing function, first argument is for accumulator
|
|
123
|
+
:param start: optional starting value
|
|
124
|
+
:return: MayBe of reduced value with f, empty MayBe if queue empty and no starting value given
|
|
125
|
+
|
|
126
|
+
"""
|
|
127
|
+
if start is None:
|
|
128
|
+
if not self._ca:
|
|
129
|
+
return MayBe()
|
|
130
|
+
return MayBe(self._ca.foldl(f, start))
|
|
131
|
+
|
|
132
|
+
def map[U](self, f: Callable[[D], U]) -> 'FIFOQueue[U]':
|
|
133
|
+
"""Map f over the FIFOQueue, retain original order.
|
|
134
|
+
|
|
135
|
+
:return: new FIFOQueue instance
|
|
136
|
+
|
|
137
|
+
"""
|
|
138
|
+
return FIFOQueue(map(f, self._ca))
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def fifo_queue[D](*ds: D) -> FIFOQueue[D]:
|
|
142
|
+
"""FIFOQueue factory function."""
|
|
143
|
+
return FIFOQueue(ds)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Copyright 2023-2024 Geoffrey R. Scheller
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
from collections.abc import Callable, Iterable, Iterator
|
|
16
|
+
from typing import TypeVar
|
|
17
|
+
from pythonic_fp.circulararray.auto import CA
|
|
18
|
+
from pythonic_fp.fptools.maybe import MayBe
|
|
19
|
+
|
|
20
|
+
__all__ = ['FIFOQueue', 'fifo_queue']
|
|
21
|
+
|
|
22
|
+
D = TypeVar('D')
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class FIFOQueue[D]:
|
|
26
|
+
|
|
27
|
+
__slots__ = ('_ca',)
|
|
28
|
+
|
|
29
|
+
def __init__(self, *dss: Iterable[D]) -> None: ...
|
|
30
|
+
def __bool__(self) -> bool: ...
|
|
31
|
+
def __len__(self) -> int: ...
|
|
32
|
+
def __eq__(self, other: object) -> bool: ...
|
|
33
|
+
def __iter__(self) -> Iterator[D]: ...
|
|
34
|
+
def __repr__(self) -> str: ...
|
|
35
|
+
def __str__(self) -> str: ...
|
|
36
|
+
def copy(self) -> FIFOQueue[D]: ...
|
|
37
|
+
def push(self, *ds: D) -> None: ...
|
|
38
|
+
def pop(self) -> MayBe[D]: ...
|
|
39
|
+
def peak_last_in(self) -> MayBe[D]: ...
|
|
40
|
+
def peak_next_out(self) -> MayBe[D]: ...
|
|
41
|
+
def fold[T](self, f: Callable[[T, D], T], start: T | None = None) -> MayBe[T]: ...
|
|
42
|
+
def map[U](self, f: Callable[[D], U]) -> FIFOQueue[U]: ...
|
|
43
|
+
|
|
44
|
+
def fifo_queue[D](*ds: D) -> FIFOQueue[D]: ...
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# Copyright 2023-2024 Geoffrey R. Scheller
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""Stateful Last-In-First-Out (LIFO) Queue data structure.
|
|
16
|
+
|
|
17
|
+
- O(1) pops
|
|
18
|
+
- O(1) amortized pushes
|
|
19
|
+
- in a Boolean context, true if not empty, false if empty
|
|
20
|
+
- will automatically increase storage capacity when needed
|
|
21
|
+
- neither indexable nor sliceable by design
|
|
22
|
+
- O(1) length determination
|
|
23
|
+
|
|
24
|
+
"""
|
|
25
|
+
from collections.abc import Callable, Iterable, Iterator
|
|
26
|
+
from typing import TypeVar
|
|
27
|
+
from pythonic_fp.circulararray.auto import CA
|
|
28
|
+
from pythonic_fp.fptools.function import swap
|
|
29
|
+
from pythonic_fp.fptools.maybe import MayBe
|
|
30
|
+
|
|
31
|
+
__all__ = ['LIFOQueue', 'lifo_queue']
|
|
32
|
+
|
|
33
|
+
D = TypeVar('D')
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class LIFOQueue[D]:
|
|
37
|
+
|
|
38
|
+
__slots__ = ('_ca',)
|
|
39
|
+
|
|
40
|
+
def __init__(self, *dss: Iterable[D]) -> None:
|
|
41
|
+
"""Initial data in natural LIFO order, oldest to newest.
|
|
42
|
+
|
|
43
|
+
:param dss: takes up to one iterable
|
|
44
|
+
:raises ValueError: if more than 1 iterable is given
|
|
45
|
+
|
|
46
|
+
"""
|
|
47
|
+
if (size := len(dss)) > 1:
|
|
48
|
+
msg = f'LIFOQueue expects at most 1 iterable argument, got {size}'
|
|
49
|
+
raise ValueError(msg)
|
|
50
|
+
self._ca = CA(dss[0]) if size == 1 else CA()
|
|
51
|
+
|
|
52
|
+
def __bool__(self) -> bool:
|
|
53
|
+
return len(self._ca) > 0
|
|
54
|
+
|
|
55
|
+
def __len__(self) -> int:
|
|
56
|
+
return len(self._ca)
|
|
57
|
+
|
|
58
|
+
def __eq__(self, other: object) -> bool:
|
|
59
|
+
if not isinstance(other, LIFOQueue):
|
|
60
|
+
return False
|
|
61
|
+
return self._ca == other._ca
|
|
62
|
+
|
|
63
|
+
def __iter__(self) -> Iterator[D]:
|
|
64
|
+
return reversed(list(self._ca))
|
|
65
|
+
|
|
66
|
+
def __repr__(self) -> str:
|
|
67
|
+
if len(self) == 0:
|
|
68
|
+
return 'LIFOQueue()'
|
|
69
|
+
return 'LIFOQueue(' + ', '.join(map(repr, self._ca)) + ')'
|
|
70
|
+
|
|
71
|
+
def __str__(self) -> str:
|
|
72
|
+
return '|| ' + ' > '.join(map(str, self)) + ' ><'
|
|
73
|
+
|
|
74
|
+
def copy(self) -> 'LIFOQueue[D]':
|
|
75
|
+
"""Shallow copy.
|
|
76
|
+
|
|
77
|
+
:return: shallow copy of the LIFOQueue
|
|
78
|
+
|
|
79
|
+
"""
|
|
80
|
+
return LIFOQueue(reversed(self._ca))
|
|
81
|
+
|
|
82
|
+
def push(self, *ds: D) -> None:
|
|
83
|
+
"""Push data onto LIFOQueue.
|
|
84
|
+
|
|
85
|
+
:param ds: data items to be pushed onto LIFOQueue
|
|
86
|
+
|
|
87
|
+
"""
|
|
88
|
+
self._ca.pushr(*ds)
|
|
89
|
+
|
|
90
|
+
def pop(self) -> MayBe[D]:
|
|
91
|
+
"""Pop newest data item off of LIFOQueue.
|
|
92
|
+
|
|
93
|
+
:return: MayBe of popped data item if queue was not empty, empty MayBe otherwise
|
|
94
|
+
|
|
95
|
+
"""
|
|
96
|
+
if self._ca:
|
|
97
|
+
return MayBe(self._ca.popr())
|
|
98
|
+
return MayBe()
|
|
99
|
+
|
|
100
|
+
def peak(self) -> MayBe[D]:
|
|
101
|
+
"""Peak at newest data item on queue.
|
|
102
|
+
|
|
103
|
+
:return: MayBe of newest data item on queue, empty MayBe if queue empty
|
|
104
|
+
|
|
105
|
+
"""
|
|
106
|
+
if self._ca:
|
|
107
|
+
return MayBe(self._ca[-1])
|
|
108
|
+
return MayBe()
|
|
109
|
+
|
|
110
|
+
def fold[T](self, f: Callable[[T, D], T], start: T | None = None) -> MayBe[T]:
|
|
111
|
+
"""Reduces LIFOQUEUE in natural LIFO Order, newest to oldest.
|
|
112
|
+
|
|
113
|
+
:param f: reducing function, first argument is for accumulator
|
|
114
|
+
:param start: optional starting value
|
|
115
|
+
:return: MayBe of reduced value with f, empty MayBe if queue empty and no starting value given
|
|
116
|
+
|
|
117
|
+
"""
|
|
118
|
+
if start is None:
|
|
119
|
+
if not self._ca:
|
|
120
|
+
return MayBe()
|
|
121
|
+
return MayBe(self._ca.foldr(swap(f), start))
|
|
122
|
+
|
|
123
|
+
def map[U](self, f: Callable[[D], U]) -> 'LIFOQueue[U]':
|
|
124
|
+
"""Map f over the LIFOQueue, retain original order.
|
|
125
|
+
|
|
126
|
+
:return: new LIFOQueue instance
|
|
127
|
+
|
|
128
|
+
"""
|
|
129
|
+
return LIFOQueue(reversed(CA(map(f, reversed(self._ca)))))
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def lifo_queue[D](*ds: D) -> LIFOQueue[D]:
|
|
133
|
+
"""LIFOQueue factory function."""
|
|
134
|
+
return LIFOQueue(ds)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Copyright 2023-2024 Geoffrey R. Scheller
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
from collections.abc import Callable, Iterable, Iterator
|
|
16
|
+
from typing import TypeVar
|
|
17
|
+
from pythonic_fp.fptools.maybe import MayBe
|
|
18
|
+
|
|
19
|
+
__all__ = ['LIFOQueue', 'lifo_queue']
|
|
20
|
+
|
|
21
|
+
D = TypeVar('D')
|
|
22
|
+
|
|
23
|
+
class LIFOQueue[D]:
|
|
24
|
+
__slots__ = ('_ca',)
|
|
25
|
+
|
|
26
|
+
def __init__(self, *dss: Iterable[D]) -> None: ...
|
|
27
|
+
def __bool__(self) -> bool: ...
|
|
28
|
+
def __len__(self) -> int: ...
|
|
29
|
+
def __eq__(self, other: object) -> bool: ...
|
|
30
|
+
def __iter__(self) -> Iterator[D]: ...
|
|
31
|
+
def __repr__(self) -> str: ...
|
|
32
|
+
def __str__(self) -> str: ...
|
|
33
|
+
def copy(self) -> LIFOQueue[D]: ...
|
|
34
|
+
def push(self, *ds: D) -> None: ...
|
|
35
|
+
def pop(self) -> MayBe[D]: ...
|
|
36
|
+
def peak(self) -> MayBe[D]: ...
|
|
37
|
+
def fold[T](self, f: Callable[[T, D], T], start: T | None = None) -> MayBe[T]: ...
|
|
38
|
+
def map[U](self, f: Callable[[D], U]) -> LIFOQueue[U]: ...
|
|
39
|
+
|
|
40
|
+
def lifo_queue[D](*ds: D) -> LIFOQueue[D]: ...
|
|
File without changes
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pythonic-fp-containers
|
|
3
|
+
Version: 3.0.1
|
|
4
|
+
Summary: Package of container like data structures.
|
|
5
|
+
Keywords: boxes,dequeue,dqueue,fifo,functional_tuple,immutable_list,lifo,queue
|
|
6
|
+
Author-email: "Geoffrey R. Scheller" <geoffrey@scheller.com>
|
|
7
|
+
Requires-Python: >=3.12
|
|
8
|
+
Description-Content-Type: text/x-rst
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Framework :: Pytest
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Typing :: Typed
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: pythonic-fp-circulararray>=5.3.2
|
|
18
|
+
Requires-Dist: pythonic-fp-fptools>=5.1.0
|
|
19
|
+
Requires-Dist: pythonic-fp-iterables>=5.1.0
|
|
20
|
+
Requires-Dist: pytest>=8.4.1 ; extra == "test"
|
|
21
|
+
Requires-Dist: pythonic-fp-splitends>=1.0.1 ; extra == "test"
|
|
22
|
+
Project-URL: Changelog, https://github.com/grscheller/pythonic-fp-containers/blob/main/CHANGELOG.rst
|
|
23
|
+
Project-URL: Documentation, https://grscheller.github.io/pythonic-fp/containers/development/build/html/releases.html
|
|
24
|
+
Project-URL: Homepage, https://github.com/grscheller/pythonic-fp/blob/main/README.md
|
|
25
|
+
Project-URL: Source, https://github.com/grscheller/pythonic-fp-containers
|
|
26
|
+
Provides-Extra: test
|
|
27
|
+
|
|
28
|
+
Pythonic FP - Containers
|
|
29
|
+
========================
|
|
30
|
+
|
|
31
|
+
PyPI project
|
|
32
|
+
`pythonic-fp.containers <https://pypi.org/project/pythonic-fp.containers>`_.
|
|
33
|
+
|
|
34
|
+
Python package of container like data structures.
|
|
35
|
+
|
|
36
|
+
+------------------+------------------------------------------------------+
|
|
37
|
+
| Description | Module |
|
|
38
|
+
+==================+======================================================+
|
|
39
|
+
| Functional tuple | *module* ``pythonic_fp.containers.functional_tuple`` |
|
|
40
|
+
+------------------+------------------------------------------------------+
|
|
41
|
+
| Immutable list | *module* ``pythonic_fp.containers.immutable_list`` |
|
|
42
|
+
+------------------+------------------------------------------------------+
|
|
43
|
+
|
|
44
|
+
**DEPRECATION WARNING:** ``pythonic_fp.fptools.box`` as been deprecated.
|
|
45
|
+
Use ``pythonic_fp.box`` instead.
|
|
46
|
+
|
|
47
|
+
This PyPI project is part of of the grscheller
|
|
48
|
+
`pythonic-fp namespace projects <https://github.com/grscheller/pythonic-fp/blob/main/README.md>`_.
|
|
49
|
+
|
|
50
|
+
Documentation
|
|
51
|
+
-------------
|
|
52
|
+
|
|
53
|
+
Documentation for this project is hosted on
|
|
54
|
+
`GitHub Pages
|
|
55
|
+
<https://grscheller.github.io/pythonic-fp/containers/development/build/html>`_.
|
|
56
|
+
|
|
57
|
+
Copyright and License
|
|
58
|
+
---------------------
|
|
59
|
+
|
|
60
|
+
Copyright (c) 2023-2025 Geoffrey R. Scheller. Licensed under the Apache
|
|
61
|
+
License, Version 2.0. See the LICENSE file for details.
|
|
62
|
+
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
pythonic_fp/containers/__init__.py,sha256=D8meKk6jpSNZMgWlM_2tuoxffLgyKHeJwHkRwHJd2C8,1524
|
|
2
|
+
pythonic_fp/containers/functional_tuple.py,sha256=k__UAhGALm-gHR8TC9DzBuASlPXeTeFYESc53JLHjl4,5636
|
|
3
|
+
pythonic_fp/containers/functional_tuple.pyi,sha256=Ca3nLJVZqnicsJ2AJpp7sHoqYFOD4d15a5lO5xBOdYA,2030
|
|
4
|
+
pythonic_fp/containers/immutable_list.py,sha256=qxXffc1uY3x9lHGRR4wt_0xJHAUNiT6Mb4Mupj8oF4U,6573
|
|
5
|
+
pythonic_fp/containers/immutable_list.pyi,sha256=48WFxgkyB-JBD2qH3WIZjWfMArxkTWRs731rv87SRzA,2349
|
|
6
|
+
pythonic_fp/containers/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
pythonic_fp/containers/queues/__init__.py,sha256=GJdzodYt32pADih2UHOiR9U4jfZ6SQSw6PsCYZ2r1wg,1459
|
|
8
|
+
pythonic_fp/containers/queues/de.py,sha256=s7R-_SqF6AcWjfzOkqCfG816aZIa3Te0TVKs7GNOUVQ,5256
|
|
9
|
+
pythonic_fp/containers/queues/de.pyi,sha256=BmjhyOiKk69Zh5et0vw5Btm-z5yRc1KDq9qH84O7L-A,1696
|
|
10
|
+
pythonic_fp/containers/queues/fifo.py,sha256=4Pc75fSxmLScb4dQQ6qUdLIg_kvPU8sBxB8kudQQB48,4189
|
|
11
|
+
pythonic_fp/containers/queues/fifo.pyi,sha256=FCXUvreC9C_4QhdnnT9o7K2cvgCcxTGp9IXv9p7hc2o,1558
|
|
12
|
+
pythonic_fp/containers/queues/lifo.py,sha256=zjz9HEjRTmY3PkXHPcpdwTSvtbha1AGPnEa1Ez31r6g,3999
|
|
13
|
+
pythonic_fp/containers/queues/lifo.pyi,sha256=EBS5tGedLZCHQK35xNzgOz_KJJGeMQR0KvlXQLiFbLU,1457
|
|
14
|
+
pythonic_fp/containers/queues/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
15
|
+
pythonic_fp_containers-3.0.1.dist-info/licenses/LICENSE,sha256=IJMkVWQUVy-SonK6SMaMotDQ6Zb-lVi2bUTiGp1GUO8,10774
|
|
16
|
+
pythonic_fp_containers-3.0.1.dist-info/WHEEL,sha256=G2gURzTEtmeR8nrdXUJfNiB3VYVxigPQ-bEQujpNiNs,82
|
|
17
|
+
pythonic_fp_containers-3.0.1.dist-info/METADATA,sha256=Qa1VF6iMX1qoL032CD-AvHegUpGzr1bAxO-lXwpT_0o,2613
|
|
18
|
+
pythonic_fp_containers-3.0.1.dist-info/RECORD,,
|