module salix::demo::basic::Celsius
rascal-0.41.2
org.rascalmpl.salix-core-0.2.9
Usage
import salix::demo::basic::Celsius;
Dependencies
import salix::App;
import salix::HTML;
import salix::Index;
import String;
import util::Math;
import Exception;
import salix::util::Debug;
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 debugCelsiusView
void debugCelsiusView(DebugModel[Model] m) {
debugView(m, view);
}
function debugCelsius
App[DebugModel[Model]] debugCelsius()
= debug("celsius"
, Model() { return 37.0; }
, void(DebugModel[Model] m) { ; }
, Model(Msg x, Model m) { return m; }
, |project://salix/src/main/rascal|);
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;
}