@@ -32,6 +32,7 @@ module Data.Array
3232 , toUnfoldable
3333 , singleton
3434 , (..), range
35+ , replicate
3536 , some
3637 , many
3738
@@ -100,16 +101,24 @@ module Data.Array
100101 , unzip
101102
102103 , foldM
104+ , foldRecM
105+
106+ , unsafeIndex
107+
108+ , module Exports
103109 ) where
104110
105111import Prelude
106112
107113import Control.Alt ((<|>))
108114import Control.Alternative (class Alternative )
109115import Control.Lazy (class Lazy , defer )
116+ import Control.Monad.Rec.Class (class MonadRec , Step (..), tailRecM2 )
110117
111118import Data.Foldable (class Foldable , foldl , foldr )
119+ import Data.Foldable (foldl , foldr , foldMap , fold , intercalate , elem , notElem , find , findMap , any , all ) as Exports
112120import Data.Maybe (Maybe (..), maybe , isJust , fromJust )
121+ import Data.Traversable (scanl , scanr ) as Exports
113122import Data.Traversable (sequence )
114123import Data.Tuple (Tuple (..))
115124import Data.Unfoldable (class Unfoldable , unfoldr )
@@ -137,6 +146,9 @@ singleton a = [a]
137146-- | Create an array containing a range of integers, including both endpoints.
138147foreign import range :: Int -> Int -> Array Int
139148
149+ -- | Create an array containing a value repeated the specified number of times.
150+ foreign import replicate :: forall a . Int -> a -> Array a
151+
140152-- | An infix synonym for `range`.
141153infix 8 range as ..
142154
@@ -214,13 +226,15 @@ head = uncons' (const Nothing) (\x _ -> Just x)
214226last :: forall a . Array a -> Maybe a
215227last xs = xs !! (length xs - 1 )
216228
217- -- | Get all but the first element of an array, creating a new array, or `Nothing` if the array is empty
229+ -- | Get all but the first element of an array, creating a new array, or
230+ -- | `Nothing` if the array is empty
218231-- |
219232-- | Running time: `O(n)` where `n` is the length of the array
220233tail :: forall a . Array a -> Maybe (Array a )
221234tail = uncons' (const Nothing ) (\_ xs -> Just xs)
222235
223- -- | Get all but the last element of an array, creating a new array, or `Nothing` if the array is empty.
236+ -- | Get all but the last element of an array, creating a new array, or
237+ -- | `Nothing` if the array is empty.
224238-- |
225239-- | Running time: `O(n)` where `n` is the length of the array
226240init :: forall a . Array a -> Maybe (Array a )
@@ -428,8 +442,8 @@ mapWithIndex f xs =
428442sort :: forall a . Ord a => Array a -> Array a
429443sort xs = sortBy compare xs
430444
431- -- | Sort the elements of an array in increasing order, where elements are compared using
432- -- | the specified partial ordering, creating a new array.
445+ -- | Sort the elements of an array in increasing order, where elements are
446+ -- | compared using the specified partial ordering, creating a new array.
433447sortBy :: forall a . (a -> a -> Ordering ) -> Array a -> Array a
434448sortBy comp xs = sortImpl comp' xs
435449 where
@@ -590,8 +604,8 @@ foreign import zipWith
590604 -> Array b
591605 -> Array c
592606
593- -- | A generalization of `zipWith` which accumulates results in some `Applicative`
594- -- | functor.
607+ -- | A generalization of `zipWith` which accumulates results in some
608+ -- | `Applicative` functor.
595609zipWithA
596610 :: forall m a b c
597611 . Applicative m
@@ -602,7 +616,8 @@ zipWithA
602616zipWithA f xs ys = sequence (zipWith f xs ys)
603617
604618-- | Rakes two lists and returns a list of corresponding pairs.
605- -- | If one input list is short, excess elements of the longer list are discarded.
619+ -- | If one input list is short, excess elements of the longer list are
620+ -- | discarded.
606621zip :: forall a b . Array a -> Array b -> Array (Tuple a b )
607622zip = zipWith Tuple
608623
@@ -615,3 +630,18 @@ unzip = uncons' (\_ -> Tuple [] []) \(Tuple a b) ts -> case unzip ts of
615630-- | Perform a fold using a monadic step function.
616631foldM :: forall m a b . Monad m => (a -> b -> m a ) -> a -> Array b -> m a
617632foldM f a = uncons' (\_ -> pure a) (\b bs -> f a b >>= \a' -> foldM f a' bs)
633+
634+ foldRecM :: forall m a b . MonadRec m => (a -> b -> m a ) -> a -> Array b -> m a
635+ foldRecM f a array = tailRecM2 go a 0
636+ where
637+ go res i
638+ | i >= length array = pure (Done res)
639+ | otherwise = do
640+ res' <- f res (unsafePartial (unsafeIndex array i))
641+ pure (Loop { a: res', b: i + 1 })
642+
643+ -- | Find the element of an array at the specified index.
644+ unsafeIndex :: forall a . Partial => Array a -> Int -> a
645+ unsafeIndex = unsafeIndexImpl
646+
647+ foreign import unsafeIndexImpl :: forall a . Array a -> Int -> a
0 commit comments