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 `unconsLast` function #99

Closed EverybodyKurts closed 6 years ago

EverybodyKurts commented 6 years ago

It would be defined something like this:

{-| Decompose a list into its last element and the remaining list. If the list is empty, return `Nothing`. Otherwise, return `Just (x, xs)`, where `x` is the last element and `xs` is rest of the list.

    unconsLast [1,2,3] == Just (3, [1,2])
    unconsLast [] = Nothing
-}
unconsLast : List a -> Maybe ( a, List a )
unconsLast =
    List.reverse
        >> uncons
        >> Maybe.andThen (\( lastElem, reversedList ) -> Just ( lastElem, (List.reverse reversedList) ))

I'd be more than happy to create a merge request using the module formatting for documentation and the like. Let me know.

pzp1997 commented 6 years ago

FWIW I believe that in some other communities this is referred to as unsnoc. I think I like your name better though.

EverybodyKurts commented 6 years ago

Lol. It took me a bit but I get it it. It's clever. I wonder if a beginner to Elm or the List.Extra module would be able to find it easily though.

EverybodyKurts commented 6 years ago

Do you think I could create a merge request for the unconsLast function following the repo's contribution guidelines?

pzp1997 commented 6 years ago

Yeah, I think it's probably fine to make a PR for this. Wait for @Chadtech to comment if you want a definitive answer though.

Chadtech commented 6 years ago

Oh shoot. I didnt see this thread, but I did just comment on the PR itself. We can continue this conversation there.

Chadtech commented 6 years ago

Okay we merged unconsLast in #101 so I am closing this.