Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upFunction Request : Array.update #298
Comments
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
sindikat
commented
Aug 23, 2015
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
evancz
Aug 23, 2015
Member
Following these guidelines I am going to close this, but I agree that this function would be very valuable and probably should be built in.
|
Following these guidelines I am going to close this, but I agree that this function would be very valuable and probably should be built in. |
evancz
closed this
Aug 23, 2015
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
jinjor
Jan 11, 2017
Contributor
I was about to make the same issue, but maybe we should consider consistency with Dict.update.
Alternative plan:
update: Int -> (Maybe a -> Maybe a) -> Array a -> Array a
Personally, I want both.
|
I was about to make the same issue, but maybe we should consider consistency with Dict.update. Alternative plan: Personally, I want both. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
boxofrox
Feb 4, 2017
@jinjor, I think your update signature needs a few more details as to how it should be implemented.
For Dict, the (Maybe a -> Maybe a) argument allows a user to assign a default value if Nothing exists at the index.
For an Array, how would that work if your index is out of bounds?
For example, what does the following produce?
things = Array.initialize 5 identity
--=> Array.fromList [ 0, 1, 2, 3, 4 ]
addOrDefault : Int -> Int -> Maybe Int -> Maybe Int
addOrDefault x default y =
case y of
Nothing -> Just default
Just yy -> Just x + yy
Array.update 25 (addOrDefault 5 0) things
--=> ????- Do you have a sparse array? Does iteration skip over indices 5 through 23?
- Do you have a dense array? What value did you fill in for indices 5 through 23?
The benefit of update : Int -> (a -> a) -> Array a -> Array a is that it's simple, useful, and has practically one implementation. Given your signature has tradeoffs and concerns beyond "change value at this index", it may belong in a separate library.
boxofrox
commented
Feb 4, 2017
•
|
@jinjor, I think your For For an For example, what does the following produce? things = Array.initialize 5 identity
--=> Array.fromList [ 0, 1, 2, 3, 4 ]
addOrDefault : Int -> Int -> Maybe Int -> Maybe Int
addOrDefault x default y =
case y of
Nothing -> Just default
Just yy -> Just x + yy
Array.update 25 (addOrDefault 5 0) things
--=> ????
The benefit of |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
@boxofrox Ah, that's fair. You are right. |
TheSeamau5 commentedJul 16, 2015
The following function would be very valuable and it would be best if the implementation were native:
This function would be analogous to
Array.setand would be strictly equivalent to the following implementation using bothArray.setandArray.get.(so, here's a good unit/property-based test for the update function).
The goal of having a native implementation is to avoid getting and setting in two distinct steps. This function would prove invaluable in the update step of the Elm Architecture
This would avoid the troublesome need of using functions like
indexedMapto do this sort of work, and a native implementation could fuse the setting and the getting step of this operation in a way that isn't possible currently.