module salix::demo::basic::Celsius
rascal-0.34.0
salix-core-0.2.3
Usage
import salix::demo::basic::Celsius;
Source code
http://github.com/usethesource/salix-core/src/main/rascal/salix/demo/basic/Celsius.rsc
Dependencies
import salix::App;
import salix::HTML;
import salix::Index;
import String;
import util::Math;
import Exception;
alias Model
real
data Msg
data Msg
= c(str c)
| f(str f)
;
function celsiusApp
SalixApp[Model] celsiusApp(str id = "root")
= makeApp(id, init, withIndex("C2F", id, view), update);
function celsiusWebApp
App[Model] celsiusWebApp()
= webApp(
celsiusApp(),
|project://salix/src/main/rascal|
);
function init
Model init() = 37.0;
function view
void view(Model m) {
h2("Celsius to fahrenheit converter");
p(() {
text("C: ");
input(\value("<round(m)>"),\type("number"), onInput(c));
});
p(() {
text("F: ");
input(\value("<round(toF(m))>"),\type("number"), onInput(f));
});
}
function toF
real toF(real c) = c * 9.0/5.0 + 32.0;
function toC
real toC(real f) = (f - 32.0) * 5.0/9.0;
function toReal {#salix-demo-basic-Celsius-toReal}
real toReal_(str s) {
try {
return toReal(s);
}
catch IllegalArgument():
return 0.0;
}
function update
real update(Msg msg, Model model) {
switch (msg) {
case c(str new): model = toReal_(new);
case f(str new): model = toC(toReal_(new));
}
return model;
}