Elm转换列表的每个元素
示例
List.map:(a->b)->Lista->Listb是一个高阶函数,它将一个参数函数应用于列表的每个元素,并返回具有修改后值的新列表。
import String
ourList : List String
ourList =
["wubba", "lubba", "dub", "dub"]
lengths : List Int
lengths =
List.mapString.length ourList
-- [5,5,3,3]
slices : List String
slices =
List.map(String.slice 1 3) ourList
-- ["ub", "ub", "ub", "ub"]如果您需要知道元素的索引,可以使用List.indexedMap:(Int->a->b)->Lista->Listb:
newList : List String newList = List.indexedMap(\index element ->String.concat[toString index, ": ", element]) ourList -- ["0: wubba","1: lubba","2: dub","3: dub"]
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短