Search code examples
listelm

Spread operator in Elm


I want to show a select box's options from a list with 2 existing options.

What I want seems like this: (spread operator in JS)

select
  [ class "js-wlSize" ]
  [ option [ disabled True ] [ text "Choose size" ]
  , option [] [ text "Default size" ]
    ...( List.map (\s -> option [ value s ] [ text s] ) myListData )
  ]

I also tried with ( :: ), but It worked for 1 default option. I don't know how to work with 2 and more.

Can anyone know how to archive this in Elm?


Solution

  • You can chain the cons operator, e.g.:

    1 :: 2 :: [ 3, 4, 5 ]
    

    But if you have a list then the append operator, ++, might be more appropriate:

    [ 1, 2 ] ++ [ 3, 4, 5 ]
    

    Note however that append is very inefficient on lists, especially compared to cons. Cons is O(1) while append is O(n), see Why is appending to a list bad?. This is insignificant for small lists, but you might want to reconsider your approach if you find yourself wanting to use append on large lists.