pyfun-zeromode 1.0.0__tar.gz → 1.2.0__tar.gz
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.
- {pyfun_zeromode-1.0.0 → pyfun_zeromode-1.2.0}/PKG-INFO +1 -1
- {pyfun_zeromode-1.0.0 → pyfun_zeromode-1.2.0}/pyproject.toml +7 -1
- {pyfun_zeromode-1.0.0 → pyfun_zeromode-1.2.0}/pyproject.toml.orig +7 -1
- pyfun_zeromode-1.2.0/src/pyfun/__init__.py +5 -0
- pyfun_zeromode-1.2.0/src/pyfun/iter.py +109 -0
- {pyfun_zeromode-1.0.0 → pyfun_zeromode-1.2.0}/src/pyfun/maybe.py +34 -1
- pyfun_zeromode-1.0.0/src/pyfun/__init__.py +0 -4
- {pyfun_zeromode-1.0.0 → pyfun_zeromode-1.2.0}/README.md +0 -0
- {pyfun_zeromode-1.0.0 → pyfun_zeromode-1.2.0}/src/pyfun/pipe.py +0 -0
- {pyfun_zeromode-1.0.0 → pyfun_zeromode-1.2.0}/src/pyfun/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "pyfun-zeromode"
|
|
3
|
-
version = "1.
|
|
3
|
+
version = "1.2.0"
|
|
4
4
|
description = "Functional programming constructs (Maybe, Pipe) for Python"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
license = "MIT"
|
|
@@ -20,3 +20,9 @@ build-backend = "uv_build"
|
|
|
20
20
|
|
|
21
21
|
[tool.uv.build-backend]
|
|
22
22
|
module-name = "pyfun"
|
|
23
|
+
|
|
24
|
+
[dependency-groups]
|
|
25
|
+
dev = [
|
|
26
|
+
"ipykernel>=7.4.0",
|
|
27
|
+
"pytest>=9.2.0",
|
|
28
|
+
]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "pyfun-zeromode"
|
|
3
|
-
version = "1.
|
|
3
|
+
version = "1.2.0"
|
|
4
4
|
description = "Functional programming constructs (Maybe, Pipe) for Python"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
license = "MIT"
|
|
@@ -21,3 +21,9 @@ build-backend = "uv_build"
|
|
|
21
21
|
|
|
22
22
|
[tool.uv.build-backend]
|
|
23
23
|
module-name = "pyfun"
|
|
24
|
+
|
|
25
|
+
[dependency-groups]
|
|
26
|
+
dev = [
|
|
27
|
+
"ipykernel>=7.4.0",
|
|
28
|
+
"pytest>=9.2.0",
|
|
29
|
+
]
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import functools
|
|
2
|
+
from collections.abc import Callable, Iterable, Iterator
|
|
3
|
+
from itertools import islice
|
|
4
|
+
from typing import Any, overload
|
|
5
|
+
|
|
6
|
+
from pyfun.maybe import Maybe, Nothing, Some
|
|
7
|
+
|
|
8
|
+
# Marks a missing initial value, since None is a valid initial value.
|
|
9
|
+
_MISSING: Any = object()
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Iter[T]:
|
|
13
|
+
"""A lazy, chainable wrapper around an iterable.
|
|
14
|
+
|
|
15
|
+
Methods that return an Iter do no work until the result is consumed, for
|
|
16
|
+
example by to_list() or first(). Like any iterator, an Iter can only be
|
|
17
|
+
consumed once.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
__slots__ = ("_it",)
|
|
21
|
+
|
|
22
|
+
def __init__(self, items: Iterable[T]) -> None:
|
|
23
|
+
self._it: Iterator[T] = iter(items)
|
|
24
|
+
|
|
25
|
+
def __iter__(self) -> Iterator[T]:
|
|
26
|
+
return self._it
|
|
27
|
+
|
|
28
|
+
# --- Lazy transformations ---
|
|
29
|
+
|
|
30
|
+
def map[U](self, func: Callable[[T], U]) -> Iter[U]:
|
|
31
|
+
"""Applies func to each item."""
|
|
32
|
+
return Iter(map(func, self._it))
|
|
33
|
+
|
|
34
|
+
def filter(self, pred: Callable[[T], bool]) -> Iter[T]:
|
|
35
|
+
"""Keeps the items for which pred returns True."""
|
|
36
|
+
return Iter(filter(pred, self._it))
|
|
37
|
+
|
|
38
|
+
def take(self, n: int) -> Iter[T]:
|
|
39
|
+
"""Keeps at most the first n items."""
|
|
40
|
+
return Iter(islice(self._it, n))
|
|
41
|
+
|
|
42
|
+
def filter_map[U](self, func: Callable[[T], Maybe[U]]) -> Iter[U]:
|
|
43
|
+
"""Applies func to each item, keeping the values of the Somes.
|
|
44
|
+
|
|
45
|
+
Items for which func returns Nothing() are dropped.
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
def values() -> Iterator[U]:
|
|
49
|
+
for item in self._it:
|
|
50
|
+
match func(item):
|
|
51
|
+
case Some(value):
|
|
52
|
+
yield value
|
|
53
|
+
case Nothing():
|
|
54
|
+
pass
|
|
55
|
+
|
|
56
|
+
return Iter(values())
|
|
57
|
+
|
|
58
|
+
# --- Consuming operations ---
|
|
59
|
+
|
|
60
|
+
def to_list(self) -> list[T]:
|
|
61
|
+
"""Returns the items as a list."""
|
|
62
|
+
return list(self._it)
|
|
63
|
+
|
|
64
|
+
def first(self) -> Maybe[T]:
|
|
65
|
+
"""Returns the first item, or Nothing() if there are none."""
|
|
66
|
+
for item in self._it:
|
|
67
|
+
return Some(item)
|
|
68
|
+
return Nothing()
|
|
69
|
+
|
|
70
|
+
def find(self, pred: Callable[[T], bool]) -> Maybe[T]:
|
|
71
|
+
"""Returns the first item for which pred returns True, or Nothing()."""
|
|
72
|
+
return self.filter(pred).first()
|
|
73
|
+
|
|
74
|
+
@overload
|
|
75
|
+
def reduce(self, func: Callable[[T, T], T]) -> Maybe[T]: ...
|
|
76
|
+
|
|
77
|
+
@overload
|
|
78
|
+
def reduce[U](self, func: Callable[[U, T], U], initial: U) -> U: ...
|
|
79
|
+
|
|
80
|
+
def reduce(
|
|
81
|
+
self, func: Callable[[Any, T], Any], initial: Any = _MISSING
|
|
82
|
+
) -> Any:
|
|
83
|
+
"""Combines the items into one value, like functools.reduce.
|
|
84
|
+
|
|
85
|
+
func is called as func(accumulated, item). With an initial value, it
|
|
86
|
+
starts from initial and returns the result directly, which is initial
|
|
87
|
+
itself if there are no items. Without one, it starts from the first
|
|
88
|
+
item, so it returns Some(result), or Nothing() if there are no items.
|
|
89
|
+
"""
|
|
90
|
+
if initial is not _MISSING:
|
|
91
|
+
return functools.reduce(func, self._it, initial)
|
|
92
|
+
for first in self._it:
|
|
93
|
+
return Some(functools.reduce(func, self._it, first))
|
|
94
|
+
return Nothing[T]()
|
|
95
|
+
|
|
96
|
+
def traverse[U](self, func: Callable[[T], Maybe[U]]) -> Maybe[list[U]]:
|
|
97
|
+
"""Applies func to each item, succeeding only if every call does.
|
|
98
|
+
|
|
99
|
+
Returns Some with the list of values, or Nothing() as soon as any call
|
|
100
|
+
returns Nothing(). Later items are not processed.
|
|
101
|
+
"""
|
|
102
|
+
values: list[U] = []
|
|
103
|
+
for item in self._it:
|
|
104
|
+
match func(item):
|
|
105
|
+
case Some(value):
|
|
106
|
+
values.append(value)
|
|
107
|
+
case Nothing():
|
|
108
|
+
return Nothing()
|
|
109
|
+
return Some(values)
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
from dataclasses import dataclass
|
|
2
|
-
from
|
|
2
|
+
from functools import wraps
|
|
3
|
+
from typing import Callable, final
|
|
3
4
|
|
|
4
5
|
|
|
6
|
+
@final
|
|
5
7
|
@dataclass(frozen=True, slots=True)
|
|
6
8
|
class Some[T]:
|
|
7
9
|
value: T
|
|
@@ -15,6 +17,10 @@ class Some[T]:
|
|
|
15
17
|
def then[U](self, func: Callable[[], Maybe[U]]) -> Maybe[U]:
|
|
16
18
|
return func()
|
|
17
19
|
|
|
20
|
+
def filter(self, pred: Callable[[T], bool]) -> Maybe[T]:
|
|
21
|
+
"""Keeps this value if pred returns True for it, otherwise gives Nothing()."""
|
|
22
|
+
return self if pred(self.value) else Nothing[T]()
|
|
23
|
+
|
|
18
24
|
def value_or(self, default: T) -> T:
|
|
19
25
|
return self.value
|
|
20
26
|
|
|
@@ -22,6 +28,7 @@ class Some[T]:
|
|
|
22
28
|
return self.value
|
|
23
29
|
|
|
24
30
|
|
|
31
|
+
@final
|
|
25
32
|
@dataclass(frozen=True, slots=True)
|
|
26
33
|
class Nothing[T]:
|
|
27
34
|
def map[U](self, func: Callable[[T], U]) -> Nothing[U]:
|
|
@@ -33,6 +40,10 @@ class Nothing[T]:
|
|
|
33
40
|
def then[U](self, func: Callable[[], Maybe[U]]) -> Nothing[U]:
|
|
34
41
|
return Nothing()
|
|
35
42
|
|
|
43
|
+
def filter(self, pred: Callable[[T], bool]) -> Nothing[T]:
|
|
44
|
+
"""Returns Nothing() without calling pred."""
|
|
45
|
+
return self
|
|
46
|
+
|
|
36
47
|
def value_or(self, default: T) -> T:
|
|
37
48
|
return default
|
|
38
49
|
|
|
@@ -49,3 +60,25 @@ def maybe[T](optional: T | None) -> Maybe[T]:
|
|
|
49
60
|
|
|
50
61
|
def ensure(cond: bool) -> Maybe[bool]:
|
|
51
62
|
return Some(True) if cond else Nothing()
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def wrap_exc[**P, R](
|
|
66
|
+
func: Callable[P, R],
|
|
67
|
+
exception: type[Exception],
|
|
68
|
+
*others: type[Exception],
|
|
69
|
+
) -> Callable[P, Maybe[R]]:
|
|
70
|
+
"""Returns a version of func that gives Nothing() instead of raising.
|
|
71
|
+
|
|
72
|
+
Only the listed exception types are caught, so any other exception still
|
|
73
|
+
propagates. A successful result is wrapped in Some.
|
|
74
|
+
"""
|
|
75
|
+
caught = (exception, *others)
|
|
76
|
+
|
|
77
|
+
@wraps(func)
|
|
78
|
+
def wrapper(*args: P.args, **kwargs: P.kwargs) -> Maybe[R]:
|
|
79
|
+
try:
|
|
80
|
+
return Some(func(*args, **kwargs))
|
|
81
|
+
except caught:
|
|
82
|
+
return Nothing()
|
|
83
|
+
|
|
84
|
+
return wrapper
|
|
File without changes
|
|
File without changes
|
|
File without changes
|