Skip to main content

module salix::demo::basic::Random

rascal-0.42.0
org.rascalmpl.salix-core-0.2.11

Usage​

import salix::demo::basic::Random;

Dependencies​

import salix::HTML;
import salix::Core;
import salix::App;
import salix::Index;

function randomApp​

SalixApp[Model] randomApp(str id = "root") 
= makeApp(id, init, withIndex("Random", id, view), update);

function randomWebApp​

App[Model] randomWebApp() = webApp(randomApp(), |project://salix/src/main/rascal|);

alias Model​

tuple[int dieFace]

function init​

Model init() = <1>;

data Msg​

data Msg  
= roll()
| newFace(int face)
;

function update​

Model update(Msg msg, Model m) {
switch (msg) {
case roll(): do(random(newFace, 1, 6));
case newFace(int n): m.dieFace = n;
}
return m;
}

function view​

void view(Model m) {
button(onClick(roll()), "Roll");
text(m.dieFace);
}

function twiceWebApp​

App[TwiceModel] twiceWebApp()
= webApp(
makeApp("root", twiceInit, withIndex("Twice", "root", twiceView), twiceUpdate),
|project://salix/src/main/rascal|
);

function twiceInit​

TwiceModel twiceInit() = twice(init(), init());

data TwiceModel​

data TwiceModel  
= twice(Model model1, Model model2)
;

data Msg​

data Msg  
= sub1(Msg msg)
| sub2(Msg msg)
;

function twiceUpdate​

TwiceModel twiceUpdate(Msg msg, TwiceModel m) {
switch (msg) {
case sub1(Msg s):
m.model1 = mapCmds(sub1, s, m.model1, update);
case sub2(Msg s):
m.model2 = mapCmds(sub2, s, m.model2, update);
}
return m;
}

function twiceView​

void twiceView(TwiceModel m) {
h2("Two times roll a die");
mapView(sub1, m.model1, view);
mapView(sub2, m.model2, view);
}