Skip to content

Commit 83b3896

Browse files
Technologicatclaude
andcommitted
slicing: add Fupped tag type for fup return value
Mirrors the Sliced pattern: abstract __getitem__ declares the subscripting interface, fup1 inherits and implements it. Both Sliced and Fupped exported in __all__. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent bed42f8 commit 83b3896

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

unpythonic/slicing.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
"""Operations on sequences with native slice syntax. Syntactic sugar, pure Python."""
33

4-
__all__ = ["islice", "Sliced", "fup"]
4+
__all__ = ["islice", "Sliced", "fup", "FupTarget", "Fuppable"]
55

66
from abc import abstractmethod
77
from collections.abc import Iterable, Iterator, Sequence
@@ -113,7 +113,19 @@ def __getitem__(self, k: int | slice) -> Iterator | Any:
113113
# return first(islicef(iterable, k, k + 1))
114114
# return islice1()
115115

116-
def fup(seq: Sequence) -> Any:
116+
class Fuppable:
117+
"""Ready to be fupped. Left-shift (``<<``) with values to perform the update."""
118+
@abstractmethod
119+
def __lshift__(self, v: Any) -> Sequence:
120+
...
121+
122+
class FupTarget:
123+
"""The target sequence of a ``fup``. Subscript to select where to fup it."""
124+
@abstractmethod
125+
def __getitem__(self, k: int | slice) -> Fuppable:
126+
...
127+
128+
def fup(seq: Sequence) -> FupTarget:
117129
"""Functionally update a sequence.
118130
119131
Usage::
@@ -136,12 +148,12 @@ def fup(seq: Sequence) -> Any:
136148
Named after the sound a sequence makes when it is hit by a functional update.
137149
"""
138150
# two-phase manual curry, first expect a subscript, then an lshift.
139-
class fup1:
151+
class fup1(FupTarget):
140152
"""Subscript me to specify index or slice where to fupdate."""
141-
def __getitem__(self, k: int | slice) -> Any:
153+
def __getitem__(self, k: int | slice) -> Fuppable:
142154
if isinstance(k, tuple):
143155
raise TypeError(f"multidimensional indexing not supported, got {k}")
144-
class fup2:
156+
class fup2(Fuppable):
145157
"""Left-shift me with values to perform the fupdate."""
146158
def __lshift__(self, v: Any) -> Sequence:
147159
return fupdate(seq, k, v)

0 commit comments

Comments
 (0)