> ## Documentation Index
> Fetch the complete documentation index at: https://docs.expectedparrot.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Collections and lifecycle

> Build logs, derive collection views, and distinguish completion from closing.

This chapter continues the [shared-state manual](https://docs.expectedparrot.com/en/latest/shared_state.md).

## Building a log from first principles

A message log demonstrates a sequence field and `append`:

```python theme={null}
from edsl.sharedstate import append, record, reduce

message_log = Machine(
    name="MessageLog",
    constants={},
    fields={
        "messages": state_field(StateType.sequence(StateType.map()), initial=[]),
    },
    commands={
        "post": Command(
            inputs={
                "author": StateType.text(),
                "text": StateType.text(),
            },
            effects=(
                append(
                    "messages",
                    record(
                        author=arg("author"),
                        text=arg("text"),
                    ),
                ),
            ),
        ),
    },
    view={
        "messages": field("messages"),
        "message_count": field("messages").length(),
        "recent_messages": reduce(
            "tail", field("messages"), count=10
        ),
    },
)
```

The poll uses a map and `put`; the log uses a sequence and `append`. A few
primitives produce substantially different structures.

## Derived collection expressions

Views and transitions derive values without redundant fields:

```python theme={null}
values = field("forecasts").values()

mean = reduce("mean", values)
median = reduce("median", values)
latest = reduce("latest_by", field("history"), field="forecaster")
```

Collections can be transformed:

```python theme={null}
from edsl.sharedstate import filter_items, local, map_sequence

active = filter_items(
    field("orders"),
    item="order",
    predicate=local("order").get("status") == "open",
)

prices = map_sequence(
    active,
    item="order",
    value_expr=local("order").get("price"),
)
```

These operations traverse finite collections and are serializable; the current
interpreter does not impose general collection-size or execution-cost limits. `choose`, `and`, and `or`
short-circuit so guarded lookups are safe.

## Completion and close

A machine can say when enough information has arrived:

```python theme={null}
complete_when=(
    field("ballots").length()
    == constant("voter_count")
)
```

Some results are computed only at close:

```python theme={null}
from edsl.sharedstate import assign

close_effects=(
    assign(
        "mean",
        reduce("mean", field("choices").values()),
    ),
)
```

Before close, a view might reveal only a submission count. After close, it can
reveal choices and results. Closing is an audited atomic transition.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.