Platform.worker
In an effort to understand Platform.worker
programs, I cooked up a simple example which you can find on GitHub. Below is a screenshot of the CLI in action,. It commands the worker to perform some mysterious computation:
There are two parts to the code, the worker program, Elm.main
, and the command-line interface, cli.js
. Elm.main
acts as a black box that accepts input via ports, does a computation, and send the resulting output back via ports. The user communicates with the black box using cli.js
.
In this demo, the output is computed as follows: if the input is even, divide it by 2. If it is odd, multiply by 3 and add 1. To do something more interesting/useful, just change the function
transform : InputType -> OutputType
in Main.js
.
Added: a REPL version
What happens if you keep this up?
Note
I would like to thank @pd-andy, @urban-a and @jfmengels on the Elm Slack for their help. I was also inspiredby Punie’s article on his simply-typed lambda calculus interpreter and the code for it.
A more serious application
The below uses the repl interface to the worker and replaces the definition of the transform
function with
type alias InputType = String
type alias OutputType = List String
transform : InputType -> OutputType
transform str =
MiniLatex.parse str
|> List.map Debug.toString