@@ -18,6 +18,8 @@ module Data.Array
18
18
19
19
, (:), cons
20
20
, snoc
21
+ , insert
22
+ , insertBy
21
23
22
24
, head
23
25
, last
@@ -34,6 +36,7 @@ module Data.Array
34
36
, deleteAt
35
37
, updateAt
36
38
, modifyAt
39
+ , alterAt
37
40
38
41
, reverse
39
42
, concat
@@ -58,6 +61,8 @@ module Data.Array
58
61
59
62
, nub
60
63
, nubBy
64
+ -- , union
65
+ -- , unionBy
61
66
, delete
62
67
, deleteBy
63
68
@@ -85,6 +90,8 @@ import Data.Monoid (Monoid, mempty)
85
90
import Data.Traversable (sequence )
86
91
import Data.Tuple (Tuple (..))
87
92
93
+ import qualified Data.Maybe.Unsafe as U
94
+
88
95
-- ------------------------------------------------------------------------------
89
96
-- Array creation --------------------------------------------------------------
90
97
-- ------------------------------------------------------------------------------
@@ -162,6 +169,17 @@ infixr 6 :
162
169
-- | Append an element to the end of an array, creating a new array.
163
170
foreign import snoc :: forall a . Array a -> a -> Array a
164
171
172
+ -- | Insert an element into a sorted array.
173
+ insert :: forall a . (Ord a ) => a -> Array a -> Array a
174
+ insert = insertBy compare
175
+
176
+ -- | Insert an element into a sorted array, using the specified function to
177
+ -- | determine the ordering of elements.
178
+ insertBy :: forall a . (a -> a -> Ordering ) -> a -> Array a -> Array a
179
+ insertBy cmp x ys =
180
+ let index = maybe 0 (+ 1 ) (findLastIndex (\y -> cmp x y == GT ) ys)
181
+ in U .fromJust (insertAt index x ys)
182
+
165
183
-- ------------------------------------------------------------------------------
166
184
-- Non-indexed reads -----------------------------------------------------------
167
185
-- ------------------------------------------------------------------------------
@@ -236,8 +254,8 @@ findIndex = findIndexImpl Just Nothing
236
254
foreign import findIndexImpl :: forall a . (forall b . b -> Maybe b )
237
255
-> (forall b . Maybe b )
238
256
-> (a -> Boolean )
239
- -> ( Array a )
240
- -> ( Maybe Int )
257
+ -> Array a
258
+ -> Maybe Int
241
259
242
260
-- | Find the last index for which a predicate holds.
243
261
findLastIndex :: forall a . (a -> Boolean ) -> Array a -> Maybe Int
@@ -246,21 +264,60 @@ findLastIndex = findLastIndexImpl Just Nothing
246
264
foreign import findLastIndexImpl :: forall a . (forall b . b -> Maybe b )
247
265
-> (forall b . Maybe b )
248
266
-> (a -> Boolean )
249
- -> ( Array a )
250
- -> ( Maybe Int )
267
+ -> Array a
268
+ -> Maybe Int
251
269
252
- -- | Insert an element at the specified index, creating a new array.
253
- foreign import insertAt :: forall a . Int -> a -> Array a -> Array a
270
+ -- | Insert an element at the specified index, creating a new array, or
271
+ -- | returning `Nothing` if the index is out of bounds.
272
+ insertAt :: forall a . Int -> a -> Array a -> Maybe (Array a )
273
+ insertAt = _insertAt Just Nothing
254
274
255
- -- | Delete the element at the specified index, creating a new array.
256
- foreign import deleteAt :: forall a . Int -> Int -> Array a -> Array a
275
+ foreign import _insertAt :: forall a . (forall b . b -> Maybe b )
276
+ -> (forall b . Maybe b )
277
+ -> Int
278
+ -> a
279
+ -> Array a
280
+ -> Maybe (Array a )
257
281
258
- -- | Change the element at the specified index, creating a new array.
259
- foreign import updateAt :: forall a . Int -> a -> Array a -> Array a
282
+ -- | Delete the element at the specified index, creating a new array, or
283
+ -- | returning `Nothing` if the index is out of bounds.
284
+ deleteAt :: forall a . Int -> Array a -> Maybe (Array a )
285
+ deleteAt = _deleteAt Just Nothing
260
286
261
- -- | Apply a function to the element at the specified index, creating a new array.
262
- modifyAt :: forall a . Int -> (a -> a ) -> Array a -> Array a
263
- modifyAt i f xs = maybe xs (\x -> updateAt i (f x) xs) (xs !! i)
287
+ foreign import _deleteAt :: forall a . (forall b . b -> Maybe b )
288
+ -> (forall b . Maybe b )
289
+ -> Int
290
+ -> Array a
291
+ -> Maybe (Array a )
292
+
293
+ -- | Change the element at the specified index, creating a new array, or
294
+ -- | returning `Nothing` if the index is out of bounds.
295
+ updateAt :: forall a . Int -> a -> Array a -> Maybe (Array a )
296
+ updateAt = _updateAt Just Nothing
297
+
298
+ foreign import _updateAt :: forall a . (forall b . b -> Maybe b )
299
+ -> (forall b . Maybe b )
300
+ -> Int
301
+ -> a
302
+ -> Array a
303
+ -> Maybe (Array a )
304
+
305
+ -- | Apply a function to the element at the specified index, creating a new
306
+ -- | array, or returning `Nothing` if the index is out of bounds.
307
+ modifyAt :: forall a . Int -> (a -> a ) -> Array a -> Maybe (Array a )
308
+ modifyAt i f xs = maybe Nothing go (xs !! i)
309
+ where
310
+ go x = updateAt i (f x) xs
311
+
312
+ -- | Update or delete the element at the specified index by applying a
313
+ -- | function to the current value, returning a new array or `Nothing` if the
314
+ -- | index is out-of-bounds.
315
+ alterAt :: forall a . Int -> (a -> Maybe a ) -> Array a -> Maybe (Array a )
316
+ alterAt i f xs = maybe Nothing go (xs !! i)
317
+ where
318
+ go x = case f x of
319
+ Nothing -> deleteAt i xs
320
+ Just x' -> updateAt i x' xs
264
321
265
322
-- ------------------------------------------------------------------------------
266
323
-- Transformations -------------------------------------------------------------
@@ -419,7 +476,7 @@ delete = deleteBy eq
419
476
-- | new array.
420
477
deleteBy :: forall a . (a -> a -> Boolean ) -> a -> Array a -> Array a
421
478
deleteBy _ _ [] = []
422
- deleteBy eq x ys = maybe ys (\i -> deleteAt i one ys) (findIndex (eq x) ys)
479
+ deleteBy eq x ys = maybe ys (\i -> U .fromJust $ deleteAt i ys) (findIndex (eq x) ys)
423
480
424
481
infix 5 \\
425
482
@@ -477,7 +534,3 @@ unzip = uncons' (\_ -> Tuple [] []) \(Tuple a b) ts -> case unzip ts of
477
534
-- | Perform a fold using a monadic step function.
478
535
foldM :: forall m a b . (Monad m ) => (a -> b -> m a ) -> a -> Array b -> m a
479
536
foldM f a = uncons' (\_ -> return a) (\b bs -> f a b >>= \a' -> foldM f a' bs)
480
-
481
- foreign import foldrArray :: forall a b . (a -> b -> b ) -> b -> Array a -> b
482
-
483
- foreign import foldlArray :: forall a b . (b -> a -> b ) -> b -> Array a -> b
0 commit comments