如何从列表和(a - > Bool)构建一个SelectList?

时间:2018-04-05 15:18:28

标签: elm

我使用的是elm-spa-example模型,我的Session包含List Recipe以及包含recipeID的模型

我想构建一个SelectList来选择已在列表中获得RecipeID的食谱。

理想情况下,我会使用类似的东西:

SelectList.selectFromList : (a -> Bool) -> List a -> SelectList a

我的情况我会这样做:

SelectList.selectFromList (\recipe -> recipe.id == recipeID) session.recipes

3 个答案:

答案 0 :(得分:2)

我做了类似的事情:

selectFromList : (a -> Bool) -> List a -> Maybe (SelectList a)
selectFromList isSelectable list = 
    case list of
        first :: rest ->
            SelectList.fromLists [] first rest
                |> SelectList.select isSelectable
                |> Just
        [] ->
            Nothing

我还补充说:

prev : SelectList a -> Maybe a
prev list =
    SelectList.before list
        |> List.reverse
        |> List.head


next : SelectList a -> Maybe a
next list =
    SelectList.after list
        |> List.head

答案 1 :(得分:1)

我把这个快速的ellie放在一起,说明了我认为实现你想要的步骤所需的步骤。它肯定没有优化,甚至不是惯用的。

https://ellie-app.com/4TJVgSCwXa1/0

firstPartialList = takeWhile condition myList
selected = Maybe.withDefault "" (getAt (length firstPartialList) myList)
secondPartialList = drop ((length firstPartialList) + 1) myList

mySelectList = SelectList.fromLists firstPartialList selected secondPartialList

condition = (\item -> item /= otherItem)


myList = ["a", "b", "c", "d"]

otherItem = "b"

答案 2 :(得分:0)

SelectList没有公开selectFromList你确定链接是对的吗?