Elm筛选清单

例子

List.filter : (a -> Bool) -> List a -> List a是一个高阶函数,它将一个参数函数从任何值转换为布尔值,并将该函数应用于给定列表的每个元素,仅保留该函数为其返回的那些元素True。List.filter以第一个参数为函数的函数通常称为谓词。

import String

catStory : List String
catStory = 
    ["a", "crazy", "cat", "walked", "into", "a", "bar"]

-- Any word with more than 3 characters is so long!
isLongWord : String -> Bool
isLongWord string =
   String.lengthstring > 3

longWordsFromCatStory : List String
longWordsFromCatStory = 
   List.filterisLongWord catStory

在中对此进行评估elm-repl:

> longWordsFromCatStory
["crazy", "walked", "into"] : List String
>
>List.filter(String.startsWith "w") longWordsFromCatStory
["walked"] : List String