Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Ok let's see. Please can you convert this Vue code into Elm code?

https://pastebin.com/trFAuJ7T

Good luck!



You couldn't have picked an easier example!

https://pastebin.com/QQARqeB9

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.


    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],
      ]


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?


It was presented to him as a puzzle and he solved the puzzle.

How far do you think he got in Elm?



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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: