So, I’m processing this list of js data which, for this evening, I know
contains exactly one thingummy. I’m I want to wire up some Elm and get things
to type check before I start to worry about handling lists of different sizes.
Calling upon my conventional (python) wisdom, I set about processing the
singleton in the list.
case msg of UpdateThing msg' -> (thing, cmd) = Thingummy.update msg' ... singleton element from model.things ...
How do I get that sucker? I’m thinking python: `model.things` but, of
course, both `model.things !! 1`, and `List.head` return `Maybe a`. So now I’m
... (thing, cmd) ... = Maybe.map (Thingummy.update msg') (List.head model.things) ...
But now the lhs pattern is wrong, I’m using `map` and, ultimately, I need to
turn that updated singleton back into a list anyways.
case msg of UpdateThing msg' -> (things, cmds) = List.unzip (List.map (Thingummy.update msg') model.things)
Job done. (I should qualify that the job here is just the quick hack for
my singleton list. It’s applying the same change to all elements)
Moral: it isn’t any less complex to deal with a list of things than
with a thing taken from a list (Maybe). I can’t ask Elm to accept my
assumption that a list will contain exactly one item. Elm wants me to model
it. If there’s exactly one, just use a variable. If there’s zero or one,
use Maybe. Or just use a list, it’s just as simple!