What's the best way to chain updates?

108 views
Skip to first unread message

Rex van der Spuy

unread,
Dec 2, 2016, 11:56:37 AM12/2/16
to Elm Discuss
Hi Everyone,

Usually when I want a series of updates to chain in sequence, I'll write some code like this:

FirstThing ->
  let 
    newModel = { model | -- update the record properties -- }
  in
    update SecondThing newModel

SecondThing ->
  let 
    newModel = { model | -- update the record properties -- }
  in
    update ThirdThing newModel

ThirdThing ->
  let 
    newModel = { model | -- update the record properties -- }
  in
    update LastThing newModel

LastThing ->
  let 
    newModel = { model | -- update the record properties -- }
  in
    newModel ! [ ]


But... isn't this similar to using a GOTO statement in BASIC?
(Hey, I love BASIC, but... !)
I've found that when I have a few of these chained in sequence, spaghetti code is the guaranteed result.

Is there someway to manage this sequencing in some kind of centralized way?
For example, something like:

SequenceOfSteps ->
  FirstThing 
  SecondThing
  ThirdThing
  LastThing

It would be nice to do this so that I can selectively insert or remove steps while testing my application.

Is this possible?
Any advice or opinions?

Simon

unread,
Dec 2, 2016, 12:28:40 PM12/2/16
to Elm Discuss

I think you want to do something like:

 { model | -- update the record properties -- }
|> update SecondThing 
|> update ThirdThing 
|> update FourthThing

Rex van der Spuy

unread,
Dec 2, 2016, 1:10:20 PM12/2/16
to Elm Discuss

Oh, now it seems obvious! :)
Thanks so much!

Desmond D'Souza

unread,
Dec 4, 2016, 3:51:22 PM12/4/16
to Elm Discuss
There is a more detailed explanation here and here and here.
Reply all
Reply to author
Forward
0 new messages