veridret-plugins-core 1__tar.gz → 2__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.
- {veridret_plugins_core-1 → veridret_plugins_core-2}/PKG-INFO +3 -3
- {veridret_plugins_core-1 → veridret_plugins_core-2}/README.md +1 -1
- {veridret_plugins_core-1 → veridret_plugins_core-2}/pyproject.toml +5 -1
- veridret_plugins_core-2/src/veridret_plugins/core/__init__.py +17 -0
- {veridret_plugins_core-1 → veridret_plugins_core-2}/src/veridret_plugins/core/_plugin.py +3 -3
- veridret_plugins_core-2/src/veridret_plugins/core/comparison/__init__.py +10 -0
- {veridret_plugins_core-1/src/veridret_plugins/core/cmptools → veridret_plugins_core-2/src/veridret_plugins/core/comparison}/reverse.py +3 -3
- {veridret_plugins_core-1/src/veridret_plugins/core/cmptools → veridret_plugins_core-2/src/veridret_plugins/core/comparison}/unordered.py +3 -3
- veridret_plugins_core-2/src/veridret_plugins/core/function/__init__.py +16 -0
- veridret_plugins_core-1/src/veridret_plugins/core/functools/call.py → veridret_plugins_core-2/src/veridret_plugins/core/function/apply.py +11 -11
- veridret_plugins_core-2/src/veridret_plugins/core/function/apply_right.py +65 -0
- {veridret_plugins_core-1/src/veridret_plugins/core/functools → veridret_plugins_core-2/src/veridret_plugins/core/function}/compose_left.py +4 -4
- {veridret_plugins_core-1/src/veridret_plugins/core/functools → veridret_plugins_core-2/src/veridret_plugins/core/function}/compose_right.py +4 -4
- veridret_plugins_core-1/src/veridret_plugins/core/call.py → veridret_plugins_core-2/src/veridret_plugins/core/function/evaluate_application.py +10 -7
- {veridret_plugins_core-1/src/veridret_plugins/core/functools → veridret_plugins_core-2/src/veridret_plugins/core/function}/identity.py +3 -3
- {veridret_plugins_core-1/src/veridret_plugins/core/itertools → veridret_plugins_core-2/src/veridret_plugins/core/iterable}/__init__.py +4 -2
- veridret_plugins_core-1/src/veridret_plugins/core/iterator.py → veridret_plugins_core-2/src/veridret_plugins/core/iterable/construct.py +10 -12
- {veridret_plugins_core-1/src/veridret_plugins/core/itertools → veridret_plugins_core-2/src/veridret_plugins/core/iterable}/filter.py +3 -3
- {veridret_plugins_core-1/src/veridret_plugins/core/itertools → veridret_plugins_core-2/src/veridret_plugins/core/iterable}/fold_left.py +11 -10
- {veridret_plugins_core-1/src/veridret_plugins/core/itertools → veridret_plugins_core-2/src/veridret_plugins/core/iterable}/map.py +3 -3
- veridret_plugins_core-2/src/veridret_plugins/core/iterable/reduce_left.py +89 -0
- veridret_plugins_core-1/src/veridret_plugins/core/__init__.py +0 -11
- veridret_plugins_core-1/src/veridret_plugins/core/cmptools/__init__.py +0 -8
- veridret_plugins_core-1/src/veridret_plugins/core/functools/__init__.py +0 -8
- veridret_plugins_core-1/src/veridret_plugins/core/functools/call_right.py +0 -65
- {veridret_plugins_core-1 → veridret_plugins_core-2}/.gitignore +0 -0
- {veridret_plugins_core-1 → veridret_plugins_core-2}/LICENSE.md +0 -0
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: veridret-plugins-core
|
|
3
|
-
Version:
|
|
3
|
+
Version: 2
|
|
4
4
|
Import-Name: veridret_plugins.core
|
|
5
5
|
Import-Namespace: veridret_plugins
|
|
6
|
-
Summary: The *official* Python package of the *official* standard
|
|
6
|
+
Summary: The *official* Python package of the core *official* standard VerIdRet plugins
|
|
7
7
|
Project-URL: homepage, https://codeberg.org/jonathan-gruber-jg/veridret-plugins-core.git
|
|
8
8
|
Project-URL: source, https://codeberg.org/jonathan-gruber-jg/veridret-plugins-core.git
|
|
9
9
|
Author-email: Jonathan Gruber <jonathan.gruber.jg@gmail.com>
|
|
@@ -17,4 +17,4 @@ Description-Content-Type: text/markdown
|
|
|
17
17
|
<!-- SPDX-License-Identifier: BSD-3-Clause -->
|
|
18
18
|
|
|
19
19
|
`veridret-plugins-core` is the *official* Python package
|
|
20
|
-
of the *official* standard
|
|
20
|
+
of the core *official* standard VerIdRet plugins.
|
|
@@ -13,7 +13,7 @@ dynamic = [
|
|
|
13
13
|
]
|
|
14
14
|
description = """\
|
|
15
15
|
The *official* Python package \
|
|
16
|
-
of the *official* standard
|
|
16
|
+
of the core *official* standard VerIdRet plugins\
|
|
17
17
|
"""
|
|
18
18
|
readme = 'README.md'
|
|
19
19
|
license = 'BSD-3-Clause'
|
|
@@ -47,6 +47,10 @@ packages = [
|
|
|
47
47
|
|
|
48
48
|
[tool.hatch.version]
|
|
49
49
|
path = 'src/veridret_plugins/core/__init__.py'
|
|
50
|
+
pattern = '''(?x)
|
|
51
|
+
^\s*__version__\s*:\s*Final\s*\[\s*LiteralString\s*\]\s*=\s*
|
|
52
|
+
(?P<q>['"])(?P<version>.*?)(?P=q)
|
|
53
|
+
'''
|
|
50
54
|
|
|
51
55
|
[tool.pylint.main]
|
|
52
56
|
disable = [
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
2
|
+
|
|
3
|
+
"""
|
|
4
|
+
The *official* Python package
|
|
5
|
+
of the core *official* standard VerIdRet plugins.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from typing import Final, LiteralString
|
|
9
|
+
|
|
10
|
+
__all__: Final[tuple[LiteralString, ...]] = (
|
|
11
|
+
'__version__',
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
__version__: Final[LiteralString] = '2'
|
|
15
|
+
"""
|
|
16
|
+
The version of the package.
|
|
17
|
+
"""
|
|
@@ -4,17 +4,17 @@
|
|
|
4
4
|
Plugin utilities.
|
|
5
5
|
"""
|
|
6
6
|
|
|
7
|
-
from typing import Annotated, Any, ClassVar
|
|
7
|
+
from typing import Annotated, Any, ClassVar, Final, LiteralString
|
|
8
8
|
|
|
9
9
|
import pydantic
|
|
10
10
|
|
|
11
11
|
import veridret.plugin
|
|
12
12
|
|
|
13
|
-
__all__ =
|
|
13
|
+
__all__: Final[tuple[LiteralString, ...]] = (
|
|
14
14
|
'AnnotatedCall',
|
|
15
15
|
'Call',
|
|
16
16
|
'evaluate_call',
|
|
17
|
-
|
|
17
|
+
)
|
|
18
18
|
|
|
19
19
|
class Call(pydantic.BaseModel):
|
|
20
20
|
"""
|
|
@@ -8,12 +8,12 @@ from collections.abc import Callable
|
|
|
8
8
|
import dataclasses
|
|
9
9
|
import operator
|
|
10
10
|
import types
|
|
11
|
-
from typing import Any, final, Never, override
|
|
11
|
+
from typing import Any, Final, final, LiteralString, Never, override
|
|
12
12
|
|
|
13
|
-
__all__ =
|
|
13
|
+
__all__: Final[tuple[LiteralString, ...]] = (
|
|
14
14
|
'call',
|
|
15
15
|
'Reverse',
|
|
16
|
-
|
|
16
|
+
)
|
|
17
17
|
|
|
18
18
|
@final
|
|
19
19
|
@dataclasses.dataclass(frozen=True)
|
|
@@ -7,12 +7,12 @@ upon an object.
|
|
|
7
7
|
"""
|
|
8
8
|
|
|
9
9
|
import dataclasses
|
|
10
|
-
from typing import Any, final, Never, override
|
|
10
|
+
from typing import Any, Final, final, LiteralString, Never, override
|
|
11
11
|
|
|
12
|
-
__all__ =
|
|
12
|
+
__all__: Final[tuple[LiteralString, ...]] = (
|
|
13
13
|
'call',
|
|
14
14
|
'Unordered',
|
|
15
|
-
|
|
15
|
+
)
|
|
16
16
|
|
|
17
17
|
@final
|
|
18
18
|
@dataclasses.dataclass(frozen=True)
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
2
|
+
|
|
3
|
+
"""
|
|
4
|
+
Utilities for manipulating functions.
|
|
5
|
+
|
|
6
|
+
The functions with which :mod:`veridret_plugins.core` is concerned
|
|
7
|
+
are chiefly unary functions,
|
|
8
|
+
i.e.,
|
|
9
|
+
those matching ``Callable[[T], U]`` for some ``T`` and ``U``;
|
|
10
|
+
multiparameter functions are substituted for by equivalent curried functions.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from typing import Final, LiteralString
|
|
14
|
+
|
|
15
|
+
__all__: Final[tuple[LiteralString, ...]] = (
|
|
16
|
+
)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# SPDX-License-Identifier: BSD-3-Clause
|
|
2
2
|
|
|
3
3
|
"""
|
|
4
|
-
The function
|
|
4
|
+
The function application operator.
|
|
5
5
|
|
|
6
6
|
The operator is in fact the identity function over the functions;
|
|
7
7
|
letting ``op`` denote the operator,
|
|
@@ -9,35 +9,35 @@ we have ``op f x = (op f) x = f x``.
|
|
|
9
9
|
"""
|
|
10
10
|
|
|
11
11
|
from collections.abc import Callable
|
|
12
|
-
from typing import Any, Protocol
|
|
12
|
+
from typing import Any, Final, LiteralString, Protocol
|
|
13
13
|
|
|
14
|
-
__all__ =
|
|
15
|
-
'
|
|
14
|
+
__all__: Final[tuple[LiteralString, ...]] = (
|
|
15
|
+
'Apply',
|
|
16
16
|
'call',
|
|
17
|
-
|
|
17
|
+
)
|
|
18
18
|
|
|
19
|
-
class
|
|
19
|
+
class Apply(Protocol):
|
|
20
20
|
"""
|
|
21
|
-
A protocol for the function
|
|
21
|
+
A protocol for the function application operator.
|
|
22
22
|
"""
|
|
23
23
|
|
|
24
24
|
def __call__[F: Callable[[Any], Any]](self, obj: F, /) -> F:
|
|
25
25
|
...
|
|
26
26
|
|
|
27
|
-
def
|
|
27
|
+
def _apply[F: Callable[[Any], Any]](func: F, /) -> F:
|
|
28
28
|
return func
|
|
29
29
|
|
|
30
|
-
def call(args: dict[str, object]) ->
|
|
30
|
+
def call(args: dict[str, object]) -> Apply:
|
|
31
31
|
"""
|
|
32
32
|
Call the plugin.
|
|
33
33
|
|
|
34
34
|
:param args:
|
|
35
35
|
A dictionary of the plugin's arguments;
|
|
36
36
|
unused.
|
|
37
|
-
:return: The function
|
|
37
|
+
:return: The function application operator.
|
|
38
38
|
"""
|
|
39
39
|
|
|
40
40
|
# Unused.
|
|
41
41
|
del args
|
|
42
42
|
|
|
43
|
-
return
|
|
43
|
+
return _apply
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
2
|
+
|
|
3
|
+
"""
|
|
4
|
+
The reverse or rightist function application operator.
|
|
5
|
+
|
|
6
|
+
The operator is in fact the normal function application operator
|
|
7
|
+
with the first two parameters switched;
|
|
8
|
+
letting ``op`` denote the operator,
|
|
9
|
+
we have ``op x f = f x``.
|
|
10
|
+
Since,
|
|
11
|
+
in an expression like ``operator a b``,
|
|
12
|
+
the normal function application operator calls
|
|
13
|
+
the *left* operand ``a`` upon the right operand ``b``
|
|
14
|
+
while the reverse function application operator calls
|
|
15
|
+
the *right* operand ``b`` upon the left operand ``a``,
|
|
16
|
+
we refer to the latter operator as also
|
|
17
|
+
the *right*\\ ist function application operator.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from collections.abc import Callable
|
|
21
|
+
from typing import Final, LiteralString, Protocol
|
|
22
|
+
|
|
23
|
+
__all__: Final[tuple[LiteralString, ...]] = (
|
|
24
|
+
'ApplyRight',
|
|
25
|
+
'ApplyRightOne',
|
|
26
|
+
'call',
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
class ApplyRightOne[T](Protocol):
|
|
30
|
+
"""
|
|
31
|
+
A protocol for the reverse function application operator
|
|
32
|
+
with the first parameter bound to an object of type ``T``.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def __call__[U](self, func: Callable[[T], U], /) -> U:
|
|
36
|
+
...
|
|
37
|
+
|
|
38
|
+
class ApplyRight(Protocol):
|
|
39
|
+
"""
|
|
40
|
+
A protocol for the reverse function application operator.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
def __call__[T](self, obj: T, /) -> ApplyRightOne[T]:
|
|
44
|
+
...
|
|
45
|
+
|
|
46
|
+
def _apply_right[T](obj: T, /) -> ApplyRightOne[T]:
|
|
47
|
+
def apply_right_one[U](func: Callable[[T], U], /) -> U:
|
|
48
|
+
return func(obj)
|
|
49
|
+
|
|
50
|
+
return apply_right_one
|
|
51
|
+
|
|
52
|
+
def call(args: dict[str, object]) -> ApplyRight:
|
|
53
|
+
"""
|
|
54
|
+
Call the plugin.
|
|
55
|
+
|
|
56
|
+
:param args:
|
|
57
|
+
A dictionary of the plugin's arguments;
|
|
58
|
+
unused.
|
|
59
|
+
:return: The reverse function application operator.
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
# Unused.
|
|
63
|
+
del args
|
|
64
|
+
|
|
65
|
+
return _apply_right
|
|
@@ -14,18 +14,18 @@ we call the operator the *left*\\ ist function composition operator.
|
|
|
14
14
|
"""
|
|
15
15
|
|
|
16
16
|
from collections.abc import Callable
|
|
17
|
-
from typing import Protocol
|
|
17
|
+
from typing import Final, LiteralString, Protocol
|
|
18
18
|
|
|
19
|
-
__all__ =
|
|
19
|
+
__all__: Final[tuple[LiteralString, ...]] = (
|
|
20
20
|
'call',
|
|
21
21
|
'ComposeLeft',
|
|
22
22
|
'ComposeLeftOne',
|
|
23
|
-
|
|
23
|
+
)
|
|
24
24
|
|
|
25
25
|
class ComposeLeftOne[T, U](Protocol):
|
|
26
26
|
"""
|
|
27
27
|
A protocol for the leftist function composition operator
|
|
28
|
-
with the first
|
|
28
|
+
with the first parameter bound to a function of type ``T -> U``.
|
|
29
29
|
"""
|
|
30
30
|
|
|
31
31
|
def __call__[V](self, func: Callable[[U], V], /) -> Callable[[T], V]:
|
|
@@ -14,18 +14,18 @@ we call the operator the *right*\\ ist function composition operator.
|
|
|
14
14
|
"""
|
|
15
15
|
|
|
16
16
|
from collections.abc import Callable
|
|
17
|
-
from typing import Protocol
|
|
17
|
+
from typing import Final, LiteralString, Protocol
|
|
18
18
|
|
|
19
|
-
__all__ =
|
|
19
|
+
__all__: Final[tuple[LiteralString, ...]] = (
|
|
20
20
|
'call',
|
|
21
21
|
'ComposeRight',
|
|
22
22
|
'ComposeRightOne',
|
|
23
|
-
|
|
23
|
+
)
|
|
24
24
|
|
|
25
25
|
class ComposeRightOne[T, U](Protocol):
|
|
26
26
|
"""
|
|
27
27
|
A protocol for the rightist function composition operator
|
|
28
|
-
with the first
|
|
28
|
+
with the first parameter bound to a function of type ``T -> U``.
|
|
29
29
|
"""
|
|
30
30
|
|
|
31
31
|
def __call__[V](self, func: Callable[[V], T], /) -> Callable[[V], U]:
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# SPDX-License-Identifier: BSD-3-Clause
|
|
2
2
|
|
|
3
3
|
"""
|
|
4
|
-
Evaluate a
|
|
4
|
+
Evaluate a function application expression.
|
|
5
5
|
|
|
6
6
|
In particular,
|
|
7
7
|
the plugin evaluates ``id a[1] a[2] ... a[n]``,
|
|
@@ -11,15 +11,15 @@ and each operand ``a[i]`` is the result of a plugin call.
|
|
|
11
11
|
|
|
12
12
|
from collections.abc import Callable
|
|
13
13
|
import functools
|
|
14
|
-
from typing import Any, cast, overload
|
|
14
|
+
from typing import Any, cast, Final, LiteralString, overload
|
|
15
15
|
|
|
16
16
|
import pydantic
|
|
17
17
|
|
|
18
18
|
from veridret_plugins.core import _plugin
|
|
19
19
|
|
|
20
|
-
__all__ =
|
|
20
|
+
__all__: Final[tuple[LiteralString, ...]] = (
|
|
21
21
|
'call',
|
|
22
|
-
|
|
22
|
+
)
|
|
23
23
|
|
|
24
24
|
class _Args(pydantic.BaseModel):
|
|
25
25
|
operands: tuple[_plugin.AnnotatedCall, ...] = pydantic.Field(
|
|
@@ -45,14 +45,17 @@ def call(args: dict[str, object]) -> Any:
|
|
|
45
45
|
Call the plugin.
|
|
46
46
|
|
|
47
47
|
:param args: A dictionary of the plugin's arguments.
|
|
48
|
-
:return:
|
|
48
|
+
:return:
|
|
49
|
+
The value
|
|
50
|
+
of the function application expression specified by ``args``.
|
|
49
51
|
:raise Exception:
|
|
50
52
|
If ``args`` is invalid,
|
|
51
53
|
evaluating any of the plugin calls
|
|
52
|
-
that evaluate to the operands
|
|
54
|
+
that evaluate to the operands
|
|
55
|
+
of the function application expression
|
|
53
56
|
fails,
|
|
54
57
|
or any of the individual calls
|
|
55
|
-
made in evaluating the whole
|
|
58
|
+
made in evaluating the whole function application expression
|
|
56
59
|
fails.
|
|
57
60
|
"""
|
|
58
61
|
|
|
@@ -4,12 +4,12 @@
|
|
|
4
4
|
The identity function.
|
|
5
5
|
"""
|
|
6
6
|
|
|
7
|
-
from typing import Protocol
|
|
7
|
+
from typing import Final, LiteralString, Protocol
|
|
8
8
|
|
|
9
|
-
__all__ =
|
|
9
|
+
__all__: Final[tuple[LiteralString, ...]] = (
|
|
10
10
|
'call',
|
|
11
11
|
'Identity',
|
|
12
|
-
|
|
12
|
+
)
|
|
13
13
|
|
|
14
14
|
class Identity(Protocol):
|
|
15
15
|
"""
|
|
@@ -8,18 +8,24 @@ the plugin constructs an iterator
|
|
|
8
8
|
over the sequence ``(a[0], a[1], ..., a[n])``,
|
|
9
9
|
where each ``a[i]`` is the result of a plugin call
|
|
10
10
|
and is computed only once the iterator is made to yield it.
|
|
11
|
+
As a result,
|
|
12
|
+
if computing an ``a[i]``,
|
|
13
|
+
i.e.,
|
|
14
|
+
evaluating the corresponding plugin call,
|
|
15
|
+
fails,
|
|
16
|
+
then attempting to yield ``a[i]`` raises an exception.
|
|
11
17
|
"""
|
|
12
18
|
|
|
13
19
|
from collections.abc import Iterator
|
|
14
|
-
from typing import Any
|
|
20
|
+
from typing import Any, Final, LiteralString
|
|
15
21
|
|
|
16
22
|
import pydantic
|
|
17
23
|
|
|
18
24
|
from veridret_plugins.core import _plugin
|
|
19
25
|
|
|
20
|
-
__all__ =
|
|
26
|
+
__all__: Final[tuple[LiteralString, ...]] = (
|
|
21
27
|
'call',
|
|
22
|
-
|
|
28
|
+
)
|
|
23
29
|
|
|
24
30
|
class _Args(pydantic.BaseModel):
|
|
25
31
|
operands: tuple[_plugin.AnnotatedCall, ...] = pydantic.Field(
|
|
@@ -34,15 +40,7 @@ def call(args: dict[str, object]) -> Iterator[Any]:
|
|
|
34
40
|
|
|
35
41
|
:param args: A dictionary of the plugin's arguments.
|
|
36
42
|
:return: An iterator of the sequence specified by ``args``.
|
|
37
|
-
:raise Exception:
|
|
38
|
-
If ``args`` is invalid.
|
|
39
|
-
|
|
40
|
-
Since the plugin calls that evaluate to the items yielded by the iterator
|
|
41
|
-
are each only evaluated once the iterator is made to yield them,
|
|
42
|
-
if evaluating any of those plugin calls fails,
|
|
43
|
-
then attempting to yield the item
|
|
44
|
-
to which the plugin call would have evaluated
|
|
45
|
-
raises an exception.
|
|
43
|
+
:raise Exception: If ``args`` is invalid.
|
|
46
44
|
"""
|
|
47
45
|
|
|
48
46
|
validated_args = _Args.model_validate(args, strict=True, by_name=False)
|
|
@@ -9,12 +9,12 @@ including the special case where ``p`` is ``None``.
|
|
|
9
9
|
"""
|
|
10
10
|
|
|
11
11
|
from collections.abc import Callable, Iterable, Iterator
|
|
12
|
-
from typing import Protocol
|
|
12
|
+
from typing import Final, LiteralString, Protocol
|
|
13
13
|
|
|
14
|
-
__all__ =
|
|
14
|
+
__all__: Final[tuple[LiteralString, ...]] = (
|
|
15
15
|
'call',
|
|
16
16
|
'Filter',
|
|
17
|
-
|
|
17
|
+
)
|
|
18
18
|
|
|
19
19
|
class Filter(Protocol):
|
|
20
20
|
"""
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
# SPDX-License-Identifier: BSD-3-Clause
|
|
2
2
|
|
|
3
3
|
"""
|
|
4
|
-
The left-associative variant of the ``fold`` higher-order
|
|
4
|
+
The left-associative variant of the ``fold`` higher-order function
|
|
5
|
+
over the iterables.
|
|
5
6
|
|
|
6
7
|
Letting ``fold_left`` denote the function,
|
|
7
8
|
``fold_left f z it`` is equivalent to the Python
|
|
@@ -10,17 +11,17 @@ Letting ``fold_left`` denote the function,
|
|
|
10
11
|
|
|
11
12
|
from collections.abc import Callable, Iterable
|
|
12
13
|
import functools
|
|
13
|
-
from typing import Protocol
|
|
14
|
+
from typing import Final, LiteralString, Protocol
|
|
14
15
|
|
|
15
|
-
__all__ =
|
|
16
|
+
__all__: Final[tuple[LiteralString, ...]] = (
|
|
16
17
|
'call',
|
|
17
18
|
'FoldLeft',
|
|
18
|
-
|
|
19
|
+
)
|
|
19
20
|
|
|
20
21
|
class FoldLeft(Protocol):
|
|
21
22
|
"""
|
|
22
|
-
A protocol for the left-associative variant
|
|
23
|
-
over the iterables.
|
|
23
|
+
A protocol for the left-associative variant
|
|
24
|
+
of the ``fold`` higher-order function over the iterables.
|
|
24
25
|
"""
|
|
25
26
|
|
|
26
27
|
def __call__[T, U](
|
|
@@ -31,12 +32,12 @@ class FoldLeft(Protocol):
|
|
|
31
32
|
def _fold_left[T, U](
|
|
32
33
|
func: Callable[[T], Callable[[U], T]], /,
|
|
33
34
|
) -> Callable[[T], Callable[[Iterable[U]], T]]:
|
|
34
|
-
def uncurried_func(x: T, y: U) -> T:
|
|
35
|
+
def uncurried_func(x: T, y: U, /) -> T:
|
|
35
36
|
return func(x)(y)
|
|
36
37
|
|
|
37
|
-
def fold_left_one(
|
|
38
|
+
def fold_left_one(init: T, /) -> Callable[[Iterable[U]], T]:
|
|
38
39
|
def fold_left_two(iterable: Iterable[U], /) -> T:
|
|
39
|
-
return functools.reduce(uncurried_func, iterable, initial=
|
|
40
|
+
return functools.reduce(uncurried_func, iterable, initial=init)
|
|
40
41
|
|
|
41
42
|
return fold_left_two
|
|
42
43
|
|
|
@@ -50,7 +51,7 @@ def call(args: dict[str, object]) -> FoldLeft:
|
|
|
50
51
|
A dictionary of the plugin's arguments;
|
|
51
52
|
unused.
|
|
52
53
|
:return:
|
|
53
|
-
The left-associative variant of the ``fold`` higher-order
|
|
54
|
+
The left-associative variant of the ``fold`` higher-order function
|
|
54
55
|
over the iterables.
|
|
55
56
|
"""
|
|
56
57
|
|
|
@@ -8,12 +8,12 @@ Letting ``map_`` denote the function,
|
|
|
8
8
|
"""
|
|
9
9
|
|
|
10
10
|
from collections.abc import Callable, Iterable, Iterator
|
|
11
|
-
from typing import Protocol
|
|
11
|
+
from typing import Final, LiteralString, Protocol
|
|
12
12
|
|
|
13
|
-
__all__ =
|
|
13
|
+
__all__: Final[tuple[LiteralString, ...]] = (
|
|
14
14
|
'call',
|
|
15
15
|
'Map',
|
|
16
|
-
|
|
16
|
+
)
|
|
17
17
|
|
|
18
18
|
class Map(Protocol):
|
|
19
19
|
"""
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# SPDX-License-Identifier: BSD-3-Clause
|
|
2
|
+
|
|
3
|
+
"""
|
|
4
|
+
The left-associative variant of the ``fold`` higher-order function,
|
|
5
|
+
with no initial value,
|
|
6
|
+
over the iterables.
|
|
7
|
+
|
|
8
|
+
The function raises a :exc:`ValueError` if the iterable is empty.
|
|
9
|
+
|
|
10
|
+
The "reduce" in the function's name is,
|
|
11
|
+
on the one hand,
|
|
12
|
+
meant to distinguish it from a "normal" fold,
|
|
13
|
+
i.e.,
|
|
14
|
+
one with an initial value;
|
|
15
|
+
this scheme is taken from the Rust programming language,
|
|
16
|
+
whose functions over iterators for a fold with and without an initial value
|
|
17
|
+
are respectively named ``fold`` and ``reduce``.
|
|
18
|
+
On the other hand,
|
|
19
|
+
given that the fold has no initial value,
|
|
20
|
+
its final result is ultimately based only
|
|
21
|
+
upon the combining operation and the element sequence of the iterable
|
|
22
|
+
and is of the same type as the elements of the iterable,
|
|
23
|
+
so we can regard the fold as *reducing* that element sequence
|
|
24
|
+
to a single value of the same type as the members of the sequence;
|
|
25
|
+
meanwhile,
|
|
26
|
+
the result of a "normal" fold,
|
|
27
|
+
i.e.,
|
|
28
|
+
one *with* an initial value,
|
|
29
|
+
is generally based also upon that initial value
|
|
30
|
+
and is not necessarily of the same type as the iterable's elements.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
from collections.abc import Callable, Iterable
|
|
34
|
+
import functools
|
|
35
|
+
from typing import Final, LiteralString, Protocol
|
|
36
|
+
|
|
37
|
+
__all__: Final[tuple[LiteralString, ...]] = (
|
|
38
|
+
'call',
|
|
39
|
+
'ReduceLeft',
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
class ReduceLeft(Protocol):
|
|
43
|
+
"""
|
|
44
|
+
A protocol for the left-associative variant
|
|
45
|
+
of the ``fold`` higher-order function,
|
|
46
|
+
with no initial value,
|
|
47
|
+
over the iterables.
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
def __call__[T](
|
|
51
|
+
self, func: Callable[[T], Callable[[T], T]], /,
|
|
52
|
+
) -> Callable[[Iterable[T]], T]:
|
|
53
|
+
...
|
|
54
|
+
|
|
55
|
+
def _reduce_left[T](
|
|
56
|
+
func: Callable[[T], Callable[[T], T]], /,
|
|
57
|
+
) -> Callable[[Iterable[T]], T]:
|
|
58
|
+
def uncurried_func(x: T, y: T, /) -> T:
|
|
59
|
+
return func(x)(y)
|
|
60
|
+
|
|
61
|
+
def reduce_left_one(iterable: Iterable[T], /) -> T:
|
|
62
|
+
it = iter(iterable)
|
|
63
|
+
|
|
64
|
+
try:
|
|
65
|
+
init = next(it)
|
|
66
|
+
except StopIteration as e:
|
|
67
|
+
raise ValueError('reduce_left of empty iterable') from e
|
|
68
|
+
|
|
69
|
+
return functools.reduce(uncurried_func, it, initial=init)
|
|
70
|
+
|
|
71
|
+
return reduce_left_one
|
|
72
|
+
|
|
73
|
+
def call(args: dict[str, object]) -> ReduceLeft:
|
|
74
|
+
"""
|
|
75
|
+
Call the plugin.
|
|
76
|
+
|
|
77
|
+
:param args:
|
|
78
|
+
A dictionary of the plugin's arguments;
|
|
79
|
+
unused.
|
|
80
|
+
:return:
|
|
81
|
+
The left-associative variant of the ``fold`` higher-order function,
|
|
82
|
+
with no initial value,
|
|
83
|
+
over the iterables.
|
|
84
|
+
"""
|
|
85
|
+
|
|
86
|
+
# Unused
|
|
87
|
+
del args
|
|
88
|
+
|
|
89
|
+
return _reduce_left
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
# SPDX-License-Identifier: BSD-3-Clause
|
|
2
|
-
|
|
3
|
-
"""
|
|
4
|
-
The reverse or rightist function call operator.
|
|
5
|
-
|
|
6
|
-
The operator is in fact the normal function call operator
|
|
7
|
-
with the first two arguments switched;
|
|
8
|
-
letting ``op`` denote the operator,
|
|
9
|
-
we have ``op x f = f x``.
|
|
10
|
-
Since,
|
|
11
|
-
in an expression like ``operator a b``,
|
|
12
|
-
the normal function call operator calls
|
|
13
|
-
the *left* operand ``a`` upon the right operand ``b``
|
|
14
|
-
while the reverse function call operator calls
|
|
15
|
-
the *right* operand ``b`` upon the left operand ``a``,
|
|
16
|
-
we refer to the latter operator as also
|
|
17
|
-
the *right*\\ ist function call operator.
|
|
18
|
-
"""
|
|
19
|
-
|
|
20
|
-
from collections.abc import Callable
|
|
21
|
-
from typing import Protocol
|
|
22
|
-
|
|
23
|
-
__all__ = [
|
|
24
|
-
'call',
|
|
25
|
-
'CallRight',
|
|
26
|
-
'CallRightOne'
|
|
27
|
-
]
|
|
28
|
-
|
|
29
|
-
class CallRightOne[T](Protocol):
|
|
30
|
-
"""
|
|
31
|
-
A protocol for the reverse function call operator
|
|
32
|
-
with the first argument bound to an object of type ``T``.
|
|
33
|
-
"""
|
|
34
|
-
|
|
35
|
-
def __call__[U](self, func: Callable[[T], U], /) -> U:
|
|
36
|
-
...
|
|
37
|
-
|
|
38
|
-
class CallRight(Protocol):
|
|
39
|
-
"""
|
|
40
|
-
A protocol for the reverse function call operator.
|
|
41
|
-
"""
|
|
42
|
-
|
|
43
|
-
def __call__[T](self, obj: T, /) -> CallRightOne[T]:
|
|
44
|
-
...
|
|
45
|
-
|
|
46
|
-
def _call_right[T](obj: T, /) -> CallRightOne[T]:
|
|
47
|
-
def call_right_one[U](func: Callable[[T], U], /) -> U:
|
|
48
|
-
return func(obj)
|
|
49
|
-
|
|
50
|
-
return call_right_one
|
|
51
|
-
|
|
52
|
-
def call(args: dict[str, object]) -> CallRight:
|
|
53
|
-
"""
|
|
54
|
-
Call the plugin.
|
|
55
|
-
|
|
56
|
-
:param args:
|
|
57
|
-
A dictionary of the plugin's arguments;
|
|
58
|
-
unused.
|
|
59
|
-
:return: A curried reverse unary-function-call operator.
|
|
60
|
-
"""
|
|
61
|
-
|
|
62
|
-
# Unused.
|
|
63
|
-
del args
|
|
64
|
-
|
|
65
|
-
return _call_right
|
|
File without changes
|
|
File without changes
|