<commit_before>---
navhome: /docs
next: true
sort: 4
title: State
---

State
=====

In the last section we built a few small apps that sent moves. These
apps were entirely stateless, however. Most useful apps require some
amount of state. Let's build a trivial stateful app. It'll keep a
running the sum of all the atoms we poke it with. Here's
`app/examples/sum.hoon`:

    /?    314
    !:
    |_  {bowl state/@}
    ++  poke-atom
      |=  arg/@
      ^-  {(list) _+>.$}
      ?:  =(arg 0)  
        [~ +>.$(state 0)]
      ~&  [%so-far (add state arg)]
      [~ +>.$(state (add state arg))]
    --

We can start it with `|start %examples-sum`, and then run it:

    ~fintud-macrep:dojo> :examples-sum &atom 5
    [%so-far 5]
    >=
    ~fintud-macrep:dojo> :examples-sum &atom 2
    [%so-far 7]
    >=
    ~fintud-macrep:dojo> :examples-sum &atom 15
    [%so-far 22]
    >=

We can see that app state is being saved, but when, where, and how?

The state is stored as the second thing in the `|_` line. In our case,
it's simply an atom named `state`. We change it by producing as our
state not with `+>.$` (as before), but with
`+>.$(state (add state arg))`. We've seen all these parts before, but
you might not recognize them.

Recall in the first chapter that we recursed with the expression
`$(b (add 3 b))`. This meant "produce `$` with `b` changed to
`(add 3 b)`. Similarly, `+>.$(state (add state arg))` means "produce
`+>.$` (i.e. our context, which contains our state) with `state` changed
to `(add state arg)`.
<commit_msg>Removed solution to the Arvo's State chapter's first exercise from its example code.<commit_after>---
navhome: /docs
next: true
sort: 4
title: State
---

State
=====

In the last section we built a few small apps that sent moves. These
apps were entirely stateless, however. Most useful apps require some
amount of state. Let's build a trivial stateful app. It'll keep a
running the sum of all the atoms we poke it with. Here's
`app/examples/sum.hoon`:

    /?    314
    !:
    |_  {bowl state/@}
    ++  poke-atom
      |=  arg/@
      ^-  {(list) _+>.$}
      ~&  [%so-far (add state arg)]
      [~ +>.$(state (add state arg))]
    --

We can start it with `|start %examples-sum`, and then run it:

    ~fintud-macrep:dojo> :examples-sum &atom 5
    [%so-far 5]
    >=
    ~fintud-macrep:dojo> :examples-sum &atom 2
    [%so-far 7]
    >=
    ~fintud-macrep:dojo> :examples-sum &atom 15
    [%so-far 22]
    >=

We can see that app state is being saved, but when, where, and how?

The state is stored as the second thing in the `|_` line. In our case,
it's simply an atom named `state`. We change it by producing as our
state not with `+>.$` (as before), but with
`+>.$(state (add state arg))`. We've seen all these parts before, but
you might not recognize them.

Recall in the first chapter that we recursed with the expression
`$(b (add 3 b))`. This meant "produce `$` with `b` changed to
`(add 3 b)`. Similarly, `+>.$(state (add state arg))` means "produce
`+>.$` (i.e. our context, which contains our state) with `state` changed
to `(add state arg)`.

{'fraction_non_alphanumeric': 0.1425339366515837, 'fraction_numerical': 0.009696186166774402, 'mean_word_length': 2.9276649746192893, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '611626', 'n_tokens_mistral': 1121, 'n_tokens_neox': 1037, 'n_words': 440}