Running Elm as a Blackbox

Image for post
Image for post

The functional programming language Elm is best known for writing web apps. You can also use Elm to write a headless “black box” app that you talk to via the terminal. Here is a sample session:

Image for post
Image for post

Start the program with the command , then type your own commands. In this example, the commands are integers which the black box factors into primes. The set-up I will describe here has the following architecture:

Image for post
Image for post

On the left is the terminal, where you type your commands. Next is a 29-line Javascript program that talks to both the terminal and . imports a module that exposes a function

When receives a string from the terminal, it computes and sends this string back to the terminal. Thus, to change the behavior of the system, it suffices to change the black box. In the example above, we installed the black box using

in . To do statistics instead of factoring integers, we would say

then recompile. Here is a session that results from this change:

Image for post
Image for post

There is also a command, whose text is defined by in whatever black box module you import. And here is is the help screen that one includes with most black box apps:

Image for post
Image for post

Note that there are a number of operations for working with files. These are defined in , and so are available to any black box app. The command loads a file and stores it in memory. The command applies to the contents of memory. This command can take arguments. Thus calculates statistics for column 5 of the data stored in memory, while does the same, but only for rows 100 through 200. You can also bypass file operations entirely, applying directly to data:

Image for post
Image for post

The code for all this is on GitHub. Just clone it, import the black box of your choice (or better, write your own). Compile with Link to the global command with

where of course you have to set . That is all there is to it!

Written by

jxxcarlson on elm slack, http://jxxcarlson.github.io

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store