Skip to content

Commit e2eecf5

Browse files
authored
Stop doing a pointless Array conversion
## Summary Just do one traversal to select a new row. This improves over the previous approach which did three traversals, adding code complexity and slowing things down. ## Details The previous code was scanning the entire list to deselect, and *then* doing a conversion to Array, updating the Array, and converting back into a List. This is a bad design for a couple reasons: 1. This update could happen in the initial list traversal! The program ends up doing a ton of extra work to convert to and from Arrays and the end result is that the program is slower *and* the code is more complex. 2. It requires a dependency on the Array.Extra package. No need to bring that in otherwise.
1 parent f37bbd4 commit e2eecf5

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

elm-v0.18.0/src/Main.elm

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module Main exposing (..)
22

33
import Array exposing (Array)
4-
import Array.Extra
54
import Html exposing (Html, Attribute, program, div, a, h1, span, button, table, td, tr, text)
65
import Html.Attributes exposing (id, class, classList, attribute, type_, href)
76
import Html.Events exposing (onClick)
@@ -312,32 +311,22 @@ update msg model =
312311
)
313312

314313
Select index ->
315-
( { model
316-
| rows =
317-
model.rows
318-
|> List.map
319-
(\row ->
320-
if row.selected == True then
321-
{ row | selected = False }
322-
else
323-
row
324-
)
325-
|> Array.fromList
326-
|> Array.Extra.update index
327-
(\row ->
328-
{ row
329-
| selected = True
330-
}
331-
)
332-
|> Array.toList
333-
}
334-
, Cmd.none
335-
)
314+
( { model | rows = List.indexedMap (select index) model.rows }, Cmd.none )
336315

337316
UpdateSeed seed ->
338317
( { model | seed = Just seed }, Cmd.none )
339318

340319

320+
select : Int -> Int -> Row -> Row
321+
select targetIndex index ({ id, label, selected } as row) =
322+
if index == targetIndex then
323+
Row id label True
324+
else if selected == True then
325+
Row id label False
326+
else
327+
row
328+
329+
341330
type alias Model =
342331
{ seed : Maybe Seed
343332
, rows : List Row

0 commit comments

Comments
 (0)