# Platform.worker example

**URL:** <https://discourse.elm-lang.org/t/platform-worker-example/5074>\
**Category:** Show and Tell\
**Created:** [January 26, 2020, 6:58pm UTC](https://discourse.elm-lang.org/t/platform-worker-example/5074 "2020-01-26T18:58:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jxxcarlson](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jxxcarlson/32/866_2.png) [@jxxcarlson](https://discourse.elm-lang.org/u/jxxcarlson)\
**Post date:** [January 26, 2020, 6:58pm UTC](https://discourse.elm-lang.org/t/platform-worker-example/5074/1 "2020-01-26T18:58:25Z")

</div>

# Platform.worker

In an effort to understand `Platform.worker` programs, I cooked up a simple example which you can find [on GitHub](https://github.com/jxxcarlson/elm-platform-worker-example). 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](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/2X/8/8f4201dc67c545104f51a808b074a6227df88dde.png)

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](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/2X/e/e5eea45434b552b7cc7aff7e243c40e13bf94312.png)

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](https://discourse.elm-lang.org/t/simply-typed-lambda-calculus-in-elm/1772) on his simply-typed lambda calculus interpreter and the [code for it](https://github.com/Punie/elm-stlc).

## A more serious application

The below uses the repl interface to the worker and replaces the definition of the `transform` function with

```elm
type alias InputType = String
type alias OutputType = List String

transform : InputType -> OutputType
transform str =
   MiniLatex.parse str
      |> List.map Debug.toString

```

 ![Screen Shot 2020-01-27 at 3.43.14 AM](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/2X/b/b55612953e2dbb1abb497438d927543fb5b5b23a.png)

---

<div class="post-metadata">

**Author:** ![ni-ko-o-kin](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/ni-ko-o-kin/32/2693_2.png) [@ni-ko-o-kin](https://discourse.elm-lang.org/u/ni-ko-o-kin)\
**Post date:** [January 27, 2020, 9:52am UTC](https://discourse.elm-lang.org/t/platform-worker-example/5074/2 "2020-01-27T09:52:53Z")

</div>

This might be of interest to you: [https://github.com/ni-ko-o-kin/elm-node](https://github.com/ni-ko-o-kin/elm-node)

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

---

<div class="post-metadata">

**Author:** ![jxxcarlson](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jxxcarlson/32/866_2.png) [@jxxcarlson](https://discourse.elm-lang.org/u/jxxcarlson)\
**Post date:** [January 27, 2020, 11:14am UTC](https://discourse.elm-lang.org/t/platform-worker-example/5074/3 "2020-01-27T11:14:46Z")

</div>

Thakyou ni-ko-o-kin!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [February 6, 2020, 11:14am UTC](https://discourse.elm-lang.org/t/platform-worker-example/5074/4 "2020-02-06T11:14:50Z")

</div>

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