Repository navigation
id(roc): finish the example migration — tic-tac-toe -> RocJson, roc-counter -> Screen, adopt RocText (#565) #2970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,40 @@ | ||
| app [program] { pf: platform "../roc-world/platform.roc" } | ||
|
|
||
| import pf.Screen | ||
| import pf.RocText | ||
|
Comment on lines
+3
to
+4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Roc and the platform assets are available, Useful? React with 👍 / 👎. |
||
|
|
||
| Model := { count : I64 } | ||
|
|
||
| program = { init, update, view, layout, records, wants, snapshot, restore } | ||
|
|
||
| init : U64 -> Model | ||
| init = |_seed| { count: 0 } | ||
|
|
||
| ## Events are matched after a RocText trim, so a journaled or typed | ||
| ## " inc " counts the same as "inc". | ||
| update : Model, U64, Str -> Model | ||
| update = |model, _pid, ev| if ev == "inc" { count: model.count + 1 } else model | ||
| update = |model, _pid, ev| if RocText.trim(ev) == "inc" { count: model.count + 1 } else model | ||
|
|
||
| ## The counter renders through Screen, like roc-screen: a boxed 20x3 | ||
| ## screen whose JSON the host draws, instead of a bare text view. | ||
| view : Model -> Str | ||
| view = |model| Str.concat("count=", model.count.to_str()) | ||
| view = |model| render(model, 20, 3) | ||
|
dezren39 marked this conversation as resolved.
Resolved
|
||
|
|
||
| ## Structured data the host stores and replicates: a JSON object of records. | ||
| ## This program renders the same layout at any viewport. | ||
| # Reflow: a wide viewport gets a wider box, a short one keeps three rows. | ||
| layout : Model, U64, U64 -> Str | ||
| layout = |model, _width, _height| view(model) | ||
| layout = |model, width, height| { | ||
| w = if width >= 30 30 else 20 | ||
| h = if height >= 4 3 else 3 | ||
| render(model, w, h) | ||
| } | ||
|
|
||
| render : Model, U64, U64 -> Str | ||
| render = |model, w, h| { | ||
| base = Screen.boxed(Screen.new(w, h), 0, 0, w, h, "counter") | ||
| Screen.render_json(Screen.put_text(base, 2, 1, "count=${model.count.to_str()}", { fg: 0, bg: 0, bold: Bool.False })) | ||
|
Comment on lines
+33
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The sandbox-enabled suite still enforces 100,000-fuel budgets for both counter Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| ## Structured data the host stores and replicates: a JSON object of records. | ||
| records : Model -> Str | ||
| records = |model| "{\"count\":${model.count.to_str()}}" | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.