elm-community / list-extra

Convenience functions for working with List.
http://package.elm-lang.org/packages/elm-community/list-extra/latest
MIT License
135 stars 59 forks source link

Proposal for splitWhen function #41

Closed EverybodyKurts closed 7 years ago

EverybodyKurts commented 7 years ago

It would combine the following function signatures:

findIndex : (a -> Bool) -> List a -> Maybe Int
splitAt : Int -> List a -> (List a, List a)

Here's a first stab at it:

splitWhen : (a -> Bool) -> List a -> Maybe ( List a, List a )
splitWhen predicate list =
    let
        index =
            findIndex predicate list
    in
        case index of
            Just i ->
                Just (splitAt i list)

            Nothing ->
                Nothing
mgold commented 7 years ago

How about:

splitWhen : (a -> Bool) -> List a -> Maybe ( List a, List a )
splitWhen predicate list =
    findIndex predicate list
    |> Maybe.map (\i -> splitAt i list)
EverybodyKurts commented 7 years ago

Wow, way shorter, lol. Looks good to me. Would love to be able to submit a pull request to this repo (with tests included). You think that'd work?

mgold commented 7 years ago

Sure.

(This repo has no maintainer so I'll go ahead and handle this.)