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

updateAt/setAt/swapAt could return a List (not a Maybe List) #32

Closed cbenz closed 7 years ago

cbenz commented 7 years ago

Several functions like removeAt : Int -> List a -> List a or updateIfIndex : (Int -> Bool) -> (a -> a) -> List a -> List a return a List a and that's fine (from my point of view)!

On the other hand, updateAt : Int -> (a -> a) -> List a -> Maybe (List a) returns a Maybe (List a) which is annoying.

May we change this to updateAt : Int -> (a -> a) -> List a -> List a?

The same remark applies to setAt and swapAt.

These functions could return the original list when the index is invalid.

What do you think about it? Shall I send a pull-request in that direction?


Use case:

numbers = [14, 42, 99]

numbers |> updateIfIndex ((==) 0) (\n -> n + 1)
-- should be the same than
numbers |> updateAt 0 (\n -> n + 1)

-- but with current we have to do:
numbers |> updateAt 0 (\n -> n + 1) |> Maybe.withDefault numbers
mgold commented 7 years ago

Seems reasonable to me. Was there any reason why you'd want to know if the index supplied was invalid?

cbenz commented 7 years ago

No particular reason for me.

So I'm going to start a PR.