Search code examples
arrayselmoption-type

Increment element at index


I am working on the guide.elm-lang website on the Random example. I am trying to add a feature that shows you the total number of times you threw the dice, and some stats on how many times you got each face.

To do this I've changed the model to look like this:

type alias Model =
  { 
    die_face : Int,
    total_throws : Int,
    stats: Array.Array Int
  }

and this is what I do to update the model:

{ model | 
     die_face = face, 
     total_throws = model.total_throws + 1, 
     stats = Array.set face ((Array.get face model.stats) + 1) model.stats
}

this throws an error that tells me:

This get call produces:

#Maybe# Int

But (+) only works with #Int# and #Float# values.

Which refers to Array.get not returning a Int but a Maybe and therefore I can't add it to the number 1.

I have tried using lists to achieve the same purpose but since they are not indexed I am not sure what to increment when I map over it. I am thinking of using records to do this and figure out a way to map the record keys to the face Int.

In general my question is. What is a good method to increment an element at index [x] of an Array in elm ? Or if I am just thinking about this wrong, what would be the elm way ?


Solution

  • Array.get returns a Maybe because it has to account for the case where the index is outside the array. The simplest way of getting around that is using Maybe.withDefault with a reasonable default value:

    Array.set face (((Array.get face model.stats) |> Maybe.withDefault 0) + 1) model.stats
    

    It might be a good idea to write a helper function for this though, to clean up the code a bit. Something like this:

    incrementAt : Int -> Array Int -> Array Int
    incrementAt index array =
        case Array.get index array of
            Just value ->
                Array.set index (value + 1) array
    
            Nothing ->
                array
    

    You can also use Array.Extra.update if you don't mind the extra dependency.