Re:Elm 0.19.1
by RussianCow on Monday October 21, 2019 @01:49PM
(#21315130)
You couldn't have picked an easier example!
FWIW, I wrote this without having previous Elm experience—the compiler's error messages are so good that it was mostly trial and error until it worked.
To your point: I think using language primitives for defining views instead of HTML templating is an advantage for Elm because you can write them using only the Elm language itself, without having to learn yet another syntax. Combined with the excellent compiler error messages, it makes writing views easier and less error prone than writing HTML templates.
Re:Elm 0.19.1
by lalaithion on Monday October 21, 2019 @01:47PM
(#21315111)
showUsers : [User] -> Html msg
showUsers users = div [id "my-component"] [ul [] (map showUser users)]
showUser : User -> Html msg
showUser user = li [] [
span [class "firstName"] [text user.firstName],
br [] [],
span [class "lastName"] [text user.lastName],
]
Re:Elm 0.19.1
by hombre_fatal on Monday October 21, 2019 @01:57PM
(#21315223)
The fact that you think this is some sort of puzzle makes me question how far you actually got with Elm at all.
What's even the "good luck!" part for? Good luck knowing about List.map?
Re:Elm 0.19.1
by KurtMueller on Monday October 21, 2019 @02:31PM
(#21315603)
It was presented to him as a puzzle and he solved the puzzle.
How far do you think he got in Elm?
Re:Elm 0.19.1
by MichaelGlass on Monday October 21, 2019 @01:22PM
(#21314817)
Re:Elm 0.19.1
by subsaharancoder on Wednesday October 23, 2019 @02:46PM
(#21337571)
Even easier in Clojurescript using Hiccup https://github.com/weavejester/hiccup to render HTML:
(def users
[{:firstname "john", :lastname "snow"} {:firstname "Peter",
:lastname "drucker"}])
(defn my-view
[]
[:ul
(for [{:keys [firstname lastname]} users]
[:li firstname " " lastname])])As others have shown, Elm is actually super good at stuff like this because you can just write resuable pure Elm functions using functional primitives like map and filter.
An ellie app to play around with:
https://ellie-app.com/6YQNRR4MmmZa1
https://pastebin.com/trFAuJ7T
Good luck!