Revision | d3beb8e84c190c79096a558af465966036573b64 (tree) |
---|---|
Time | 2022-06-23 22:24:31 |
Author | Antoon Pardon <aej@pard...> |
Commiter | Antoon Pardon |
Select iterator added
@@ -1,18 +1,6 @@ | ||
1 | 1 | #!/bin/bash |
2 | 2 | |
3 | 3 | set -e |
4 | -set -x | |
5 | - | |
6 | -echo "${0%/*}" | |
7 | -pushd "${0%/*}" | |
8 | -PROJECTDIRECTORY=$(pwd) | |
9 | -popd | |
10 | - | |
11 | -PATH=$PATH:$PROJECTDIRECTORY/bin | |
12 | - | |
13 | -make all | |
14 | - | |
15 | -cd $PROJECTDIRECTORY | |
16 | 4 | |
17 | 5 | ENVIRONMENT=conda3.10 |
18 | 6 | PYTHONPATH=$(type -p python3) |
@@ -27,6 +15,17 @@ | ||
27 | 15 | exit 1 |
28 | 16 | esac |
29 | 17 | |
18 | +echo "${0%/*}" | |
19 | +pushd "${0%/*}" > /dev/null | |
20 | +PROJECTDIRECTORY=$(pwd) | |
21 | +popd > /dev/null | |
30 | 22 | |
31 | -pushd tests; make all; popd | |
32 | -pushd source; make all; popd | |
23 | +PATH=$PATH:$PROJECTDIRECTORY/bin | |
24 | + | |
25 | +make all | |
26 | + | |
27 | +cd $PROJECTDIRECTORY | |
28 | + | |
29 | + | |
30 | +pushd tests > /dev/null; make all; popd > /dev/null | |
31 | +pushd source > /dev/null; make all; popd > /dev/null |
@@ -98,5 +98,14 @@ | ||
98 | 98 | # [[1, 1], [2, 3, 5], [8]] | concat -> 1 1 2 3 5 8 |
99 | 99 | # """ |
100 | 100 | |
101 | -concat = cast(Callable[ [ Iterable[Any] ], Iterable[Any] ], Apply(chain.from_iterable)) | |
101 | +#concat = cast(Callable[ [ Iterable[Any] ], Iterable[Any] ], Apply(chain.from_iterable)) | |
102 | 102 | concat = Apply(chain.from_iterable) # type: ignore |
103 | + | |
104 | +class Select(PipeIterator): | |
105 | + """ | |
106 | + This class produces a pipe iterator that only lets items through that are | |
107 | + selected through the condition. | |
108 | + """ | |
109 | + # pylint: disable=too-few-public-methods | |
110 | + def __init__(self, condition: Callable[[Iterator[Any]], Any] ): | |
111 | + super().__init__(partial(filter, condition)) |
@@ -1,3 +1,3 @@ | ||
1 | -version = '0.01.00j' | |
2 | -modified = '2022-06-22 @ 22:27:11' | |
1 | +version = '0.01.00k' | |
2 | +modified = '2022-06-23 @ 15:23:41' | |
3 | 3 | installed = '*********************' |
@@ -5,7 +5,7 @@ | ||
5 | 5 | from typing import List |
6 | 6 | |
7 | 7 | # pylint: disable=unused-import |
8 | -from AGPlib.iterators import PipeIterator, Apply, Map, concat, SubMap | |
8 | +from AGPlib.iterators import PipeIterator, Apply, Map, concat, SubMap, Select | |
9 | 9 | # pylint: enable=unused-import |
10 | 10 | |
11 | 11 | def shuffled_range_list(*args) -> List[int]: |
@@ -30,6 +30,9 @@ | ||
30 | 30 | def is_odd(nr: int) -> bool: |
31 | 31 | return bool(nr % 2) |
32 | 32 | |
33 | +def is_larger_than_50(nr: int) -> bool: | |
34 | + return nr > 50 | |
35 | + | |
33 | 36 | def collatz(nr: int) -> int: |
34 | 37 | return 3 * nr + 1 if is_odd(nr) else nr // 2 |
35 | 38 |
@@ -82,4 +85,15 @@ | ||
82 | 85 | s.assertEqual(result0, result1) |
83 | 86 | s.assertEqual(result0, result2) |
84 | 87 | |
88 | + def test_select(s): | |
89 | + # pylint: disable=redefined-outer-name | |
90 | + List = Apply(list) | |
91 | + source = shuffled_range_list(100) | |
92 | + for condition in [is_odd, is_larger_than_50]: | |
93 | + result0 = [nr for nr in source if condition(nr)] | |
94 | + result1 = source | Select(condition) | List | |
95 | + result2 = source >> Select(condition) >> List | |
96 | + s.assertEqual(result0, result1) | |
97 | + s.assertEqual(result0, result2) | |
98 | + | |
85 | 99 | unitmain() |