代码之家  ›  专栏  ›  技术社区  ›  Natim

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

elm
  •  1
  • Natim  · 技术社区  · 6 年前

    我正在使用 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 回复  |  直到 6 年前
        1
  •  2
  •   Natim    6 年前

    我是这样做的:

    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
    
        2
  •  1
  •   stevensonmt    6 年前

    我把这个快速的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"
    
        3
  •  0
  •   JosephStevens    6 年前

    SelectList不公开selectFromList您确定链接正确吗?