Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/reference/collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ API.
Detailed module references are available for {doc}`Seq <seq>`, {doc}`Block <block>`,
{doc}`Map <map>`, and {doc}`TypedArray <array>`.

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}
Expand Down
6 changes: 6 additions & 0 deletions docs/reference/extras.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions expression/collections/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions expression/collections/asyncseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions expression/collections/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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."""
Expand Down
2 changes: 1 addition & 1 deletion expression/collections/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion expression/collections/maptree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 15 additions & 13 deletions expression/extra/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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]
21 changes: 21 additions & 0 deletions tests/test_asyncseq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
12 changes: 10 additions & 2 deletions tests/test_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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]
21 changes: 21 additions & 0 deletions tests/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 31 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Loading