mylvinta
mylvinta

Reputation: 41

Elm: populating a record with List values

Trying to learn Elm, and it's pretty hard :)

What I'm trying to accomplish:

I have a model that's a record with a number of key value pairs. I want to populate those keys with values from a list of strings.

module Main exposing (..)
import List
import Html exposing (Html, program, div, text)

type alias Model =
    { one: String
    , two: String
    , three: String
    }

fakeData: List String
fakeData = ["foo", "bar", "baz", "bad", "baf"]

populate: Model -> List String -> Model
populate model data =
    case List.head data of
        Just str ->
            case model.one of
                "" ->
                    let updatedModel = 
                        { model | one = str }
                    in 
                        case List.tail data of
                            Just items ->
                                populate updatedModel items
                            Nothing ->
                                model
                _ ->
                    case model.two of
                        "" ->
                            let updatedModel = 
                                { model | two = str }
                            in 
                                case List.tail data of
                                    Just items ->
                                        populate updatedModel items
                                    Nothing ->
                                        model
                        _ ->
                            case model.three of
                                "" ->
                                    let updatedModel = 
                                        { model | three = str }
                                    in 
                                        case List.tail data of
                                            Just items ->
                                                populate updatedModel items
                                            Nothing ->
                                                model
                                _ ->
                                    model
        Nothing ->
            model


init: ( Model, Cmd Msg)
init =
    ( populate { one = "", two = "", three = "" } fakeData, Cmd.none )

type Msg =
    NoOp

view: Model -> Html Msg
view model =
    div []
        [ text (toString model) ]

update: Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        NoOp ->
            ( model, Cmd.none )

subscriptions: Model -> Sub Msg
subscriptions model =
    Sub.none

main: Program Never Model Msg
main = 
    program
    { init = init
    , view = view
    , update = update
    , subscriptions = subscriptions
    }

This program prints out:

{ one = "foo", two = "bar", three = "baz" }

I guess I have a hard time figuring out how to make this code less repetitive and easier to reason about. What if I have 20 keys in the model that all need to be populated? The above code would get pretty insane.

Upvotes: 0

Views: 315

Answers (1)

Chad Gilbert
Chad Gilbert

Reputation: 36375

You could use pattern matching on the list instead:

populate : Model -> List String -> Model
populate model data =
    case data of
        one :: two :: three :: _ ->
            { model | one = one, two = two, three = three }

        _ ->
            model

Upvotes: 3

Related Questions