2

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?

bird
  • 1,872
  • 1
  • 15
  • 32

1 Answers1

9

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.

glennsl
  • 28,186
  • 12
  • 57
  • 75
  • How to determine whether a list is large or small (in respect to the above)? – Isaac Gregson Oct 01 '21 at 08:30
  • That depends entirely on circumstance and constraints. Whether you're doing this just once, or in another loop that is `O(n)`, for example, which would make the whole algorithm `O(n^2)`. If in doubt, it's always best to measure. – glennsl Oct 01 '21 at 09:47