Ah, I see now :) Yeah, elm requiring exhaustiveness for things like dict lookups can be a pain, but it will really save you on large codebases. I use Maybe.withDefault quite a bit in those cases. You could define emptyLine somewhere in your code and just do Maybe.withDefault emptyLine (Dict.get name model.lines)
Using dicts in how you're using them makes sense. You might be able to use just a list instead of a dict, though I'm not sure how much (if anything) that would gain you. It might be cleaner, it might not. You might want to take a look at the Extra packages which add more functions to the standard library. So, http://package.elm-lang.org/packages/elm-community/list-extra/2.0.1/List-Extra On a separate note, one thing that took me a bit to discover in elm (and you may already be aware of) was using 'pipeline' style using |>. [0..20] |> List.map ((+) 5) |> List.filter (\x -> x `rem` 2 == 0) |> List.intersperse 5 |> List.foldl (+) 0 For me this is very powerful because its so easily extensible and readable. I think if you converted the above to a list comprehension it'd be a bit of a beast :) That being said, for small stuff list comprehensions are nice for sure. I use the same pipeline style in my animation library as a way to specify keyframes. https://github.com/mdgriffith/elm-style-animation -Matt On Friday, June 24, 2016 at 9:09:42 AM UTC-4, Matthieu Amiguet wrote: > > Hi Matt, > > Thanks for your nice answer! > > > As for Dicts, I would say double check to make sure you don't > > actually want a record. Basically if you know all the fields at > > compile time...you probably want a record, not a dict. > > I may have an habit of using dicts too often... (in python, almost > everything is a dict anyway ;-) ) > > What do you think? in my code I have > > type alias Model = > { lines : Dict String Line > , from : String > , to : String > [...] > } > > > type alias Line = > { name : String > , from : String > , to : String > [...] > } > > The Dict content is static, but I chose a Dict in order to be able to > iterate over it, as in > > getTimes : Model -> Cmd Msg > getTimes model = > Cmd.batch <| List.map getLineTimes (Dict.values model.lines) > > I also need to find a line from its name because each line has its own > Http.get and needs to get the value back > > type Msg > = FetchSucceed String (List String) > | ... > > > update : Msg -> Model -> ( Model, Cmd Msg ) > update action model = > case action of > > FetchSucceed name times -> > { model > | lines = > let > line = > case Dict.get name model.lines of > Just l -> > l > > Nothing -> -- should never occur! > Line name "" "" [] [] Nothing > [...] > in > Dict.insert name newLine model.lines > > > > It seems to me that if I replace my Dict by a record I'll have to deal > with each line separately. But maybe I'm missing something? > > One thing that bothers me is that I must provide some code for the case > when the key is not in the dict, although I know by design that it will not > happen. This could be avoided by using records, of course. > > > So for something like `[i +5 for i in range(20)]` in python, you could > do something like `List.map ((+) 5) [0..20]` > > Right, but I still like how python comprehensions allow to combine map, > filter and cartesian product in a simple, short syntax! > > > Cheers and again, welcome to elm. > > Thanks :-) > > Matthieu > -- 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 elm-discuss+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.