pythonic-fp.splitends 1.0.1__py3-none-any.whl → 2.0.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/splitends/__init__.py +14 -11
- pythonic_fp/splitends/__init__.pyi +0 -0
- pythonic_fp/splitends/splitend.py +160 -66
- pythonic_fp/splitends/splitend.pyi +21 -28
- pythonic_fp/splitends/splitend_node.py +73 -70
- pythonic_fp/splitends/splitend_node.pyi +14 -30
- {pythonic_fp_splitends-1.0.1.dist-info → pythonic_fp_splitends-2.0.0.dist-info}/METADATA +10 -11
- pythonic_fp_splitends-2.0.0.dist-info/RECORD +11 -0
- pythonic_fp_splitends-1.0.1.dist-info/RECORD +0 -10
- {pythonic_fp_splitends-1.0.1.dist-info → pythonic_fp_splitends-2.0.0.dist-info}/WHEEL +0 -0
- {pythonic_fp_splitends-1.0.1.dist-info → pythonic_fp_splitends-2.0.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -14,19 +14,22 @@
|
|
|
14
14
|
|
|
15
15
|
"""Mutable stack objects that can safely share data.
|
|
16
16
|
|
|
17
|
-
Python package
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
Python package implementing a singularly linked stack of nodes called a ``SplitEnd``.
|
|
18
|
+
While mutable, these data structures can safely share immutable data nodes between
|
|
19
|
+
themselves. These nodes, of type ``SENode``, always contain data. A root node is
|
|
20
|
+
a node whose previous node is itself. Root nodes mark the bottom of the stack.
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
- different mutable split ends can safely share the same "tail"
|
|
25
|
-
- each ``SplitEnd`` sees itself as a singularly linked list
|
|
26
|
-
- bush-like datastructures can be formed using multiple ``SplitEnds``
|
|
27
|
-
- the ``SplitEnd`` copy method and ``len`` are O(1)
|
|
28
|
-
- in boolean context returns true if the ``SplitEnd`` is not just a "root"
|
|
22
|
+
Like one of many "split ends" from shafts of hair, a ``SplitEnd`` can be "snipped"
|
|
23
|
+
shorter or "extended" further from its "end" node at the top of the stack. Its "root"
|
|
24
|
+
node is at the bottom of the stack and can not be "snipped" or "cut" off.
|
|
29
25
|
|
|
26
|
+
A ``SplitEnd`` can be duplicated onto a compatible root node. By compatible, the
|
|
27
|
+
new root must contain data that compares as equal to the old root node.
|
|
28
|
+
|
|
29
|
+
TODO: A ``scalp`` is a container of root nodes all of whose contained data must be
|
|
30
|
+
hashable. The ``scalp.add(d)`` method will return the unique root node with data
|
|
31
|
+
that compares as equal to ``d``, or create a new root node if such a contained node
|
|
32
|
+
does not exist.
|
|
30
33
|
"""
|
|
31
34
|
|
|
32
35
|
__author__ = 'Geoffrey R. Scheller'
|
|
File without changes
|
|
@@ -12,51 +12,79 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
-
"""
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
"""
|
|
16
|
+
Class SplitEnds
|
|
17
|
+
===============
|
|
18
|
+
|
|
19
|
+
**LIFO stacks safely sharing immutable data.**
|
|
20
|
+
|
|
21
|
+
- each ``SplitEnd`` is a very simple stateful (mutable) LIFO stack
|
|
22
|
+
- data can be either "extended" to or "snipped" off the "end"
|
|
23
|
+
- the "root" of a ``SplitEnd``
|
|
24
|
+
- it is fixed and cannot be removed from the ``SplitEnd``
|
|
25
|
+
- different mutable split ends can safely share the same "tail"
|
|
26
|
+
- each ``SplitEnd`` sees itself as a singularly linked list
|
|
27
|
+
- bush-like datastructures can be formed using multiple ``SplitEnds``
|
|
28
|
+
- the ``SplitEnd.split`` and ``len`` methods are O(1)
|
|
29
|
+
- in boolean context returns true if the ``SplitEnd`` is not just its "root"
|
|
30
|
+
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
from collections.abc import Callable, Iterator
|
|
34
|
+
from pythonic_fp.iterables.folding import reduce_left, fold_left
|
|
35
|
+
from typing import cast, Final, overload
|
|
36
|
+
from pythonic_fp.queues.lifo import LIFOQueue
|
|
37
|
+
from pythonic_fp.gadgets.sentinels.flavored import Sentinel
|
|
20
38
|
from .splitend_node import SENode
|
|
21
39
|
|
|
22
40
|
__all__ = ['SplitEnd']
|
|
23
41
|
|
|
24
|
-
|
|
42
|
+
type _SecretType = tuple[str, str, str]
|
|
43
|
+
_secret_value: Final[_SecretType] = 'split', 'end', '_private'
|
|
44
|
+
type _Sentinel = Sentinel[_SecretType]
|
|
45
|
+
_sentinel: Final[_Sentinel] = Sentinel(_secret_value)
|
|
25
46
|
|
|
26
47
|
|
|
27
48
|
class SplitEnd[D]:
|
|
28
|
-
|
|
29
|
-
a ``splitend`` can be "snipped" shorter or "extended"
|
|
30
|
-
further from its "tip". Its root is irremovable and
|
|
31
|
-
cannot be "snipped" off. While mutable, different
|
|
32
|
-
splitends can safely share data with each other.
|
|
33
|
-
|
|
34
|
-
"""
|
|
35
|
-
|
|
36
|
-
__slots__ = '_count', '_tip', '_root'
|
|
49
|
+
__slots__ = '_end', '_root', '_count'
|
|
37
50
|
|
|
38
|
-
def __init__(self,
|
|
51
|
+
def __init__(self, *ds: D, root: SENode[D] | _Sentinel = _sentinel) -> None:
|
|
39
52
|
"""
|
|
40
|
-
:param root_data:
|
|
41
|
-
:param data:
|
|
42
|
-
|
|
53
|
+
:param root_data: Irremovable initial data at bottom of stack.
|
|
54
|
+
:param data: Removable data to be pushed onto splitend stack.
|
|
43
55
|
"""
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
56
|
+
if root is _sentinel:
|
|
57
|
+
if ds:
|
|
58
|
+
root_node: SENode[D] = SENode(ds[0])
|
|
59
|
+
ds = ds[1:]
|
|
60
|
+
else:
|
|
61
|
+
msg = 'SplitEnd: No data provided for root node'
|
|
62
|
+
raise ValueError(msg)
|
|
63
|
+
else:
|
|
64
|
+
if root:
|
|
65
|
+
root_node = cast(SENode[D], root)
|
|
66
|
+
else:
|
|
67
|
+
msg = 'SplitEnd: Provided node is not a root node'
|
|
68
|
+
raise ValueError(msg)
|
|
69
|
+
|
|
70
|
+
end, count = root_node, 1
|
|
71
|
+
for d in ds:
|
|
72
|
+
node = SENode(d, end)
|
|
73
|
+
end, count = node, count + 1
|
|
74
|
+
|
|
75
|
+
self._end, self._root, self._count = end, root_node, count
|
|
50
76
|
|
|
51
77
|
def __iter__(self) -> Iterator[D]:
|
|
52
|
-
return iter(self.
|
|
78
|
+
return iter(self._end)
|
|
53
79
|
|
|
54
80
|
def __reversed__(self) -> Iterator[D]:
|
|
55
81
|
return reversed(list(self))
|
|
56
82
|
|
|
57
83
|
def __bool__(self) -> bool:
|
|
58
|
-
|
|
59
|
-
|
|
84
|
+
"""
|
|
85
|
+
:returns: ``True`` is ``SplitEnd`` is not just its root node.
|
|
86
|
+
"""
|
|
87
|
+
return bool(self._end)
|
|
60
88
|
|
|
61
89
|
def __len__(self) -> int:
|
|
62
90
|
return self._count
|
|
@@ -76,79 +104,145 @@ class SplitEnd[D]:
|
|
|
76
104
|
if self._root != other._root:
|
|
77
105
|
return False
|
|
78
106
|
|
|
79
|
-
left = self.
|
|
80
|
-
right = other.
|
|
107
|
+
left = self._end
|
|
108
|
+
right = other._end
|
|
81
109
|
for _ in range(self._count):
|
|
82
110
|
if left is right:
|
|
83
111
|
return True
|
|
84
|
-
if left.
|
|
112
|
+
if left.data() != right.data():
|
|
85
113
|
return False
|
|
86
114
|
if left:
|
|
87
|
-
left = left.
|
|
88
|
-
right = right.
|
|
115
|
+
left = left.prev()
|
|
116
|
+
right = right.prev()
|
|
89
117
|
return True
|
|
90
118
|
|
|
119
|
+
def cut(self, num: int | None = None) -> tuple[D, ...]:
|
|
120
|
+
"""Cut data off end of ``SplitEnd``.
|
|
121
|
+
|
|
122
|
+
:param num: Optional number of nodes to cut, default is entire stack.
|
|
123
|
+
:returns: Tuple of data cut off from end.
|
|
124
|
+
"""
|
|
125
|
+
if num is None or num > self._count:
|
|
126
|
+
num = self._count
|
|
127
|
+
|
|
128
|
+
data: tuple[D, ...] = ()
|
|
129
|
+
node = self._end
|
|
130
|
+
count = self._count
|
|
131
|
+
n = num
|
|
132
|
+
while n > 0:
|
|
133
|
+
d, node = node.both()
|
|
134
|
+
data = data + (d,)
|
|
135
|
+
n -= 1
|
|
136
|
+
|
|
137
|
+
if self._count - num > 1:
|
|
138
|
+
self._end, self._count = node, count - num
|
|
139
|
+
else:
|
|
140
|
+
self._end, self._count = node, 1
|
|
141
|
+
|
|
142
|
+
return data
|
|
143
|
+
|
|
91
144
|
def extend(self, *ds: D) -> None:
|
|
92
145
|
"""Add data onto the tip of the SplitEnd. Like adding a hair
|
|
93
146
|
extension.
|
|
94
147
|
|
|
95
148
|
:param ds: data to extend the splitend
|
|
96
|
-
|
|
97
149
|
"""
|
|
98
150
|
for d in ds:
|
|
99
|
-
node = SENode(d, self.
|
|
100
|
-
self.
|
|
151
|
+
node = SENode(d, self._end)
|
|
152
|
+
self._end, self._count = node, self._count + 1
|
|
101
153
|
|
|
102
|
-
def
|
|
103
|
-
"""
|
|
154
|
+
def peak(self) -> D:
|
|
155
|
+
"""Return the data at end (top) of SplitEnd without consuming it.
|
|
156
|
+
|
|
157
|
+
:returns: The data at the end of the SplitEnd.
|
|
158
|
+
"""
|
|
159
|
+
return self._end.data()
|
|
160
|
+
|
|
161
|
+
def root(self) -> SENode[D]:
|
|
162
|
+
"""
|
|
163
|
+
:returns: The root SENode node of the SplitEnd.
|
|
164
|
+
"""
|
|
165
|
+
return self._root
|
|
166
|
+
|
|
167
|
+
def reroot(self, root: SENode[D]) -> 'SplitEnd[D]':
|
|
168
|
+
"""Create a brand new SplitEnd with the same data but different root.
|
|
169
|
+
|
|
170
|
+
.. note::
|
|
104
171
|
|
|
105
|
-
|
|
172
|
+
Two nodes are compatible root nodes if and only if
|
|
106
173
|
|
|
174
|
+
- they are both actually root nodes
|
|
175
|
+
- that is their previous nodes are themselves
|
|
176
|
+
- their data compare as equal
|
|
177
|
+
- comparing by identity is too strong for some use cases
|
|
178
|
+
|
|
179
|
+
:returns: New SplitEnd with the same data and the new ``root``.
|
|
180
|
+
:raises ValueError: If new and original root nodes are not compatible.
|
|
107
181
|
"""
|
|
108
|
-
if
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
182
|
+
if not root:
|
|
183
|
+
msg = 'New root node is not a root node.'
|
|
184
|
+
raise ValueError(msg)
|
|
185
|
+
if root.data() != self._root.data():
|
|
186
|
+
msg = 'New root node not compatible with current root node.'
|
|
187
|
+
raise ValueError(msg)
|
|
112
188
|
|
|
113
|
-
|
|
189
|
+
lifo = LIFOQueue[D]()
|
|
190
|
+
for data in self:
|
|
191
|
+
lifo.push(data)
|
|
192
|
+
lifo.pop()
|
|
114
193
|
|
|
115
|
-
|
|
116
|
-
"""Return data from tip of SplitEnd, do not consume it.
|
|
194
|
+
return SplitEnd(*lifo, root = root)
|
|
117
195
|
|
|
118
|
-
|
|
196
|
+
def snip(self) -> D:
|
|
197
|
+
"""Snip data off tip of SplitEnd. Just return data if tip is root.
|
|
119
198
|
|
|
199
|
+
:returns: Data snipped off tip, just return root data if at root.
|
|
120
200
|
"""
|
|
121
|
-
|
|
201
|
+
if self._count > 1:
|
|
202
|
+
data, self._end, self._count = self._end.both() + (self._count - 1,)
|
|
203
|
+
else:
|
|
204
|
+
data = self._end.data()
|
|
122
205
|
|
|
123
|
-
|
|
124
|
-
"""Return a copy of the SplitEnd. O(1) space & time complexity.
|
|
206
|
+
return data
|
|
125
207
|
|
|
126
|
-
|
|
208
|
+
def split(self, *ds: D) -> 'SplitEnd[D]':
|
|
209
|
+
"""Split the end and add more data.
|
|
127
210
|
|
|
211
|
+
:returns: New instance, same data nodes plus additional ones on end.
|
|
128
212
|
"""
|
|
129
|
-
se: SplitEnd[D] = SplitEnd(self._root.
|
|
130
|
-
se._count, se.
|
|
213
|
+
se: SplitEnd[D] = SplitEnd(self._root.data())
|
|
214
|
+
se._count, se._end, se._root = self._count, self._end, self._root
|
|
215
|
+
se.extend(*ds)
|
|
131
216
|
return se
|
|
132
217
|
|
|
133
|
-
|
|
134
|
-
|
|
218
|
+
@overload
|
|
219
|
+
def fold[T](self, f: Callable[[D, D], D]) -> D: ...
|
|
220
|
+
@overload
|
|
221
|
+
def fold[T](self, f: Callable[[T, D], T], init: T) -> T: ...
|
|
135
222
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
:returns: reduced value folding from tip to root in natural LIFO order
|
|
223
|
+
def fold[T](self, f: Callable[[T, D], T], init: T | None = None) -> T:
|
|
224
|
+
"""Reduce with a function, folding from tip to root.
|
|
139
225
|
|
|
226
|
+
:param f: Folding function, first argument is for the accumulator.
|
|
227
|
+
:param init: Optional initial starting value for the fold.
|
|
228
|
+
:returns: Reduced value folding from tip to root in natural LIFO order.
|
|
140
229
|
"""
|
|
141
|
-
|
|
230
|
+
if init is None:
|
|
231
|
+
return self._end.fold(f) # type: ignore
|
|
232
|
+
return self._end.fold(f, init)
|
|
233
|
+
|
|
234
|
+
@overload
|
|
235
|
+
def rev_fold[T](self, f: Callable[[D, D], D]) -> D: ...
|
|
236
|
+
@overload
|
|
237
|
+
def rev_fold[T](self, f: Callable[[T, D], T], init: T) -> T: ...
|
|
142
238
|
|
|
143
239
|
def rev_fold[T](self, f: Callable[[T, D], T], init: T | None = None) -> T:
|
|
144
240
|
"""Reduce with a function, fold from root to tip.
|
|
145
241
|
|
|
146
|
-
:param f:
|
|
147
|
-
:param init:
|
|
148
|
-
:returns:
|
|
149
|
-
|
|
242
|
+
:param f: Folding function, first argument is for the accumulator.
|
|
243
|
+
:param init: Optional initial starting value for the fold.
|
|
244
|
+
:returns: Reduced value folding from root to tip.
|
|
150
245
|
"""
|
|
151
|
-
# The get() is safe because SplitEnds are never "empty" due to the root.
|
|
152
246
|
if init is None:
|
|
153
|
-
return
|
|
154
|
-
return
|
|
247
|
+
return cast(T, reduce_left(reversed(self), cast(Callable[[D, D], D], f)))
|
|
248
|
+
return fold_left(reversed(self), f, init)
|
|
@@ -1,38 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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, Hashable, Iterator
|
|
16
|
-
from typing import TypeVar
|
|
1
|
+
from .splitend_node import SENode
|
|
2
|
+
from collections.abc import Callable, Iterator
|
|
3
|
+
from pythonic_fp.gadgets.sentinels.flavored import Sentinel
|
|
4
|
+
from typing import overload
|
|
17
5
|
|
|
18
6
|
__all__ = ['SplitEnd']
|
|
19
7
|
|
|
20
|
-
|
|
21
|
-
|
|
8
|
+
type _SecretType = tuple[str, str, str]
|
|
9
|
+
type _Sentinel = Sentinel[_SecretType]
|
|
22
10
|
class SplitEnd[D]:
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
def __init__(self, root_data: D, *data: D) -> None: ...
|
|
11
|
+
def __init__(self, *ds: D, root: SENode[D] | _Sentinel = ...) -> None: ...
|
|
26
12
|
def __iter__(self) -> Iterator[D]: ...
|
|
27
13
|
def __reversed__(self) -> Iterator[D]: ...
|
|
28
14
|
def __bool__(self) -> bool: ...
|
|
29
15
|
def __len__(self) -> int: ...
|
|
30
|
-
def
|
|
31
|
-
def
|
|
32
|
-
def __eq__(self, other: object, /) -> bool: ...
|
|
16
|
+
def __eq__(self, other: object) -> bool: ...
|
|
17
|
+
def cut(self, num: int | None = None) -> tuple[D, ...]: ...
|
|
33
18
|
def extend(self, *ds: D) -> None: ...
|
|
34
|
-
def snip(self) -> D: ...
|
|
35
19
|
def peak(self) -> D: ...
|
|
36
|
-
def
|
|
37
|
-
def
|
|
38
|
-
def
|
|
20
|
+
def root(self) -> SENode[D]: ...
|
|
21
|
+
def reroot(self, root: SENode[D]) -> SplitEnd[D]: ...
|
|
22
|
+
def snip(self) -> D: ...
|
|
23
|
+
def split(self, *ds: D) -> SplitEnd[D]: ...
|
|
24
|
+
@overload
|
|
25
|
+
def fold[T](self, f: Callable[[D, D], D]) -> D: ...
|
|
26
|
+
@overload
|
|
27
|
+
def fold[T](self, f: Callable[[T, D], T], init: T) -> T: ...
|
|
28
|
+
@overload
|
|
29
|
+
def rev_fold[T](self, f: Callable[[D, D], D]) -> D: ...
|
|
30
|
+
@overload
|
|
31
|
+
def rev_fold[T](self, f: Callable[[T, D], T], init: T) -> T: ...
|
|
@@ -12,123 +12,126 @@
|
|
|
12
12
|
# See the License for the specific language governing permissions and
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
|
-
"""
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
to
|
|
15
|
+
"""
|
|
16
|
+
Class SENode
|
|
17
|
+
============
|
|
18
|
+
|
|
19
|
+
**Used to make inwardly directed bush-like graphs.**
|
|
20
|
+
|
|
21
|
+
- designed so multiple splitends can safely share the same data
|
|
22
|
+
- nodes always contain data
|
|
23
|
+
- data node ``SENode[D]`` make up end-to-root singularly linked lists
|
|
24
|
+
- two nodes compare as equal if
|
|
25
|
+
- both their previous Nodes are the same
|
|
26
|
+
- their data compare as equal
|
|
27
|
+
- a root node is a node whose previous node is itself
|
|
28
|
+
- root nodes mark the bottom of splitend stacks
|
|
29
|
+
- more than one node can point to the same proceeding node
|
|
30
|
+
- forming bush like graphs
|
|
20
31
|
|
|
21
32
|
"""
|
|
22
33
|
|
|
23
|
-
from collections.abc import Callable,
|
|
24
|
-
from typing import cast,
|
|
25
|
-
from pythonic_fp.
|
|
34
|
+
from collections.abc import Callable, Iterator
|
|
35
|
+
from typing import cast, Final, overload, Self
|
|
36
|
+
from pythonic_fp.gadgets.sentinels.flavored import Sentinel
|
|
26
37
|
|
|
27
38
|
__all__ = ['SENode']
|
|
28
39
|
|
|
29
|
-
|
|
40
|
+
type _Sentinel = Sentinel[str]
|
|
41
|
+
_sentinel: Final[_Sentinel] = Sentinel('_split_end_node_private_str')
|
|
30
42
|
|
|
31
43
|
|
|
32
44
|
class SENode[D]:
|
|
33
|
-
"""Data node for class SplitEnd
|
|
34
|
-
|
|
35
|
-
- hashable data node for a end-to-root singularly linked list.
|
|
36
|
-
- designed so multiple splitends can safely share the same data
|
|
37
|
-
|
|
38
|
-
- two nodes compare as equal if
|
|
39
|
-
|
|
40
|
-
- both their previous Nodes are the same
|
|
41
|
-
- their data compare as equal
|
|
42
|
-
|
|
43
|
-
- more than one node can point to the same proceeding node
|
|
44
|
-
|
|
45
|
-
- forming bush like graphs
|
|
46
|
-
|
|
47
|
-
"""
|
|
48
|
-
|
|
49
45
|
__slots__ = '_data', '_prev'
|
|
50
46
|
|
|
51
|
-
def __init__(self, data: D, prev:
|
|
47
|
+
def __init__(self, data: D, prev: Self | _Sentinel = _sentinel) -> None:
|
|
52
48
|
"""
|
|
53
|
-
:param data:
|
|
54
|
-
:param prev:
|
|
49
|
+
:param data: Nodes always contain data of type ``D``.
|
|
50
|
+
:param prev: Link to previous node. Points to ``self`` if a root node.
|
|
51
|
+
"""
|
|
52
|
+
self._data = data
|
|
53
|
+
if prev is not _sentinel:
|
|
54
|
+
self._prev = cast(Self, prev)
|
|
55
|
+
else:
|
|
56
|
+
self._prev = self
|
|
55
57
|
|
|
58
|
+
def __bool__(self) -> bool:
|
|
59
|
+
"""
|
|
60
|
+
:returns: ``True`` if ``SENode`` is not a root node.
|
|
56
61
|
"""
|
|
57
|
-
self.
|
|
58
|
-
self._prev: MayBe[SENode[D]] = MayBe(prev) if prev is not None else MayBe()
|
|
62
|
+
return self._prev is not self
|
|
59
63
|
|
|
60
64
|
def __iter__(self) -> Iterator[D]:
|
|
61
65
|
node = self
|
|
62
66
|
while node:
|
|
63
67
|
yield node._data
|
|
64
|
-
node = node._prev
|
|
68
|
+
node = node._prev
|
|
65
69
|
yield node._data
|
|
66
70
|
|
|
67
|
-
def __bool__(self) -> bool:
|
|
68
|
-
return self._prev != MayBe()
|
|
69
|
-
|
|
70
71
|
def __eq__(self, other: object) -> bool:
|
|
72
|
+
"""
|
|
73
|
+
Two ``SENodes`` nodes are equal if their previous nodes are the
|
|
74
|
+
same object and their data compare as equal.
|
|
75
|
+
"""
|
|
71
76
|
if not isinstance(other, type(self)):
|
|
72
77
|
return False
|
|
73
78
|
|
|
74
|
-
if self._prev
|
|
79
|
+
if self._prev is not other._prev:
|
|
75
80
|
return False
|
|
76
81
|
if self._data == other._data:
|
|
77
82
|
return True
|
|
78
83
|
return False
|
|
79
84
|
|
|
80
|
-
def
|
|
81
|
-
"""
|
|
82
|
-
|
|
83
|
-
:returns: data stored in SENode
|
|
85
|
+
def both(self) -> tuple[D, Self]:
|
|
86
|
+
"""Peak at data and previous node, if a root then data and self.
|
|
84
87
|
|
|
88
|
+
:returns: tuple of type tuple[D, SENode[D]]
|
|
85
89
|
"""
|
|
86
|
-
return self._data
|
|
87
|
-
|
|
88
|
-
def pop2(self) -> 'tuple[D, SENode[D]]':
|
|
89
|
-
"""Return the data at the tip and the tail of the SENode.
|
|
90
|
+
return self._data, self._prev
|
|
90
91
|
|
|
91
|
-
|
|
92
|
+
def data(self) -> D:
|
|
93
|
+
"""Peak at data.
|
|
92
94
|
|
|
95
|
+
:returns: The data stored in the ``SENode``.
|
|
93
96
|
"""
|
|
94
|
-
|
|
95
|
-
return self._data, self._prev.get()
|
|
96
|
-
return self._data, self
|
|
97
|
-
|
|
98
|
-
def push(self, data: D) -> 'SENode[D]':
|
|
99
|
-
"""Push data onto the queue and return a new node containing the data.
|
|
97
|
+
return self._data
|
|
100
98
|
|
|
101
|
-
|
|
102
|
-
|
|
99
|
+
def prev(self) -> Self:
|
|
100
|
+
"""Peak at previous node.
|
|
103
101
|
|
|
102
|
+
:returns: The previous node stored in the ``SENode``.
|
|
104
103
|
"""
|
|
105
|
-
return
|
|
104
|
+
return self._prev
|
|
106
105
|
|
|
107
|
-
def
|
|
108
|
-
"""
|
|
106
|
+
def push(self, data: D) -> Self:
|
|
107
|
+
"""Create a new ``SENode[D]``.
|
|
109
108
|
|
|
110
|
-
|
|
109
|
+
:param data: Data for new node to contain.
|
|
110
|
+
:returns: New ``SENode`` whose previous node is the current node.
|
|
111
|
+
"""
|
|
112
|
+
return cast(Self, SENode(data, self))
|
|
111
113
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
) -> T
|
|
114
|
+
@overload
|
|
115
|
+
def fold(self, f: Callable[[D, D], D]) -> D: ...
|
|
116
|
+
@overload
|
|
117
|
+
def fold[T](self, f: Callable[[T, D], T], init: T) -> T: ...
|
|
117
118
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
:returns: reduced value folding from end to root in natural LIFO order
|
|
119
|
+
def fold[T](self, f: Callable[[T, D], T], init: T | _Sentinel = _sentinel) -> T:
|
|
120
|
+
"""Fold data across linked nodes with a function..
|
|
121
121
|
|
|
122
|
+
:param f: Folding function, first argument is for accumulated value.`
|
|
123
|
+
:param init: Optional initial starting value for the fold.
|
|
124
|
+
:returns: Reduced value folded from end to root in natural LIFO order.
|
|
122
125
|
"""
|
|
123
|
-
if init is
|
|
124
|
-
acc
|
|
125
|
-
node = self._prev.get()
|
|
126
|
-
else:
|
|
127
|
-
acc = init
|
|
126
|
+
if init is not _sentinel:
|
|
127
|
+
acc = cast(T, init)
|
|
128
128
|
node = self
|
|
129
|
+
else:
|
|
130
|
+
acc = cast(T, self._data) # in this case T = D
|
|
131
|
+
node = self._prev
|
|
129
132
|
|
|
130
133
|
while node:
|
|
131
134
|
acc = f(acc, node._data)
|
|
132
|
-
node = node._prev
|
|
135
|
+
node = node._prev
|
|
133
136
|
acc = f(acc, node._data)
|
|
134
137
|
return acc
|
|
@@ -1,36 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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, Hashable, Iterator
|
|
16
|
-
from typing import TypeVar
|
|
17
|
-
from pythonic_fp.fptools.maybe import MayBe
|
|
1
|
+
from collections.abc import Callable, Iterator
|
|
2
|
+
from pythonic_fp.gadgets.sentinels.flavored import Sentinel
|
|
3
|
+
from typing import Self, overload
|
|
18
4
|
|
|
19
5
|
__all__ = ['SENode']
|
|
20
6
|
|
|
21
|
-
|
|
22
|
-
|
|
7
|
+
type _Sentinel = Sentinel[str]
|
|
23
8
|
class SENode[D]:
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
_data: D
|
|
27
|
-
_prev: MayBe[SENode[D]]
|
|
28
|
-
|
|
29
|
-
def __init__(self, data: D, prev: SENode[D] | None = None) -> None: ...
|
|
30
|
-
def __iter__(self) -> Iterator[D]: ...
|
|
9
|
+
def __init__(self, data: D, prev: Self | _Sentinel = ...) -> None: ...
|
|
31
10
|
def __bool__(self) -> bool: ...
|
|
11
|
+
def __iter__(self) -> Iterator[D]: ...
|
|
32
12
|
def __eq__(self, other: object) -> bool: ...
|
|
33
|
-
def
|
|
34
|
-
def
|
|
35
|
-
def
|
|
36
|
-
def
|
|
13
|
+
def both(self) -> tuple[D, Self]: ...
|
|
14
|
+
def data(self) -> D: ...
|
|
15
|
+
def prev(self) -> Self: ...
|
|
16
|
+
def push(self, data: D) -> Self: ...
|
|
17
|
+
@overload
|
|
18
|
+
def fold(self, f: Callable[[D, D], D]) -> D: ...
|
|
19
|
+
@overload
|
|
20
|
+
def fold[T](self, f: Callable[[T, D], T], init: T) -> T: ...
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pythonic-fp-splitends
|
|
3
|
-
Version:
|
|
3
|
+
Version: 2.0.0
|
|
4
4
|
Summary: Mutable stack objects that can safely share data.
|
|
5
5
|
Keywords: SplitEnd,lifo,stack
|
|
6
6
|
Author-email: "Geoffrey R. Scheller" <geoffrey@scheller.com>
|
|
@@ -14,13 +14,13 @@ Classifier: Operating System :: OS Independent
|
|
|
14
14
|
Classifier: Programming Language :: Python :: 3.13
|
|
15
15
|
Classifier: Typing :: Typed
|
|
16
16
|
License-File: LICENSE
|
|
17
|
-
Requires-Dist: pythonic-fp-fptools>=5.1.
|
|
18
|
-
Requires-Dist: pythonic-fp-iterables>=5.1.
|
|
17
|
+
Requires-Dist: pythonic-fp-fptools>=5.1.2
|
|
18
|
+
Requires-Dist: pythonic-fp-iterables>=5.1.2
|
|
19
|
+
Requires-Dist: pythonic-fp-queues>=5.1.0
|
|
19
20
|
Requires-Dist: pytest>=8.4.1 ; extra == "test"
|
|
20
|
-
Requires-Dist: pythonic-fp-gadgets>=3.0.1 ; extra == "test"
|
|
21
21
|
Project-URL: Changelog, https://github.com/grscheller/pythonic-fp-splitends/blob/main/CHANGELOG.rst
|
|
22
|
-
Project-URL: Documentation, https://grscheller.github.io/pythonic-fp/splitends/development/build/html/
|
|
23
|
-
Project-URL: Homepage, https://github.
|
|
22
|
+
Project-URL: Documentation, https://grscheller.github.io/pythonic-fp/splitends/development/build/html/
|
|
23
|
+
Project-URL: Homepage, https://grscheller.github.io/pythonic-fp/homepage/build/html/
|
|
24
24
|
Project-URL: Source, https://github.com/grscheller/pythonic-fp-splitends
|
|
25
25
|
Provides-Extra: test
|
|
26
26
|
|
|
@@ -43,11 +43,10 @@ between themselves.
|
|
|
43
43
|
- the ``SplitEnd`` copy method and ``len`` are O(1)
|
|
44
44
|
- in boolean context returns true if the ``SplitEnd`` is not just a "root"
|
|
45
45
|
|
|
46
|
-
|
|
47
|
-
`pythonic-fp
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
change.
|
|
46
|
+
Part of the
|
|
47
|
+
`pythonic-fp
|
|
48
|
+
<https://grscheller.github.io/pythonic-fp/homepage/build/html/index.html>`_
|
|
49
|
+
PyPI projects.
|
|
51
50
|
|
|
52
51
|
Documentation
|
|
53
52
|
-------------
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
pythonic_fp/splitends/__init__.py,sha256=mydkUVuGD_Ns3G4LYgOQvPfTxMCkCkFwC3oZFDHnRmM,1775
|
|
2
|
+
pythonic_fp/splitends/__init__.pyi,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
pythonic_fp/splitends/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
+
pythonic_fp/splitends/splitend.py,sha256=XQ4-1x8lW5i480YHjBcwAhh4ddxtPF7KsIrMnbaM8Ks,8242
|
|
5
|
+
pythonic_fp/splitends/splitend.pyi,sha256=OdTRATFXX5smbpwc8wm3gt4yyeTd_kN8VbMeNYl2Cho,1193
|
|
6
|
+
pythonic_fp/splitends/splitend_node.py,sha256=rNjGU8VqhOo6AFa-R6zcOrrAtfIpzkzI9ulsa8iRg2w,4252
|
|
7
|
+
pythonic_fp/splitends/splitend_node.pyi,sha256=TozeGg4lmjMf0UPfZRTxRJR2kRpq0053VjbjIBUeosk,705
|
|
8
|
+
pythonic_fp_splitends-2.0.0.dist-info/licenses/LICENSE,sha256=IJMkVWQUVy-SonK6SMaMotDQ6Zb-lVi2bUTiGp1GUO8,10774
|
|
9
|
+
pythonic_fp_splitends-2.0.0.dist-info/WHEEL,sha256=G2gURzTEtmeR8nrdXUJfNiB3VYVxigPQ-bEQujpNiNs,82
|
|
10
|
+
pythonic_fp_splitends-2.0.0.dist-info/METADATA,sha256=LMjr3oM6p1DbZhL9f6mAvd8gDDa5klTdA8IiJiFdn_w,2435
|
|
11
|
+
pythonic_fp_splitends-2.0.0.dist-info/RECORD,,
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
pythonic_fp/splitends/__init__.py,sha256=gZzfqx_ODj0p32Zije6mFB-VUovxn2e8MGXjtpqcP2g,1457
|
|
2
|
-
pythonic_fp/splitends/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
-
pythonic_fp/splitends/splitend.py,sha256=k5oBCoTDL3OGKAgJ9bYeHZ5JL-FnRKwdQ7mEqS1e3x0,5018
|
|
4
|
-
pythonic_fp/splitends/splitend.pyi,sha256=xBJHTBdMO0VzeyhIUTiflm3BA3Pfq1GOjy9kvJOju1Q,1437
|
|
5
|
-
pythonic_fp/splitends/splitend_node.py,sha256=Fr74SpejGuLPK4giv1jKslX7NXxYe6Zh31Q3HDyAvEE,3894
|
|
6
|
-
pythonic_fp/splitends/splitend_node.pyi,sha256=3vmKwiQBsJV0v043fkA6CVPaNSFeJ02chUuwIs3G-YY,1273
|
|
7
|
-
pythonic_fp_splitends-1.0.1.dist-info/licenses/LICENSE,sha256=IJMkVWQUVy-SonK6SMaMotDQ6Zb-lVi2bUTiGp1GUO8,10774
|
|
8
|
-
pythonic_fp_splitends-1.0.1.dist-info/WHEEL,sha256=G2gURzTEtmeR8nrdXUJfNiB3VYVxigPQ-bEQujpNiNs,82
|
|
9
|
-
pythonic_fp_splitends-1.0.1.dist-info/METADATA,sha256=Q1-I-USkH1CwBTKIJ82ctnvwuLFKZxDGvQmawdzLHkY,2575
|
|
10
|
-
pythonic_fp_splitends-1.0.1.dist-info/RECORD,,
|
|
File without changes
|
{pythonic_fp_splitends-1.0.1.dist-info → pythonic_fp_splitends-2.0.0.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|