我使用的是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
答案 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你确定链接是对的吗?