pythonic-fp-queues 5.1.0__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/queues/__init__.py +33 -0
- pythonic_fp/queues/de.py +185 -0
- pythonic_fp/queues/fifo.py +143 -0
- pythonic_fp/queues/lifo.py +134 -0
- pythonic_fp/queues/py.typed +0 -0
- pythonic_fp_queues-5.1.0.dist-info/METADATA +60 -0
- pythonic_fp_queues-5.1.0.dist-info/RECORD +9 -0
- pythonic_fp_queues-5.1.0.dist-info/WHEEL +4 -0
- pythonic_fp_queues-5.1.0.dist-info/licenses/LICENSE +190 -0
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
"""
|
|
16
|
+
Queues
|
|
17
|
+
======
|
|
18
|
+
|
|
19
|
+
+-------------------------+-----------+--------------------------+
|
|
20
|
+
| module | class | name |
|
|
21
|
+
+=========================+===========+==========================+
|
|
22
|
+
| pythonic_fp.queues.fifo | FIFOQueue | First-In-First-Out Queue |
|
|
23
|
+
+-------------------------+-----------+--------------------------+
|
|
24
|
+
| pythonic_fp.queues.lifo | LIFOQueue | Last-In-First-Out Queue |
|
|
25
|
+
+-------------------------+-----------+--------------------------+
|
|
26
|
+
| pythonic_fp.queues.de | DEQueue | Double-Ended Queue |
|
|
27
|
+
+-------------------------+-----------+--------------------------+
|
|
28
|
+
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
__author__ = 'Geoffrey R. Scheller'
|
|
32
|
+
__copyright__ = 'Copyright (c) 2023-2025 Geoffrey R. Scheller'
|
|
33
|
+
__license__ = 'Apache License 2.0'
|
pythonic_fp/queues/de.py
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
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 cast, overload
|
|
17
|
+
from pythonic_fp.circulararray.auto import CA
|
|
18
|
+
from pythonic_fp.fptools.maybe import MayBe
|
|
19
|
+
|
|
20
|
+
__all__ = ['DEQueue', 'de_queue']
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class DEQueue[D]:
|
|
24
|
+
"""Stateful Double-Ended (DE) Queue data structure.
|
|
25
|
+
|
|
26
|
+
- O(1) pops each end
|
|
27
|
+
- O(1) amortized pushes each end
|
|
28
|
+
- O(1) length determination
|
|
29
|
+
- in a Boolean context, truthy if not empty, falsy if empty
|
|
30
|
+
- will automatically increase storage capacity when needed
|
|
31
|
+
- neither indexable nor sliceable by design
|
|
32
|
+
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
__slots__ = ('_ca',)
|
|
36
|
+
|
|
37
|
+
def __init__(self, *dss: Iterable[D]) -> None:
|
|
38
|
+
"""
|
|
39
|
+
:param dss: "Optionally" takes a single iterable to initialize data in FIFO order.
|
|
40
|
+
:raises TypeError: When ``dss[0]`` not Iterable.
|
|
41
|
+
:raises ValueError: If more than 1 iterable is given.
|
|
42
|
+
"""
|
|
43
|
+
if (size := len(dss)) > 1:
|
|
44
|
+
msg = f'DEQueue expects at most 1 argument, got {size}'
|
|
45
|
+
raise ValueError(msg)
|
|
46
|
+
self._ca = CA(dss[0]) if size == 1 else CA()
|
|
47
|
+
|
|
48
|
+
def __bool__(self) -> bool:
|
|
49
|
+
return len(self._ca) > 0
|
|
50
|
+
|
|
51
|
+
def __len__(self) -> int:
|
|
52
|
+
return len(self._ca)
|
|
53
|
+
|
|
54
|
+
def __eq__(self, other: object) -> bool:
|
|
55
|
+
if not isinstance(other, DEQueue):
|
|
56
|
+
return False
|
|
57
|
+
return self._ca == other._ca
|
|
58
|
+
|
|
59
|
+
def __iter__(self) -> Iterator[D]:
|
|
60
|
+
return iter(list(self._ca))
|
|
61
|
+
|
|
62
|
+
def __reversed__(self) -> Iterator[D]:
|
|
63
|
+
return reversed(list(self._ca))
|
|
64
|
+
|
|
65
|
+
def __repr__(self) -> str:
|
|
66
|
+
if len(self) == 0:
|
|
67
|
+
return 'DEQueue()'
|
|
68
|
+
return 'DEQueue(' + ', '.join(map(repr, self._ca)) + ')'
|
|
69
|
+
|
|
70
|
+
def __str__(self) -> str:
|
|
71
|
+
return '>< ' + ' | '.join(map(str, self)) + ' ><'
|
|
72
|
+
|
|
73
|
+
def copy(self) -> 'DEQueue[D]':
|
|
74
|
+
"""Shallow copy.
|
|
75
|
+
|
|
76
|
+
:returns: Shallow copy of the DEQueue.
|
|
77
|
+
"""
|
|
78
|
+
return DEQueue(self._ca)
|
|
79
|
+
|
|
80
|
+
def pushl(self, *ds: D) -> None:
|
|
81
|
+
"""Push data onto left side of DEQueue.
|
|
82
|
+
|
|
83
|
+
:param ds: Items to be pushed onto DEQueue from the left.
|
|
84
|
+
"""
|
|
85
|
+
self._ca.pushl(*ds)
|
|
86
|
+
|
|
87
|
+
def pushr(self, *ds: D) -> None:
|
|
88
|
+
"""Push data onto right side of DEQueue.
|
|
89
|
+
|
|
90
|
+
:param ds: Items to be pushed onto DEQueue from the right.
|
|
91
|
+
"""
|
|
92
|
+
self._ca.pushr(*ds)
|
|
93
|
+
|
|
94
|
+
def popl(self) -> MayBe[D]:
|
|
95
|
+
"""Pop next item from left side DEQueue, if it exists.
|
|
96
|
+
|
|
97
|
+
:returns: MayBe of popped item if queue was not empty, empty MayBe otherwise.
|
|
98
|
+
"""
|
|
99
|
+
if self._ca:
|
|
100
|
+
return MayBe(self._ca.popl())
|
|
101
|
+
return MayBe()
|
|
102
|
+
|
|
103
|
+
def popr(self) -> MayBe[D]:
|
|
104
|
+
"""Pop next item off right side DEQueue, if it exists.
|
|
105
|
+
|
|
106
|
+
:returns: MayBe of popped item if queue was not empty, empty MayBe otherwise.
|
|
107
|
+
"""
|
|
108
|
+
if self._ca:
|
|
109
|
+
return MayBe(self._ca.popr())
|
|
110
|
+
return MayBe()
|
|
111
|
+
|
|
112
|
+
def peakl(self) -> MayBe[D]:
|
|
113
|
+
"""Peak left side of DEQueue. Does not consume item.
|
|
114
|
+
|
|
115
|
+
:returns: MayBe of leftmost item if queue not empty, empty MayBe otherwise.
|
|
116
|
+
"""
|
|
117
|
+
if self._ca:
|
|
118
|
+
return MayBe(self._ca[0])
|
|
119
|
+
return MayBe()
|
|
120
|
+
|
|
121
|
+
def peakr(self) -> MayBe[D]:
|
|
122
|
+
"""Peak right side of DEQueue. Does not consume item.
|
|
123
|
+
|
|
124
|
+
:returns: MayBe of rightmost item if queue not empty, empty MayBe otherwise.
|
|
125
|
+
"""
|
|
126
|
+
if self._ca:
|
|
127
|
+
return MayBe(self._ca[-1])
|
|
128
|
+
return MayBe()
|
|
129
|
+
|
|
130
|
+
@overload
|
|
131
|
+
def foldl[L](self, f: Callable[[D, D], D]) -> MayBe[D]: ...
|
|
132
|
+
@overload
|
|
133
|
+
def foldl[L](self, f: Callable[[L, D], L], start: L) -> MayBe[L]: ...
|
|
134
|
+
|
|
135
|
+
def foldl[L](self, f: Callable[[L, D], L], start: L | None = None) -> MayBe[L]:
|
|
136
|
+
"""Reduces DEQueue left to right.
|
|
137
|
+
|
|
138
|
+
:param f: Reducing function, first argument is for accumulator.
|
|
139
|
+
:param start: Optional starting value.
|
|
140
|
+
:returns: MayBe of reduced value with f, empty MayBe if queue empty and no starting value given.
|
|
141
|
+
"""
|
|
142
|
+
if start is None:
|
|
143
|
+
if not self._ca:
|
|
144
|
+
return MayBe()
|
|
145
|
+
return MayBe(cast(L, self._ca.foldl(cast(Callable[[D, D], D], f)))) # L = D
|
|
146
|
+
return MayBe(self._ca.foldl(f, start))
|
|
147
|
+
|
|
148
|
+
@overload
|
|
149
|
+
def foldr[R](self, f: Callable[[D, D], D]) -> MayBe[D]: ...
|
|
150
|
+
@overload
|
|
151
|
+
def foldr[R](self, f: Callable[[D, R], R], start: R) -> MayBe[R]: ...
|
|
152
|
+
|
|
153
|
+
def foldr[R](self, f: Callable[[D, R], R], start: R | None = None) -> MayBe[R]:
|
|
154
|
+
"""Reduces DEQueue right to left.
|
|
155
|
+
|
|
156
|
+
:param f: Reducing function, second argument is for accumulator.
|
|
157
|
+
:param start: Optional starting value.
|
|
158
|
+
:returns: MayBe of reduced value with f, empty MayBe if queue empty and no starting value given.
|
|
159
|
+
"""
|
|
160
|
+
if start is None:
|
|
161
|
+
if not self._ca:
|
|
162
|
+
return MayBe()
|
|
163
|
+
return MayBe(cast(R, self._ca.foldr(cast(Callable[[D, D], D], f)))) # R = D
|
|
164
|
+
return MayBe(self._ca.foldr(f, start))
|
|
165
|
+
|
|
166
|
+
def map[U](self, f: Callable[[D], U]) -> 'DEQueue[U]':
|
|
167
|
+
"""Map left to right.
|
|
168
|
+
|
|
169
|
+
.. tip::
|
|
170
|
+
|
|
171
|
+
Order map done does not matter if ``f`` is pure.
|
|
172
|
+
|
|
173
|
+
:param f: Function to map over queue.
|
|
174
|
+
:returns: New DEQueue instance, retain original order.
|
|
175
|
+
"""
|
|
176
|
+
return DEQueue(map(f, self._ca))
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def de_queue[D](*ds: D) -> DEQueue[D]:
|
|
180
|
+
"""DEQueue factory function.
|
|
181
|
+
|
|
182
|
+
:param ds: Initial items as if pushed on from right to left.
|
|
183
|
+
:returns: DEQueue with items initialized in FIFO order.
|
|
184
|
+
"""
|
|
185
|
+
return DEQueue(ds)
|
|
@@ -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
|
+
from collections.abc import Callable, Iterable, Iterator
|
|
16
|
+
from typing import cast, overload
|
|
17
|
+
from pythonic_fp.circulararray.auto import CA
|
|
18
|
+
from pythonic_fp.fptools.maybe import MayBe
|
|
19
|
+
|
|
20
|
+
__all__ = ['FIFOQueue', 'fifo_queue']
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class FIFOQueue[D]:
|
|
24
|
+
"""Stateful First-In-First-Out (FIFO) Queue data structure.
|
|
25
|
+
|
|
26
|
+
- O(1) pops
|
|
27
|
+
- O(1) amortized pushes
|
|
28
|
+
- O(1) length determination
|
|
29
|
+
- in a Boolean context, truthy if not empty, falsy if empty
|
|
30
|
+
- will automatically increase storage capacity when needed
|
|
31
|
+
- neither indexable nor sliceable by design
|
|
32
|
+
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
__slots__ = ('_ca',)
|
|
36
|
+
|
|
37
|
+
def __init__(self, *dss: Iterable[D]) -> None:
|
|
38
|
+
"""
|
|
39
|
+
:param dss: Takes 1 or 0 iterables, initializes items in natural FIFO order.
|
|
40
|
+
:raises ValueError: If more than 1 iterable is given.
|
|
41
|
+
"""
|
|
42
|
+
if (size := len(dss)) > 1:
|
|
43
|
+
msg = f'FIFOQueue expects at most 1 iterable argument, got {size}'
|
|
44
|
+
raise ValueError(msg)
|
|
45
|
+
self._ca = CA(dss[0]) if size == 1 else CA()
|
|
46
|
+
|
|
47
|
+
def __bool__(self) -> bool:
|
|
48
|
+
return len(self._ca) > 0
|
|
49
|
+
|
|
50
|
+
def __len__(self) -> int:
|
|
51
|
+
return len(self._ca)
|
|
52
|
+
|
|
53
|
+
def __eq__(self, other: object) -> bool:
|
|
54
|
+
if not isinstance(other, FIFOQueue):
|
|
55
|
+
return False
|
|
56
|
+
return self._ca == other._ca
|
|
57
|
+
|
|
58
|
+
def __iter__(self) -> Iterator[D]:
|
|
59
|
+
return iter(list(self._ca))
|
|
60
|
+
|
|
61
|
+
def __repr__(self) -> str:
|
|
62
|
+
if len(self) == 0:
|
|
63
|
+
return 'FIFOQueue()'
|
|
64
|
+
return 'FIFOQueue(' + ', '.join(map(repr, self._ca)) + ')'
|
|
65
|
+
|
|
66
|
+
def __str__(self) -> str:
|
|
67
|
+
return '<< ' + ' < '.join(map(str, self)) + ' <<'
|
|
68
|
+
|
|
69
|
+
def copy(self) -> 'FIFOQueue[D]':
|
|
70
|
+
"""Shallow copy.
|
|
71
|
+
|
|
72
|
+
:returns: Shallow copy of the FIFOQueue.
|
|
73
|
+
"""
|
|
74
|
+
return FIFOQueue(self._ca)
|
|
75
|
+
|
|
76
|
+
def push(self, *ds: D) -> None:
|
|
77
|
+
"""Push items onto FIFOQueue.
|
|
78
|
+
|
|
79
|
+
:param ds: Items to be pushed onto FIFOQueue.
|
|
80
|
+
"""
|
|
81
|
+
self._ca.pushr(*ds)
|
|
82
|
+
|
|
83
|
+
def pop(self) -> MayBe[D]:
|
|
84
|
+
"""Pop oldest data item off of FIFOQueue.
|
|
85
|
+
|
|
86
|
+
:returns: MayBe of popped data item if queue was not empty, empty MayBe otherwise.
|
|
87
|
+
"""
|
|
88
|
+
if self._ca:
|
|
89
|
+
return MayBe(self._ca.popl())
|
|
90
|
+
return MayBe()
|
|
91
|
+
|
|
92
|
+
def peak_last_in(self) -> MayBe[D]:
|
|
93
|
+
"""Peak at newest item on queue.
|
|
94
|
+
|
|
95
|
+
:returns: MayBe of newest item on queue, empty MayBe if queue empty.
|
|
96
|
+
"""
|
|
97
|
+
if self._ca:
|
|
98
|
+
return MayBe(self._ca[-1])
|
|
99
|
+
return MayBe()
|
|
100
|
+
|
|
101
|
+
def peak_next_out(self) -> MayBe[D]:
|
|
102
|
+
"""Peak at oldest data item on queue.
|
|
103
|
+
|
|
104
|
+
:returns: MayBe of oldest item on queue, empty MayBe if queue empty.
|
|
105
|
+
"""
|
|
106
|
+
if self._ca:
|
|
107
|
+
return MayBe(self._ca[0])
|
|
108
|
+
return MayBe()
|
|
109
|
+
|
|
110
|
+
@overload
|
|
111
|
+
def fold[T](self, f: Callable[[D, D], D]) -> MayBe[D]: ...
|
|
112
|
+
@overload
|
|
113
|
+
def fold[T](self, f: Callable[[T, D], T], start: T) -> MayBe[T]: ...
|
|
114
|
+
|
|
115
|
+
def fold[T](self, f: Callable[[T, D], T], start: T | None = None) -> MayBe[T]:
|
|
116
|
+
"""Reduces FIFOQueue in natural FIFO Order, oldest to newest.
|
|
117
|
+
|
|
118
|
+
:param f: Reducing function, first argument is for accumulator.
|
|
119
|
+
:param start: Optional starting value.
|
|
120
|
+
:returns: MayBe of reduced value, empty MayBe if queue empty and no starting value given.
|
|
121
|
+
"""
|
|
122
|
+
if start is None:
|
|
123
|
+
if not self._ca:
|
|
124
|
+
return MayBe()
|
|
125
|
+
return MayBe(cast(T, self._ca.foldl(cast(Callable[[D, D], D], f)))) # T = D
|
|
126
|
+
return MayBe(self._ca.foldl(f, start))
|
|
127
|
+
|
|
128
|
+
def map[U](self, f: Callable[[D], U]) -> 'FIFOQueue[U]':
|
|
129
|
+
"""Map f over the FIFOQueue, retain original order.
|
|
130
|
+
|
|
131
|
+
:param f: Function to map over queue.
|
|
132
|
+
:returns: New FIFOQueue instance.
|
|
133
|
+
"""
|
|
134
|
+
return FIFOQueue(map(f, self._ca))
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def fifo_queue[D](*ds: D) -> FIFOQueue[D]:
|
|
138
|
+
"""FIFOQueue factory function.
|
|
139
|
+
|
|
140
|
+
:param ds: Initial items pushed on in FIFO order.
|
|
141
|
+
:returns: FIFOQueue with initialized items from ``ds``.
|
|
142
|
+
"""
|
|
143
|
+
return FIFOQueue(ds)
|
|
@@ -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
|
+
from collections.abc import Callable, Iterable, Iterator
|
|
16
|
+
from typing import cast, overload
|
|
17
|
+
from pythonic_fp.circulararray.auto import CA
|
|
18
|
+
from pythonic_fp.fptools.function import swap
|
|
19
|
+
from pythonic_fp.fptools.maybe import MayBe
|
|
20
|
+
|
|
21
|
+
__all__ = ['LIFOQueue', 'lifo_queue']
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class LIFOQueue[D]:
|
|
25
|
+
"""Stateful Last-In-First-Out (LIFO) Queue data structure.
|
|
26
|
+
|
|
27
|
+
- O(1) pops
|
|
28
|
+
- O(1) amortized pushes
|
|
29
|
+
- O(1) length determination
|
|
30
|
+
- in a Boolean context, true if not empty, false if empty
|
|
31
|
+
- will automatically increase storage capacity when needed
|
|
32
|
+
- neither indexable nor sliceable by design
|
|
33
|
+
|
|
34
|
+
"""
|
|
35
|
+
__slots__ = ('_ca',)
|
|
36
|
+
|
|
37
|
+
def __init__(self, *dss: Iterable[D]) -> None:
|
|
38
|
+
"""
|
|
39
|
+
:param dss: Takes 1 or 0 iterables, initializes data in natural LIFO order.
|
|
40
|
+
:raises ValueError: If more than 1 iterable is given.
|
|
41
|
+
"""
|
|
42
|
+
if (size := len(dss)) > 1:
|
|
43
|
+
msg = f'LIFOQueue expects at most 1 iterable argument, got {size}'
|
|
44
|
+
raise ValueError(msg)
|
|
45
|
+
self._ca = CA(dss[0]) if size == 1 else CA()
|
|
46
|
+
|
|
47
|
+
def __bool__(self) -> bool:
|
|
48
|
+
return len(self._ca) > 0
|
|
49
|
+
|
|
50
|
+
def __len__(self) -> int:
|
|
51
|
+
return len(self._ca)
|
|
52
|
+
|
|
53
|
+
def __eq__(self, other: object) -> bool:
|
|
54
|
+
if not isinstance(other, LIFOQueue):
|
|
55
|
+
return False
|
|
56
|
+
return self._ca == other._ca
|
|
57
|
+
|
|
58
|
+
def __iter__(self) -> Iterator[D]:
|
|
59
|
+
return reversed(list(self._ca))
|
|
60
|
+
|
|
61
|
+
def __repr__(self) -> str:
|
|
62
|
+
if len(self) == 0:
|
|
63
|
+
return 'LIFOQueue()'
|
|
64
|
+
return 'LIFOQueue(' + ', '.join(map(repr, self._ca)) + ')'
|
|
65
|
+
|
|
66
|
+
def __str__(self) -> str:
|
|
67
|
+
return '|| ' + ' > '.join(map(str, self)) + ' ><'
|
|
68
|
+
|
|
69
|
+
def copy(self) -> 'LIFOQueue[D]':
|
|
70
|
+
"""Shallow copy.
|
|
71
|
+
|
|
72
|
+
:returns: Shallow copy of the LIFOQueue.
|
|
73
|
+
"""
|
|
74
|
+
return LIFOQueue(reversed(self._ca))
|
|
75
|
+
|
|
76
|
+
def push(self, *ds: D) -> None:
|
|
77
|
+
"""Push items onto LIFOQueue.
|
|
78
|
+
|
|
79
|
+
:param ds: Items to be pushed onto LIFOQueue.
|
|
80
|
+
"""
|
|
81
|
+
self._ca.pushr(*ds)
|
|
82
|
+
|
|
83
|
+
def pop(self) -> MayBe[D]:
|
|
84
|
+
"""Pop newest data item off of LIFOQueue.
|
|
85
|
+
|
|
86
|
+
:returns: MayBe of popped item if queue was not empty, empty MayBe otherwise.
|
|
87
|
+
"""
|
|
88
|
+
if self._ca:
|
|
89
|
+
return MayBe(self._ca.popr())
|
|
90
|
+
return MayBe()
|
|
91
|
+
|
|
92
|
+
def peak(self) -> MayBe[D]:
|
|
93
|
+
"""Peak at newest item on queue.
|
|
94
|
+
|
|
95
|
+
:returns: MayBe of newest item on queue, empty MayBe if queue empty.
|
|
96
|
+
"""
|
|
97
|
+
if self._ca:
|
|
98
|
+
return MayBe(self._ca[-1])
|
|
99
|
+
return MayBe()
|
|
100
|
+
|
|
101
|
+
@overload
|
|
102
|
+
def fold[T](self, f: Callable[[D, D], D]) -> MayBe[D]: ...
|
|
103
|
+
@overload
|
|
104
|
+
def fold[T](self, f: Callable[[T, D], T], start: T) -> MayBe[T]: ...
|
|
105
|
+
|
|
106
|
+
def fold[T](self, f: Callable[[T, D], T], start: T | None = None) -> MayBe[T]:
|
|
107
|
+
"""Reduces LIFOQUEUE in natural LIFO Order, newest to oldest.
|
|
108
|
+
|
|
109
|
+
:param f: Reducing function, first argument is for accumulator.
|
|
110
|
+
:param start: Optional starting value.
|
|
111
|
+
:returns: MayBe of reduced value, empty MayBe if queue empty and no starting value given.
|
|
112
|
+
"""
|
|
113
|
+
if start is None:
|
|
114
|
+
if not self._ca:
|
|
115
|
+
return MayBe()
|
|
116
|
+
return MayBe(cast(T, self._ca.foldl(cast(Callable[[D, D], D], f)))) # T = D
|
|
117
|
+
return MayBe(self._ca.foldr(swap(f), start))
|
|
118
|
+
|
|
119
|
+
def map[U](self, f: Callable[[D], U]) -> 'LIFOQueue[U]':
|
|
120
|
+
"""Map f over the LIFOQueue, retain original order.
|
|
121
|
+
|
|
122
|
+
:param f: Function to map over queue.
|
|
123
|
+
:returns: New LIFOQueue instance.
|
|
124
|
+
"""
|
|
125
|
+
return LIFOQueue(reversed(CA(map(f, reversed(self._ca)))))
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def lifo_queue[D](*ds: D) -> LIFOQueue[D]:
|
|
129
|
+
"""LIFOQueue factory function.
|
|
130
|
+
|
|
131
|
+
:param ds: Initial items pushed on in LIFO order.
|
|
132
|
+
:returns: LIFOQueue with initialized items from ``ds``.
|
|
133
|
+
"""
|
|
134
|
+
return LIFOQueue(ds)
|
|
File without changes
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pythonic-fp-queues
|
|
3
|
+
Version: 5.1.0
|
|
4
|
+
Summary: Queues
|
|
5
|
+
Keywords: queue,fifo,lifo,dqueue
|
|
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 :: 5 - Production/Stable
|
|
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>=6.0.0
|
|
18
|
+
Requires-Dist: pythonic-fp-fptools>=5.1.2
|
|
19
|
+
Requires-Dist: pytest>=8.4.1 ; extra == "test"
|
|
20
|
+
Project-URL: Changelog, https://github.com/grscheller/pythonic-fp-queues/blob/main/CHANGELOG.rst
|
|
21
|
+
Project-URL: Documentation, https://grscheller.github.io/pythonic-fp/queues/development/build/html/index.html
|
|
22
|
+
Project-URL: Homepage, https://github.com/grscheller/pythonic-fp/blob/main/README.md
|
|
23
|
+
Project-URL: Source, https://github.com/grscheller/pythonic-fp-queues
|
|
24
|
+
Provides-Extra: test
|
|
25
|
+
|
|
26
|
+
Pythonic FP - Queues
|
|
27
|
+
====================
|
|
28
|
+
|
|
29
|
+
PyPI project
|
|
30
|
+
`pythonic-fp.queues
|
|
31
|
+
<https://pypi.org/project/pythonic-fp.queues>`_.
|
|
32
|
+
|
|
33
|
+
+-------------------------+-----------+--------------------------+
|
|
34
|
+
| module | class | name |
|
|
35
|
+
+=========================+===========+==========================+
|
|
36
|
+
| pythonic_fp.queues.fifo | FIFOQueue | First-In-First-Out Queue |
|
|
37
|
+
+-------------------------+-----------+--------------------------+
|
|
38
|
+
| pythonic_fp.queues.lifo | LIFOQueue | Last-In-First-Out Queue |
|
|
39
|
+
+-------------------------+-----------+--------------------------+
|
|
40
|
+
| pythonic_fp.queues.de | DEQueue | Double-Ended Queue |
|
|
41
|
+
+-------------------------+-----------+--------------------------+
|
|
42
|
+
|
|
43
|
+
Part of the
|
|
44
|
+
`pythonic-fp
|
|
45
|
+
<https://grscheller.github.io/pythonic-fp/homepage/build/html/index.html>`_
|
|
46
|
+
PyPI projects.
|
|
47
|
+
|
|
48
|
+
Documentation
|
|
49
|
+
-------------
|
|
50
|
+
|
|
51
|
+
Documentation for this project is hosted on
|
|
52
|
+
`GitHub Pages
|
|
53
|
+
<https://grscheller.github.io/pythonic-fp/queues/development/build/html>`_.
|
|
54
|
+
|
|
55
|
+
Copyright and License
|
|
56
|
+
---------------------
|
|
57
|
+
|
|
58
|
+
Copyright (c) 2023-2025 Geoffrey R. Scheller. Licensed under the Apache
|
|
59
|
+
License, Version 2.0. See the LICENSE file for details.
|
|
60
|
+
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
pythonic_fp/queues/__init__.py,sha256=BagRl3n3W-lxxPigkGNFMDFNs8surchki-YNXczrPto,1352
|
|
2
|
+
pythonic_fp/queues/de.py,sha256=qHDFe1djYGcDMn1THApsYtAd_iZDnJyrMJK0gHkD2vA,6072
|
|
3
|
+
pythonic_fp/queues/fifo.py,sha256=V7kAxqnBBBgheBHbaCo1tyORHJqeVR-HMzWiDdAulbU,4593
|
|
4
|
+
pythonic_fp/queues/lifo.py,sha256=HAlSz4eKlVatLXkL6uux_gZXjFwiA4X_btDTAF6PM10,4406
|
|
5
|
+
pythonic_fp/queues/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
|
+
pythonic_fp_queues-5.1.0.dist-info/licenses/LICENSE,sha256=IJMkVWQUVy-SonK6SMaMotDQ6Zb-lVi2bUTiGp1GUO8,10774
|
|
7
|
+
pythonic_fp_queues-5.1.0.dist-info/WHEEL,sha256=G2gURzTEtmeR8nrdXUJfNiB3VYVxigPQ-bEQujpNiNs,82
|
|
8
|
+
pythonic_fp_queues-5.1.0.dist-info/METADATA,sha256=6AAV5vX106ojMsJtRQXGxZHz7ROKCZsghKcpYcuzASo,2282
|
|
9
|
+
pythonic_fp_queues-5.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright (c) 2023-2025 Geoffrey R. Scheller
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|