How do I get a list item by index in elm?

11684

I got a list, and now I want the nth item. In Haskell I would use !!, but I can't find an elm variant of that.

Daniël Heres

There is no equivalent of this in Elm. You could of course implement it yourself.

(Note: This is not a "total" function, so it creates an exception when the index is out of range).

infixl 9 !!
(!!) : [a] -> Int -> a
xs !! n  = head (drop n xs)

A better way would be to define a total function, using the Maybe data type.

infixl 9 !!
(!!) : [a] -> Int -> Maybe a
xs !! n  = 
  if | n < 0     -> Nothing
     | otherwise -> case (xs,n) of
         ([],_)    -> Nothing
         (x::xs,0) -> Just x
         (_::xs,n) -> xs !! (n-1)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I get an item from an icollection via index when in the class contrusctor I set the icollection as a list?

From Dev

How do I get an item from an icollection via index when in the class contrusctor I set the icollection as a list?

From Dev

How do I get the id of a list item?

From Dev

how do I set a list item by index in jinja2

From Dev

How can I get the index of a nested list item?

From Dev

How do I get the current time in Elm?

From Dev

How do I get the current date in elm?

From Dev

How do I get the width of a Form in Elm?

From Dev

How do I get the index of each item in a groupby object in Pandas?

From Dev

How to get List index of tapped List item?

From Dev

how do i get the value of a list item on list item tap in jquery mobile

From Java

How to get the index of an item in a list in a single step?

From Dev

How to get the last index of a specific item in a List?

From Dev

How do I get the list of all terms in a Whoosh index?

From Dev

How do i get the value of first index in a returned List of array

From Dev

How do I get the current time in Elm 0.17/0.18?

From Dev

How do I get keepWhen behaviour in Elm 0.15?

From Dev

How do I get random numbers in Elm 0.13 without a signal?

From Dev

How do I get random numbers in Elm 0.13 without a signal?

From Dev

How do I extract the index or name of the list item within FUN of lapply?

From Dev

In Python, how do I find the index of an sub-item given a list?

From Dev

How do I navigate between List and Item?

From Dev

How i can get GameObject index in the list?

From Dev

How do I get the selected item from drop down list and submit it to my Details view?

From Dev

How do i get id of autocomplete textview out of two autocomplete textview on item selected from its list?

From Dev

how to get the index of dynamically created element (list item)?

From Dev

How to get selected list item in listview by using selected index

From Dev

How to get the index of a dictionary inside list if a specific key item is in the dictionary

From Dev

List<T>.Any(); How to get index of matched item?

Related Related

  1. 1

    How do I get an item from an icollection via index when in the class contrusctor I set the icollection as a list?

  2. 2

    How do I get an item from an icollection via index when in the class contrusctor I set the icollection as a list?

  3. 3

    How do I get the id of a list item?

  4. 4

    how do I set a list item by index in jinja2

  5. 5

    How can I get the index of a nested list item?

  6. 6

    How do I get the current time in Elm?

  7. 7

    How do I get the current date in elm?

  8. 8

    How do I get the width of a Form in Elm?

  9. 9

    How do I get the index of each item in a groupby object in Pandas?

  10. 10

    How to get List index of tapped List item?

  11. 11

    how do i get the value of a list item on list item tap in jquery mobile

  12. 12

    How to get the index of an item in a list in a single step?

  13. 13

    How to get the last index of a specific item in a List?

  14. 14

    How do I get the list of all terms in a Whoosh index?

  15. 15

    How do i get the value of first index in a returned List of array

  16. 16

    How do I get the current time in Elm 0.17/0.18?

  17. 17

    How do I get keepWhen behaviour in Elm 0.15?

  18. 18

    How do I get random numbers in Elm 0.13 without a signal?

  19. 19

    How do I get random numbers in Elm 0.13 without a signal?

  20. 20

    How do I extract the index or name of the list item within FUN of lapply?

  21. 21

    In Python, how do I find the index of an sub-item given a list?

  22. 22

    How do I navigate between List and Item?

  23. 23

    How i can get GameObject index in the list?

  24. 24

    How do I get the selected item from drop down list and submit it to my Details view?

  25. 25

    How do i get id of autocomplete textview out of two autocomplete textview on item selected from its list?

  26. 26

    how to get the index of dynamically created element (list item)?

  27. 27

    How to get selected list item in listview by using selected index

  28. 28

    How to get the index of a dictionary inside list if a specific key item is in the dictionary

  29. 29

    List<T>.Any(); How to get index of matched item?

HotTag

Archive