@@ -59,7 +59,9 @@ module Data.Array
5959 , insertAt
6060 , deleteAt
6161 , updateAt
62+ , updateAtIndices
6263 , modifyAt
64+ , modifyAtIndices
6365 , alterAt
6466
6567 , reverse
@@ -115,15 +117,15 @@ import Control.Alternative (class Alternative)
115117import Control.Lazy (class Lazy , defer )
116118import Control.Monad.Rec.Class (class MonadRec , Step (..), tailRecM2 )
117119import Control.Monad.ST (pureST )
118- import Data.Array.ST (unsafeFreeze , emptySTArray , pushSTArray )
120+ import Data.Array.ST (unsafeFreeze , emptySTArray , pokeSTArray , pushSTArray , modifySTArray , withArray )
119121import Data.Array.ST.Iterator (iterator , iterate , pushWhile )
120- import Data.Foldable (class Foldable , foldl , foldr )
122+ import Data.Foldable (class Foldable , foldl , foldr , traverse_ )
121123import Data.Foldable (foldl , foldr , foldMap , fold , intercalate , elem , notElem , find , findMap , any , all ) as Exports
122124import Data.Maybe (Maybe (..), maybe , isJust , fromJust )
123125import Data.NonEmpty (NonEmpty , (:|))
124126import Data.Traversable (scanl , scanr ) as Exports
125127import Data.Traversable (sequence , traverse )
126- import Data.Tuple (Tuple (..))
128+ import Data.Tuple (Tuple (..), uncurry )
127129import Data.Unfoldable (class Unfoldable , unfoldr )
128130import Partial.Unsafe (unsafePartial )
129131
@@ -445,6 +447,17 @@ mapWithIndex :: forall a b. (Int -> a -> b) -> Array a -> Array b
445447mapWithIndex f xs =
446448 zipWith f (range 0 (length xs - 1 )) xs
447449
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)
448461
449462-- ------------------------------------------------------------------------------
450463-- Sorting ---------------------------------------------------------------------
0 commit comments