Directories in Elm

Here’s the translation of the Go code to Elm, with explanations in Markdown format suitable for Hugo:

Our first program will work with directories in the file system. Here’s the full source code:

import Platform exposing (Program)
import Task
import File
import File.Select as Select
import File.Dir as Dir

main : Program () Model Msg
main =
    Platform.worker
        { init = init
        , update = update
        , subscriptions = always Sub.none
        }

type alias Model =
    ()

type Msg
    = CreateDirectory
    | DirectoryCreated (Result File.Error ())
    | ListDirectory
    | DirectoryListed (Result File.Error (List String))

init : () -> ( Model, Cmd Msg )
init _ =
    ( (), Cmd.none )

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        CreateDirectory ->
            ( model, Dir.create "subdir" |> Task.attempt DirectoryCreated )

        DirectoryCreated (Ok _) ->
            ( model, Cmd.none )

        DirectoryCreated (Err _) ->
            ( model, Cmd.none )

        ListDirectory ->
            ( model, Dir.list "subdir" |> Task.attempt DirectoryListed )

        DirectoryListed (Ok files) ->
            let
                _ = Debug.log "Files in subdir" files
            in
            ( model, Cmd.none )

        DirectoryListed (Err _) ->
            ( model, Cmd.none )

In Elm, working with the file system is not as straightforward as in some other languages due to its focus on web applications. However, we can simulate some directory operations using the elm/file package.

First, we set up a Platform.worker program, which is suitable for this kind of task-based operation without a user interface.

We define a Msg type that represents the different actions we want to perform:

  • CreateDirectory: to create a new directory
  • DirectoryCreated: to handle the result of creating a directory
  • ListDirectory: to list the contents of a directory
  • DirectoryListed: to handle the result of listing a directory

In the update function, we handle these messages:

For CreateDirectory, we use Dir.create to create a new directory named “subdir”.

CreateDirectory ->
    ( model, Dir.create "subdir" |> Task.attempt DirectoryCreated )

For ListDirectory, we use Dir.list to get the contents of the “subdir” directory.

ListDirectory ->
    ( model, Dir.list "subdir" |> Task.attempt DirectoryListed )

When we receive the DirectoryListed message with a successful result, we log the list of files to the console:

DirectoryListed (Ok files) ->
    let
        _ = Debug.log "Files in subdir" files
    in
    ( model, Cmd.none )

Note that Elm’s file system operations are more limited compared to Go’s, as Elm is primarily designed for web applications. In a real-world scenario, you might need to use ports to interact with the file system through JavaScript if you’re running Elm on Node.js, or you might need to communicate with a backend server for file system operations in a web application.

To run this Elm program, you would typically compile it to JavaScript and then run it in a browser or with Node.js. The exact commands would depend on your Elm setup and environment.

$ elm make Main.elm --output=main.js
$ node main.js

Remember that this is a simplified example and real-world file system operations in Elm would likely require additional setup and potentially the use of ports or a backend server.