diff --git a/docs/reference/collections.md b/docs/reference/collections.md index 7b9d3183..8860018e 100644 --- a/docs/reference/collections.md +++ b/docs/reference/collections.md @@ -7,6 +7,13 @@ API. Detailed module references are available for {doc}`Seq `, {doc}`Block `, {doc}`Map `, and {doc}`TypedArray `. +For both `Block` and `TypedArray`, `take_last(0)` returns an empty collection, +while `skip_last(0)` preserves all elements. For example, +`list(Block.of(1, 2, 3).take_last(0))` is `[]`. + +`AsyncSeq.take(n)` consumes at most `n` source items. Taking zero items does not +advance the source, and completing the result does not request an extra item. + ## Additional modules ```{eval-rst} diff --git a/docs/reference/extras.md b/docs/reference/extras.md index ba854e3c..a72389cf 100644 --- a/docs/reference/extras.md +++ b/docs/reference/extras.md @@ -2,6 +2,12 @@ These optional helpers build on the core types. +Parser repetition (`many`, `many1`, and separator combinators) processes long inputs +without adding a Python stack frame for each match. Repeated parsers must consume +input when they succeed: `many(preturn(1))("")` raises `ValueError` because it +cannot make progress. For example, `many(pchar("a"))("a" * 3000)` returns an +`Ok` containing all 3000 characters. + ```{eval-rst} .. automodule:: expression.extra :members: diff --git a/expression/collections/array.py b/expression/collections/array.py index 4522851a..b98b49e8 100644 --- a/expression/collections/array.py +++ b/expression/collections/array.py @@ -340,7 +340,7 @@ def skip(self, count: int) -> TypedArray[_TSource]: return TypedArray(self.value[count:]) def skip_last(self, count: int) -> TypedArray[_TSource]: - return TypedArray(self.value[:-count]) + return TypedArray(self.value[:-count] if count != 0 else self.value[:]) def sort(self: TypedArray[_TSourceSortable], reverse: bool = False) -> TypedArray[_TSourceSortable]: """Sort array directly. @@ -406,7 +406,7 @@ def take_last(self, count: int) -> TypedArray[_TSource]: Returns: The result list. """ - return TypedArray(self.value[-count:]) + return TypedArray(self.value[-count:] if count != 0 else self.value[:0]) def try_head(self) -> Option[_TSource]: """Try to return first element. diff --git a/expression/collections/asyncseq.py b/expression/collections/asyncseq.py index 7576da8a..87acb8d8 100644 --- a/expression/collections/asyncseq.py +++ b/expression/collections/asyncseq.py @@ -167,12 +167,14 @@ async def take(self, count: int) -> AsyncIterable[TSource]: """Returns the first N elements of the sequence.""" async def _take(source: AsyncIterable[TSource]) -> AsyncIterable[TSource]: + if count <= 0: + return i = 0 async for value in source: - if i >= count: - break yield value i += 1 + if i >= count: + break return pipe(self, _take) diff --git a/expression/collections/block.py b/expression/collections/block.py index 297b96cc..9d8d0365 100644 --- a/expression/collections/block.py +++ b/expression/collections/block.py @@ -384,7 +384,7 @@ def skip(self, count: int) -> Block[_TSource]: return Block(self._value[count:]) def skip_last(self, count: int) -> Block[_TSource]: - return Block(self._value[:-count]) + return Block(self._value[:-count] if count != 0 else self._value) def tail(self) -> Block[_TSource]: """Return tail of List.""" @@ -441,7 +441,7 @@ def take_last(self, count: int) -> Block[_TSource]: Returns: The result list. """ - return Block(self._value[-count:]) + return Block(self._value[-count:] if count != 0 else ()) def dict(self) -> list[_TSource]: """Returns a json serializable representation of the list.""" diff --git a/expression/collections/map.py b/expression/collections/map.py index a5fbf33c..1b4edabc 100644 --- a/expression/collections/map.py +++ b/expression/collections/map.py @@ -237,7 +237,7 @@ def __eq__(self, o: Any) -> bool: else: if kv != kv_other: return False - return True + return next(iterator, None) is None def __bool__(self) -> bool: return not maptree.is_empty(self._tree) diff --git a/expression/collections/maptree.py b/expression/collections/maptree.py index c78a1872..f7e2eae8 100644 --- a/expression/collections/maptree.py +++ b/expression/collections/maptree.py @@ -325,7 +325,7 @@ def change(k: Key, u: Callable[[Option[Value]], Option[Value]], m: MapTree[Key, return m case _: - for v in u(NOTHING): + for v in u(NOTHING).to_list(): return Some(MapTreeLeaf(k, v)) else: return m diff --git a/expression/extra/parser.py b/expression/extra/parser.py index 6f793cbe..13b9fca0 100644 --- a/expression/extra/parser.py +++ b/expression/extra/parser.py @@ -278,22 +278,22 @@ def pstring(string_input: str) -> Parser[str]: def parse_zero_or_more(parser: Parser[_A], input: Remaining) -> tuple[Block[_A], Remaining]: - # run parser with the input - first_result = parser.run(input) - - # test the result for Failure/Success - match first_result: - case Ok((first_value, input_after_first_parse)): - # if parse succeeds, call recursively - # to get the subsequent values - subsequent_values, remaining_input = parse_zero_or_more(parser, input_after_first_parse) - values = subsequent_values.cons(first_value) - return values, remaining_input - case _: - return (block.empty, input) + values: list[_A] = [] + remaining = input + while True: + match parser.run(remaining): + case Ok((value, next_input)): + if next_input == remaining: + raise ValueError("Repeated parser must consume input") + values.append(value) + remaining = next_input + case Error(): + return Block(values), remaining def many(parser: Parser[_A]) -> Parser[Block[_A]]: + """Parse zero or more occurrences, rejecting success without input consumption.""" + def run(input: Remaining) -> ParseResult[Block[_A]]: # parse the input -- wrap in Success as it always succeeds ok = parse_zero_or_more(parser, input) @@ -303,6 +303,8 @@ def run(input: Remaining) -> ParseResult[Block[_A]]: def many1(parser: Parser[_A]) -> Parser[Block[_A]]: + """Parse one or more occurrences, rejecting success without input consumption.""" + def run(input: Remaining) -> ParseResult[Block[_A]]: # run parser with the input firstResult = parser.run(input) diff --git a/tests/test_array.py b/tests/test_array.py index 9c0c5863..3794672a 100644 --- a/tests/test_array.py +++ b/tests/test_array.py @@ -239,7 +239,7 @@ def test_array_take(xs: list[int], x: int): @given(st.lists(st.integers()), st.integers(min_value=0)) # type: ignore def test_array_take_last(xs: list[int], x: int): - expected = xs[-x:] + expected = xs[len(xs) - min(x, len(xs)) :] ys: TypedArray[int] ys = array.of_seq(xs).take_last(x) assert list(ys) == expected @@ -257,7 +257,7 @@ def test_array_skip(xs: list[int], x: int): @given(st.lists(st.integers()), st.integers(min_value=0)) # type: ignore def test_array_skip_last(xs: list[int], x: int): - expected = xs[:-x] + expected = xs[: max(len(xs) - x, 0)] ys: TypedArray[int] ys = array.of_seq(xs).skip_last(x) assert list(ys) == expected @@ -428,3 +428,10 @@ def test_array_monad_law_associativity_iterable(xs: list[int]): m = array.of_seq(xs) assert m.collect(f).collect(g) == m.collect(lambda x: f(x).collect(g)) + + +def test_array_trailing_zero_count(): + source = array.of_seq([1, 2, 3]) + assert list(source.take_last(0)) == [] + assert list(pipe(source, array.take_last(0))) == [] + assert list(source.skip_last(0)) == [1, 2, 3] diff --git a/tests/test_asyncseq.py b/tests/test_asyncseq.py index 1b67b60b..cf6ee0f4 100644 --- a/tests/test_asyncseq.py +++ b/tests/test_asyncseq.py @@ -41,3 +41,24 @@ async def runner(): asyncio.run(runner()) assert acc == sum(range(count)) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("count", [0, 1, 3, 4]) +async def test_asyncseq_take_preserves_remaining_items(count: int): + source = AsyncSeq.range(3) + taken = [value async for value in await source.take(count)] + remaining = [value async for value in source] + assert taken == list(range(min(count, 3))) + assert remaining == list(range(min(count, 3), 3)) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("count", [0, 1]) +async def test_asyncseq_take_does_not_request_another_item(count: int): + async def source(): + for value in range(count): + yield value + raise AssertionError("Requested an item beyond the take limit") + + assert [value async for value in await AsyncSeq(source()).take(count)] == list(range(count)) diff --git a/tests/test_block.py b/tests/test_block.py index 8b527e22..fa8c88e5 100644 --- a/tests/test_block.py +++ b/tests/test_block.py @@ -206,7 +206,7 @@ def test_block_take(xs: List[int], x: int): @given(st.lists(st.integers()), st.integers(min_value=0)) # type: ignore def test_block_take_last(xs: List[int], x: int): - expected = xs[-x:] + expected = xs[len(xs) - min(x, len(xs)) :] ys: Block[int] ys = block.of_seq(xs).take_last(x) assert list(ys) == expected @@ -224,7 +224,7 @@ def test_block_skip(xs: List[int], x: int): @given(st.lists(st.integers()), st.integers(min_value=0)) # type: ignore def test_block_skip_last(xs: List[int], x: int): - expected = xs[:-x] + expected = xs[: max(len(xs) - x, 0)] ys: Block[int] ys = block.of_seq(xs).skip_last(x) assert list(ys) == expected @@ -458,3 +458,11 @@ def test_serialize_block_works(): assert model_.annotated_type_empty == block.empty assert model_.custom_type == Block(["a", "b", "c"]) assert model_.custom_type_empty == block.empty + + +def test_block_trailing_zero_count(): + source = block.of_seq([1, 2, 3]) + assert list(source.take_last(0)) == [] + assert list(pipe(source, block.take_last(0))) == [] + assert list(source.skip_last(0)) == [1, 2, 3] + assert list(pipe(source, block.skip_last(0))) == [1, 2, 3] diff --git a/tests/test_map.py b/tests/test_map.py index 553a36ff..a11c6642 100644 --- a/tests/test_map.py +++ b/tests/test_map.py @@ -150,3 +150,24 @@ def test_expression_issue_105(): m = m.add("1", 1).add("2", 2).add("3", 3).add("4", 4) m = m.change("2", lambda x: x) m = m.change("3", lambda x: x) + + +def test_map_equality_checks_both_lengths(): + empty = Map[int, str].empty() + short = Map.of_list([(1, "a")]) + longer = Map.of_list([(1, "a"), (2, "b")]) + for left, right in [(empty, short), (short, longer)]: + assert left != right + assert right != left + equal = Map.of_list([(2, "b"), (1, "a")]) + assert longer == equal + assert hash(longer) == hash(equal) + assert len({empty, short, longer, equal}) == 3 + + +def test_map_change_missing_key_to_nothing(): + from expression import NOTHING + + for original in [Map[int, int].empty(), Map.of_list([(1, 10), (2, 20), (3, 30)])]: + assert original.change(0, lambda _: NOTHING) == original + assert pipe(original, map.change(4, lambda _: NOTHING)) == original diff --git a/tests/test_parser.py b/tests/test_parser.py index 686fe28b..e265676b 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -251,3 +251,34 @@ def test_parse_name_expr(): case _: assert False + + +def test_many_long_input_preserves_order_and_remaining(): + from expression.extra.parser import many1, sep_by1 + + text = "ab" * 1500 + "!" + expected = Ok((Block(text[:-1]), (text, len(text) - 1))) + assert many(any_of("ab")).run((text, 0)) == expected + assert many1(any_of("ab")).run((text, 0)) == expected + + separated = ",".join("ab" * 750) + "!" + assert sep_by1(any_of("ab"), pchar(",")).run((separated, 0)) == Ok( + (Block("ab" * 750), (separated, len(separated) - 1)) + ) + + +def test_many_rejects_non_consuming_parser(): + import pytest + + from expression.extra.parser import many1, preturn + + for repeated in [many(preturn(1)), many1(preturn(1)), many(opt(pchar("a")))]: + with pytest.raises(ValueError, match="must consume input"): + repeated("") + + +def test_many_preserves_input_on_failure(): + from expression.extra.parser import many1 + + assert many(pchar("a")).run(("b", 0)) == Ok((Block(), ("b", 0))) + assert many1(pchar("a"))("b").is_error()