@@ -32,6 +32,7 @@ module Data.Array
32
32
, toUnfoldable
33
33
, singleton
34
34
, (..), range
35
+ , replicate
35
36
, some
36
37
, many
37
38
@@ -100,16 +101,24 @@ module Data.Array
100
101
, unzip
101
102
102
103
, foldM
104
+ , foldRecM
105
+
106
+ , unsafeIndex
107
+
108
+ , module Exports
103
109
) where
104
110
105
111
import Prelude
106
112
107
113
import Control.Alt ((<|>))
108
114
import Control.Alternative (class Alternative )
109
115
import Control.Lazy (class Lazy , defer )
116
+ import Control.Monad.Rec.Class (class MonadRec , Step (..), tailRecM2 )
110
117
111
118
import Data.Foldable (class Foldable , foldl , foldr )
119
+ import Data.Foldable (foldl , foldr , foldMap , fold , intercalate , elem , notElem , find , findMap , any , all ) as Exports
112
120
import Data.Maybe (Maybe (..), maybe , isJust , fromJust )
121
+ import Data.Traversable (scanl , scanr ) as Exports
113
122
import Data.Traversable (sequence )
114
123
import Data.Tuple (Tuple (..))
115
124
import Data.Unfoldable (class Unfoldable , unfoldr )
@@ -137,6 +146,9 @@ singleton a = [a]
137
146
-- | Create an array containing a range of integers, including both endpoints.
138
147
foreign import range :: Int -> Int -> Array Int
139
148
149
+ -- | Create an array containing a value repeated the specified number of times.
150
+ foreign import replicate :: forall a . Int -> a -> Array a
151
+
140
152
-- | An infix synonym for `range`.
141
153
infix 8 range as ..
142
154
@@ -214,13 +226,15 @@ head = uncons' (const Nothing) (\x _ -> Just x)
214
226
last :: forall a . Array a -> Maybe a
215
227
last xs = xs !! (length xs - 1 )
216
228
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
218
231
-- |
219
232
-- | Running time: `O(n)` where `n` is the length of the array
220
233
tail :: forall a . Array a -> Maybe (Array a )
221
234
tail = uncons' (const Nothing ) (\_ xs -> Just xs)
222
235
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.
224
238
-- |
225
239
-- | Running time: `O(n)` where `n` is the length of the array
226
240
init :: forall a . Array a -> Maybe (Array a )
@@ -428,8 +442,8 @@ mapWithIndex f xs =
428
442
sort :: forall a . Ord a => Array a -> Array a
429
443
sort xs = sortBy compare xs
430
444
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.
433
447
sortBy :: forall a . (a -> a -> Ordering ) -> Array a -> Array a
434
448
sortBy comp xs = sortImpl comp' xs
435
449
where
@@ -590,8 +604,8 @@ foreign import zipWith
590
604
-> Array b
591
605
-> Array c
592
606
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.
595
609
zipWithA
596
610
:: forall m a b c
597
611
. Applicative m
@@ -602,7 +616,8 @@ zipWithA
602
616
zipWithA f xs ys = sequence (zipWith f xs ys)
603
617
604
618
-- | 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.
606
621
zip :: forall a b . Array a -> Array b -> Array (Tuple a b )
607
622
zip = zipWith Tuple
608
623
@@ -615,3 +630,18 @@ unzip = uncons' (\_ -> Tuple [] []) \(Tuple a b) ts -> case unzip ts of
615
630
-- | Perform a fold using a monadic step function.
616
631
foldM :: forall m a b . Monad m => (a -> b -> m a ) -> a -> Array b -> m a
617
632
foldM 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