Natim
Natim

Reputation: 18112

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

I am using elm-spa-example model and I have a Session that contains a List Recipe as well as a Model that contains a recipeID

I'd like to build a SelectList that would select the recipe that has got the RecipeID in the list.

Ideally I would use something like:

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

I my case I would do:

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

Upvotes: 1

Views: 96

Answers (3)

Natim
Natim

Reputation: 18112

I did something like that:

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

I also added:

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

Upvotes: 2

stevensonmt
stevensonmt

Reputation: 732

I put together this quick ellie that illustrates what I think are the steps needed to achieve what you want. It's certainly not optimized or even idiomatic.

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"

Upvotes: 1

JosephStevens
JosephStevens

Reputation: 1706

SelectList doesn't expose selectFromList are you sure the link is right?

Upvotes: 0

Related Questions