Array vs List in Elm

后端 未结 3 1726
感情败类
感情败类 2021-02-12 12:32

I was suprised to learn that Array and List were two different types in Elm:

  • Array

  • List

In my case,

3条回答
  •  忘掉有多难
    2021-02-12 12:58

    Something like this should work:

    import Array
    import Debug
    
    fromJust : Maybe a -> a
    fromJust x = case x of
        Just y -> y
        Nothing -> Debug.crash "error: fromJust Nothing"
    
    selectFromList : List a -> List Int -> List a
    selectFromList els idxs = 
      let arr = Array.fromList els
       in List.map (\i -> fromJust (Array.get i arr)) idxs
    

    It converts the input list to an array for fast indexing, then maps the list of indices to their corresponding values in the array. I took the fromJust function from this StackOverflow question.

提交回复
热议问题