gren-lang / core

Gren's core package
https://packages.gren-lang.org/package/gren-lang/core/version/latest/overview
Other
35 stars 8 forks source link

Add `Array.update` or similar #61

Closed mbartlett21 closed 1 year ago

mbartlett21 commented 1 year ago

This would be useful for making modifications to individual items in an array.

I am currently using the below definition to do this:

{-| Updates the element at the given index using the provided function,
or returns the array unmodified if the index is out of bounds.

    arrayUpdate 1 (\a -> a * 10) [ 1, 2, 3 ] == [ 1, 20, 3 ]
    arrayUpdate 10 (\a -> a * 10) [ 1, 2, 3 ] == [ 1, 2, 3 ]

 -}
arrayUpdate : Int -> (a -> a) -> Array a -> Array a
arrayUpdate i f arr =
    case Array.get i arr of
        Just v ->
            Array.set i (f v) arr

        Nothing ->
            arr
robinheghan commented 1 year ago

Fixed in upcoming release.