Created
January 27, 2019 10:24
-
-
Save robinheghan/c77721f25c1ee60ef6a8ffca38b97319 to your computer and use it in GitHub Desktop.
Potential optimization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main exposing (main) | |
{- | |
So I just went over my projects at work, and my hobby project. | |
This gist is an example of a common way for me, and my colleagues at work, to structure progrograms: | |
src/Main.elm <-- Top-level model-view-update | |
src/VippsModal.elm <-- Popup modal, for paying with a norwegian payment solution, has its own model-view-update | |
src/Data/UserInfo.elm <-- The UserInfo alias, and methods for working on that data | |
src/Backend.elm <-- For communicating with the backend | |
-} | |
import Html | |
import UserInfo exposing (UserInfo) | |
import VippsModal | |
type alias Model = | |
{ userInfo : UserInfo | |
, vippsModalModel : VippsModal.Model | |
} | |
type Msg | |
= EnterUserName String | |
| EnterPhoneNumber String | |
| VippsModalMsg VippsModal.Msg | |
update : Msg -> Model -> (Model, Cmd Msg) | |
update msg model = | |
case msg of | |
EnterUserName name -> | |
-- Even if `UserInfo` returns the same object here, because the user tried to have a # in the name or something, | |
-- we'll allocate a new object with different identity than the last. | |
( { model | userInfo = UserInfo.updateName name model.userInfo } | |
, Cmd.none | |
) | |
EnterPhoneNumber number -> | |
-- Even if `UserInfo` returns the same object here, because the user made a bad input or something, | |
-- we'll allocate a new object with different identity than the last. | |
( { model | userInfo = UserInfo.updatePhone number model.userInfo } | |
, Cmd.none | |
} | |
VippsMsg vippsMsg -> | |
let | |
( updatedVippsModal, vippsCmd ) = | |
VippsModal.update vippsMsg model.vippsModalModel | |
in | |
-- Even if `VippsModal.update` returns the same object as it was passed in, because it only wanted to perform a | |
-- side-effect or something, we'll still allocate a new modal with different identity than the last. | |
( { model | vippsModalModel = updatedVippsModal } | |
, Cmd.map VippsMsg vippsCmd | |
) | |
-- And then comes the view and main functions, which aren't of much interest to this example |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Today, update is implemented as: | |
function update(original, patch) { | |
var updated = {}; | |
for (var key in original) { | |
updated[key] = original[key]; | |
} | |
for (var key in patch) { | |
updated[key] = patch[key] | |
} | |
return updated; | |
} | |
*/ | |
// Alternatively, we could do: | |
function copyObj(original) { | |
var copy = {}; | |
for (var key in original) { | |
copy[key] = original[key]; | |
} | |
return copy; | |
} | |
function update(original, patch) { | |
var updated = copyObj(original); | |
for (var key in patch) { | |
updated[key] = patch[key] | |
} | |
return updated; | |
} | |
function update1(key, value, original) { | |
if (original[key] === value) { | |
return original; | |
} | |
var updated = copyObj(original); | |
updated[key] = value; | |
return updated; | |
} | |
// and then something similar for update2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment