@@ -59,7 +59,9 @@ module Data.Array
59
59
, insertAt
60
60
, deleteAt
61
61
, updateAt
62
+ , updateAtIndices
62
63
, modifyAt
64
+ , modifyAtIndices
63
65
, alterAt
64
66
65
67
, reverse
@@ -115,15 +117,15 @@ import Control.Alternative (class Alternative)
115
117
import Control.Lazy (class Lazy , defer )
116
118
import Control.Monad.Rec.Class (class MonadRec , Step (..), tailRecM2 )
117
119
import Control.Monad.ST (pureST )
118
- import Data.Array.ST (unsafeFreeze , emptySTArray , pushSTArray )
120
+ import Data.Array.ST (unsafeFreeze , emptySTArray , pokeSTArray , pushSTArray , modifySTArray , withArray )
119
121
import Data.Array.ST.Iterator (iterator , iterate , pushWhile )
120
- import Data.Foldable (class Foldable , foldl , foldr )
122
+ import Data.Foldable (class Foldable , foldl , foldr , traverse_ )
121
123
import Data.Foldable (foldl , foldr , foldMap , fold , intercalate , elem , notElem , find , findMap , any , all ) as Exports
122
124
import Data.Maybe (Maybe (..), maybe , isJust , fromJust )
123
125
import Data.NonEmpty (NonEmpty , (:|))
124
126
import Data.Traversable (scanl , scanr ) as Exports
125
127
import Data.Traversable (sequence , traverse )
126
- import Data.Tuple (Tuple (..))
128
+ import Data.Tuple (Tuple (..), uncurry )
127
129
import Data.Unfoldable (class Unfoldable , unfoldr )
128
130
import Partial.Unsafe (unsafePartial )
129
131
@@ -445,6 +447,17 @@ mapWithIndex :: forall a b. (Int -> a -> b) -> Array a -> Array b
445
447
mapWithIndex f xs =
446
448
zipWith f (range 0 (length xs - 1 )) xs
447
449
450
+ -- | Change the elements at the specified indices in index/value pairs.
451
+ -- | Out-of-bounds indices will have no effect.
452
+ updateAtIndices :: forall t a . Foldable t => t (Tuple Int a ) -> Array a -> Array a
453
+ updateAtIndices us xs =
454
+ pureST (withArray (\res -> traverse_ (uncurry $ pokeSTArray res) us) xs)
455
+
456
+ -- | Apply a function to the element at the specified indices,
457
+ -- | creating a new array. Out-of-bounds indices will have no effect.
458
+ modifyAtIndices :: forall t a . Foldable t => t Int -> (a -> a ) -> Array a -> Array a
459
+ modifyAtIndices is f xs =
460
+ pureST (withArray (\res -> traverse_ (\i -> modifySTArray res i f) is) xs)
448
461
449
462
-- ------------------------------------------------------------------------------
450
463
-- Sorting ---------------------------------------------------------------------
0 commit comments