Hi Wouten,
yes, my fourth code snippet is wrong, it should contain an Int instead of
an Item, thanks.
Also, thanks for your input. In fact, the sorting was poorly described by
me. The key I sort the items is in fact different from the "lookup key", I
just missed to mention that explicitly.
I'm pretty sure that List.filter returns a List, but I assume you call
List.head in getItem as well. I'm running with a Dict right now for lookup,
and whenever I need to show the collection to the user (this is the use
case where I want a special order), I sort the dict's values.
On Monday, August 15, 2016 at 10:58:34 AM UTC+2, Wouter In t Velt wrote:
>
> I would advise against storing the item itself in model.itemActive (Option
> 1). This would be storing the same data in 2 places, which is not good.
> You really need 2 elements:
>
> 1. A key to identify the item: either A: the index of the item in the
> collection or B: a separate key that you store in the item
> 2. A key to sort the item: either A: the index of the item in the
> collection (i.e. the collection is always in the right order) or B: a
> separate sortKey stored in the item
>
> With your Option 2, it is not possible to make the model.itemActive =
> index (1A) AND keep the list in the right order (2A)
> Because you need to update the itemActive every time you resort the List.
> To do this, you need some value to find back the active item in the
> resorted List. And for this, something needs to be a separate
> identification key in the item.
>
> The same applies to the Array and Dict Options. If you keep the collection
> (Array or Dict) always in right order, where do you store the
> identification key?
>
> Whichever option you choose, you either have expensive sorts (Dict and
> Array options), or you have expensive get/sets (List options)
>
> In front-end, which frequent adds/ removes/ sorts etc, I typically go with
> a List-type option.
>
> type alias Model =
> { collection : List Item
> , itemActive : ID
> }
>
> type alias Item =
> { item : String
> , id : ID
> }
>
>
> type alias ID = Int
>
>
> getItem : List Item -> ID -> Maybe Item
> getItem : list id =
> List.filter (\item -> item.id == id) list
>
> PS: Option 4 is titled "array with index" but it the code in fact "array
> with explicit item".
> (You probably meant the code to be with index. Array with explicit item
> would not make much sense, because you lose the fast index-based get and
> set on arrays)
>
--
You received this message because you are subscribed to the Google Groups "Elm
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.