# `Cauldron2D.Thinkers`
[🔗](https://github.com/jaman/cauldron/blob/v0.2.2/cauldron_2d/lib/cauldron_2d/thinkers.ex#L1)

The things in a world that change by themselves each tick — a door closing, a lift
waiting, a light flickering, a creature — kept in a fixed order so a world run twice
from the same start runs the same.

    thinkers = Cauldron2D.Thinkers.new()
    {door, thinkers} = Cauldron2D.Thinkers.add(thinkers, %{kind: :door, sector: 12, speed: 2.0})

    {thinkers, world} =
      Cauldron2D.Thinkers.run(thinkers, world, fn thinker, id, world ->
        MyGame.think(thinker, id, world)
      end)

A thinker is any term, filed under the id `add/2` gives it. `run/3` calls the function
for each thinker in the order they were added; it answers `{:keep, thinker, world}` to
carry on or `{:done, world}` to be removed. A run visits the thinkers that existed when
it began; thinkers to start during a run are added with `add/2` to the thinkers it
returns.

# `id`

```elixir
@type id() :: pos_integer()
```

# `t`

```elixir
@type t() :: %Cauldron2D.Thinkers{
  entries: :gb_trees.tree(id(), term()),
  next: pos_integer()
}
```

# `add`

```elixir
@spec add(t(), term()) :: {id(), t()}
```

Add `thinker`; gives its id and the thinkers.

# `count`

```elixir
@spec count(t()) :: non_neg_integer()
```

How many thinkers there are.

# `get`

```elixir
@spec get(t(), id()) :: term() | nil
```

Thinker `id`, or `nil`.

# `new`

```elixir
@spec new() :: t()
```

No thinkers.

# `remove`

```elixir
@spec remove(t(), id()) :: t()
```

Remove thinker `id`, if it is there.

# `run`

```elixir
@spec run(t(), world, (term(), id(), world -&gt; {:keep, term(), world} | {:done, world})) ::
  {t(), world}
when world: term()
```

Run every thinker once, in order. See the moduledoc.

# `to_list`

```elixir
@spec to_list(t()) :: [{id(), term()}]
```

Every thinker with its id, in order.

# `update`

```elixir
@spec update(t(), id(), (term() -&gt; term())) :: t()
```

Replace thinker `id` with `fun.(thinker)`; nothing if it is not there.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
