`Maybe` handling in Matrix manipulation in Elm I'd like to learn if my rewrite is worse than the original, less clear or less "idiomatic".

I've found this code that handles updating two dimensional matrixes in Elm:

type alias Matrix a = Array.Array (Array.Array a)
type alias Location = (Int, Int)

get : Location -&gt; Matrix a -&gt; Maybe a
get location m =
  Array.get (row location) m `andThen` Array.get (col location)


set : Location -&gt; a -&gt; Matrix a -&gt; Matrix a
set location value m =
  update location (always value) m

update : Location -&gt; (a -&gt; a) -&gt; Matrix a -&gt; Matrix a
update location f m =
  get location m
  |&gt; Maybe.map (
    \current -&gt;

      Array.get (row location) m
      |&gt; Maybe.map (
          \oldRow -&gt;
            Array.set (col location) (f current) oldRow
            |&gt; (\newRow -&gt; Array.set (row location) newRow m)
      )
      |&gt; Maybe.withDefault m
  )
  |&gt; Maybe.withDefault m


I couldn't understand update at first so I tried to write my own (using my own domain types):

type Player = PlayerOne | PlayerTwo
type Slot = Empty | PlayedBy Player
type alias Board = Array.Array (Array.Array (Slot))
type alias Position = (Int, Int)

get : Board -&gt; Position -&gt; Maybe Slot
get board (row, column) =
    Array.get row board `Maybe.andThen` Array.get column

setPosition : Board -&gt; Position -&gt; Player -&gt; Board
setPosition board (row, column) player = 
    case get board (row, column) of
        Nothing -&gt;
            board
        Just Empty -&gt;
            case Array.get row board of
                Nothing -&gt;
                    board
                Just oldRow -&gt;
                    let
                        newRow = Array.set column (PlayedBy player) oldRow
                    in
                        Array.set row newRow board
        Just s -&gt;
            board


After rewriting I could understand what's going on in the original update function, but ended up feeling my version's clearer. This is my first day writing Elm so I'm no authority in Elm code clarity.
I like your code a lot more than the original! It is definitely cleaner and more obvious than the first example you gave. However, there still seems to be a process you’re repeating — get value from array, pattern match on it, update array — that you can still factor out. (The original code is guilty of this, too.)

I tried to rewrite your function in a matter that avoids this, and my code’s at the bottom; see if you find it more readable. But first, some stylistic advice on writing Elm:


Generally, instead of writing things like Array.Array, it's useful to expose the type name from the data structure package you're using. Also, type parameters don’t need parentheses around them (except for grouping).

So you can rewrite:

type alias Board = Array.Array (Array.Array (Slot))


to

import Array exposing (Array)

type alias Board = Array (Array Slot)


which I think is a bit more Elm-y.
A useful design guideline (for packages, but  it’s a good idea for code in general): the data structure is always the last argument. You’ve reordered them from your first example, but the original order was there for a good reason: if your types look like

f : (extra params) -&gt; Container -&gt; Container


you can partially apply your function and end up with:

f "extra" "arguments" : Container -&gt; Container


which is very useful for function composition and piping: often you want to take a Container and pipe it through a bunch of Container -&gt; Container functions, like so:

result =
    initialContainerValue
    |&gt; f "hello" "world"
    |&gt; someOtherStep
    |&gt; f "oak" "fir"

The Elm style guide contains some more recommendations that could apply to your code, but they’re more subjective. The two big ones I could think of have to do with whitespace. 


Always have 2 empty lines between top-level declarations.
The examples in the guide have single empty lines between case branches, too.



My version

{-| Apply a function at the given index in the array.

     applyAt 2 negate (fromList [50, 60, 70]) == fromList [50, 60, -70]
-}
applyAt : Int -&gt; (a -&gt; a) -&gt; Array a -&gt; Array a
applyAt i f array =
    case Array.get i array of
        Nothing -&gt;
            array

        Just a -&gt;
            Array.set i (f a) array


{-| Update a slot: the given player occupies it, but only if it's empty. -}
setSlot : Player -&gt; Slot -&gt; Slot
setSlot player slot =
    case slot of
        Empty -&gt;
            PlayedBy player

        s -&gt;
            s


{-| Update a slot somewhere in a board. -}
setPosition : Position -&gt; Player -&gt; Board -&gt; Board
setPosition (row, column) player =
    applyAt row
        (applyAt column
            (setSlot player))