module salix::demo::basic::Loop
rascal-0.34.0
salix-core-0.2.3
Usage
import salix::demo::basic::Loop;
Source code
http://github.com/usethesource/salix-core/src/main/rascal/salix/demo/basic/Loop.rsc
Dependencies
import salix::App;
import salix::Core;
import salix::HTML;
import salix::Index;
import String;
import List;
alias Model
tuple[int count, list[int] numbers]
function init
Model init() {
do(random(addNumber, 1, 100));
return <100, []>;
}
function loopApp
SalixApp[Model] loopApp(str id = "root")
= makeApp(id, init, withIndex("Loop", id, view), update);
function loopWebApp
App[Model] loopWebApp()
= webApp(loopApp(), |project://salix/src/main/rascal|);
data Msg
data Msg
= addNumber(int n)
| updateCount(str x)
;
function update
Model update(Msg msg, Model m) {
switch (msg) {
case addNumber(int x): {
if (size(m.numbers) < m.count) {
m.numbers += [x];
do(random(addNumber, 1, 100));
}
}
case updateCount(str x): {
m.count = toInt(x);
m.numbers = m.numbers[0..m.count];
do(random(addNumber, 1, 100));
}
}
return m;
}
function view
void view(Model m) {
text("Count: ");
input(\type("text"), \value("<m.count>"), onInput(updateCount));
for (int i <- [0..size(m.numbers)]) {
p("<i>: <m.numbers[i]>");
}
}