List.map : (a -> b) -> List a -> List b
is a higher-order function that applies a one-parameter function to each element of a list, returning a new list with the modified values.
import String
ourList : List String
ourList =
["wubba", "lubba", "dub", "dub"]
lengths : List Int
lengths =
List.map String.length ourList
-- [5,5,3,3]
slices : List String
slices =
List.map (String.slice 1 3) ourList
-- ["ub", "ub", "ub", "ub"]
If you need to know the index of the elements you can use List.indexedMap : (Int -> a -> b) -> List a -> List b
:
newList : List String
newList =
List.indexedMap (\index element -> String.concat [toString index, ": ", element]) ourList
-- ["0: wubba","1: lubba","2: dub","3: dub"]