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

How to build a SelectList from a List and a (a -> Bool)?

我正在使用 elm-spa-example 模型,我有一个包含 List RecipeSession 以及一个包含 recipeID

的模型

我想构建一个 SelectList 将 select 已在列表中获得 RecipeID 的食谱。

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

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

我的情况是:

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

SelectList 不公开 selectFromList 您确定 link 正确吗?

我做了类似的事情:

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

我把这个快速的 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"