Platform.worker example


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:

Screen Shot 2020-01-26 at 7.55.03 PM

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

Screen Shot 2020-01-26 at 10.12.39 PM

What happens if you keep this up?


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
      |> Debug.toString


This might be of interest to you:

You could rename InputType and OutputType to just Input and Output.

Thakyou ni-ko-o-kin!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.