@@ -9,8 +9,11 @@ module Data.ArrayBuffer.Typed
99 , create , whole , remainder , part , empty , fromArray
1010 , fill , set , setTyped , copyWithin
1111 , map , traverse , traverse_ , filter
12+ , mapWithIndex , traverseWithIndex , traverseWithIndex_ , filterWithIndex
1213 , sort , reverse
13- , elem , all , any
14+ , elem
15+ , all , any
16+ , allWithIndex , anyWithIndex
1417 , unsafeAt , hasIndex , at , (!)
1518 , foldlM , foldl1M , foldl , foldl1 , foldrM , foldr1M , foldr , foldr1
1619 , find , findIndex , indexOf , lastIndexOf
@@ -191,29 +194,75 @@ fill a x mz = case mz of
191194set :: forall a t . TypedArray a t => ArrayView a -> Maybe Offset -> Array t -> Effect Boolean
192195set = setInternal A .length
193196
194- -- | Maps a new value over the typed array, creating a new buffer and typed array as well.
195- map :: forall a t . TypedArray a t => (t -> Offset -> t ) -> ArrayView a -> ArrayView a
196- map f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o))))
197+ ap1 :: forall a b c . (a -> c ) -> (a -> b -> c )
198+ ap1 f = \x _ -> f x
199+
200+
201+ -- | Maps a new value over the typed array, creating a new buffer and
202+ -- | typed array as well.
203+ map :: forall a t . TypedArray a t => (t -> t ) -> ArrayView a -> ArrayView a
204+ map = mapWithIndex' <<< ap1
205+
206+ -- | Apply a function to each element in an array, supplying a
207+ -- | generated zero-based index integer along with the element,
208+ -- | creating a typed array with the new elements
209+ mapWithIndex :: forall a t . TypedArray a t => (Offset -> t -> t ) -> ArrayView a -> ArrayView a
210+ mapWithIndex = mapWithIndex' <<< flip
211+
212+ mapWithIndex' :: forall a t . TypedArray a t => (t -> Offset -> t ) -> ArrayView a -> ArrayView a
213+ mapWithIndex' f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn2 (\x o -> pure (f x o))))
214+
215+ -- | Traverses over each value, returning a new one
216+ traverse :: forall a t . TypedArray a t => (t -> Effect t ) -> ArrayView a -> Effect (ArrayView a )
217+ traverse = traverseWithIndex' <<< ap1
197218
198219-- | Traverses over each value, returning a new one
199- traverse :: forall a t . TypedArray a t => (t -> Offset -> Effect t ) -> ArrayView a -> Effect (ArrayView a )
200- traverse f a = runEffectFn2 mapImpl a (mkEffectFn2 f)
220+ traverseWithIndex :: forall a t . TypedArray a t => (Offset -> t -> Effect t ) -> ArrayView a -> Effect (ArrayView a )
221+ traverseWithIndex = traverseWithIndex' <<< flip
222+
223+ traverseWithIndex' :: forall a t . TypedArray a t => (t -> Offset -> Effect t ) -> ArrayView a -> Effect (ArrayView a )
224+ traverseWithIndex' f a = runEffectFn2 mapImpl a (mkEffectFn2 f)
225+
226+ -- | Traverses over each value
227+ traverse_ :: forall a t . TypedArray a t => (t -> Effect Unit ) -> ArrayView a -> Effect Unit
228+ traverse_ = traverseWithIndex_' <<< ap1
201229
202230-- | Traverses over each value
203- traverse_ :: forall a t . TypedArray a t => (t -> Offset -> Effect Unit ) -> ArrayView a -> Effect Unit
204- traverse_ f a = runEffectFn2 forEachImpl a (mkEffectFn2 f)
231+ traverseWithIndex_ :: forall a t . TypedArray a t => (Offset -> t -> Effect Unit ) -> ArrayView a -> Effect Unit
232+ traverseWithIndex_ = traverseWithIndex_' <<< flip
233+
234+ traverseWithIndex_' :: forall a t . TypedArray a t => (t -> Offset -> Effect Unit ) -> ArrayView a -> Effect Unit
235+ traverseWithIndex_' f a = runEffectFn2 forEachImpl a (mkEffectFn2 f)
205236
206237-- | Test a predicate to pass on all values
207- all :: forall a t . TypedArray a t => (t -> Offset -> Boolean ) -> ArrayView a -> Boolean
208- all p a = runFn2 everyImpl a (mkFn2 p)
238+ all :: forall a t . TypedArray a t => (t -> Boolean ) -> ArrayView a -> Boolean
239+ all = every <<< ap1
240+
241+ allWithIndex :: forall a t . TypedArray a t => (Offset -> t -> Boolean ) -> ArrayView a -> Boolean
242+ allWithIndex = every <<< flip
243+
244+ every :: forall a t . TypedArray a t => (t -> Offset -> Boolean ) -> ArrayView a -> Boolean
245+ every p a = runFn2 everyImpl a (mkFn2 p)
209246
210247-- | Test a predicate to pass on any value
211- any :: forall a t . TypedArray a t => (t -> Offset -> Boolean ) -> ArrayView a -> Boolean
212- any p a = runFn2 someImpl a (mkFn2 p)
248+ any :: forall a t . TypedArray a t => (t -> Boolean ) -> ArrayView a -> Boolean
249+ any = some <<< ap1
250+
251+ anyWithIndex :: forall a t . TypedArray a t => (Offset -> t -> Boolean ) -> ArrayView a -> Boolean
252+ anyWithIndex = some <<< flip
253+
254+ some :: forall a t . TypedArray a t => (t -> Offset -> Boolean ) -> ArrayView a -> Boolean
255+ some p a = runFn2 someImpl a (mkFn2 p)
213256
214257-- | Returns a new typed array with all values that pass the predicate
215- filter :: forall a t . TypedArray a t => (t -> Offset -> Boolean ) -> ArrayView a -> ArrayView a
216- filter p a = runFn2 filterImpl a (mkFn2 p)
258+ filter :: forall a t . TypedArray a t => (t -> Boolean ) -> ArrayView a -> ArrayView a
259+ filter = filterWithIndex' <<< ap1
260+
261+ filterWithIndex :: forall a t . TypedArray a t => (Offset -> t -> Boolean ) -> ArrayView a -> ArrayView a
262+ filterWithIndex = filterWithIndex' <<< flip
263+
264+ filterWithIndex' :: forall a t . TypedArray a t => (t -> Offset -> Boolean ) -> ArrayView a -> ArrayView a
265+ filterWithIndex' p a = runFn2 filterImpl a (mkFn2 p)
217266
218267-- | Tests if a value is an element of the typed array
219268elem :: forall a t . TypedArray a t => t -> Maybe Offset -> ArrayView a -> Boolean
@@ -240,8 +289,14 @@ foldr1M :: forall a t. TypedArray a t => (t -> t -> Offset -> Effect t) -> Array
240289foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o))
241290
242291-- | Returns the first value satisfying the predicate
243- find :: forall a t . TypedArray a t => (t -> Offset -> Boolean ) -> ArrayView a -> Maybe t
244- find f a = toMaybe (runFn2 findImpl a (mkFn2 f))
292+ find :: forall a t . TypedArray a t => (t -> Boolean ) -> ArrayView a -> Maybe t
293+ find = findWithIndex' <<< ap1
294+
295+ findWithIndex :: forall a t . TypedArray a t => (Offset -> t -> Boolean ) -> ArrayView a -> Maybe t
296+ findWithIndex = findWithIndex' <<< flip
297+
298+ findWithIndex' :: forall a t . TypedArray a t => (t -> Offset -> Boolean ) -> ArrayView a -> Maybe t
299+ findWithIndex' f a = toMaybe (runFn2 findImpl a (mkFn2 f))
245300
246301-- | Returns the first index of the value satisfying the predicate
247302findIndex :: forall a t . TypedArray a t => (t -> Offset -> Boolean ) -> ArrayView a -> Maybe Offset
@@ -255,18 +310,35 @@ indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo))
255310lastIndexOf :: forall a t . TypedArray a t => t -> Maybe Offset -> ArrayView a -> Maybe Offset
256311lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo))
257312
258- foldl :: forall a b t . TypedArray a t => (b -> t -> Offset -> b ) -> b -> ArrayView a -> b
259- foldl f i a = unsafePerformEffect (foldlM (\acc x o -> pure (f acc x o)) i a)
313+ foldl :: forall a b t . TypedArray a t => (b -> t -> b ) -> b -> ArrayView a -> b
314+ foldl f = foldlWithIndex' (\a x _ -> f a x)
315+
316+ foldlWithIndex :: forall a b t . TypedArray a t => (Offset -> b -> t -> b ) -> b -> ArrayView a -> b
317+ foldlWithIndex f = foldlWithIndex' (\a x o -> f o a x)
318+
319+ foldlWithIndex' :: forall a b t . TypedArray a t => (b -> t -> Offset -> b ) -> b -> ArrayView a -> b
320+ foldlWithIndex' f i = unsafePerformEffect <<< foldlM (\a x o -> pure (f a x o)) i
321+
322+ foldr :: forall a b t . TypedArray a t => (t -> b -> b ) -> b -> ArrayView a -> b
323+ foldr f = foldrWithIndex' (\a x _ -> f a x)
324+
325+ foldrWithIndex :: forall a b t . TypedArray a t => (Offset -> t -> b -> b ) -> b -> ArrayView a -> b
326+ foldrWithIndex f = foldrWithIndex' (\a x o -> f o a x)
327+
328+ foldrWithIndex' :: forall a b t . TypedArray a t => (t -> b -> Offset -> b ) -> b -> ArrayView a -> b
329+ foldrWithIndex' f i = unsafePerformEffect <<< foldrM (\x a o -> pure (f x a o)) i
260330
261- foldr :: forall a b t . TypedArray a t => (t -> b -> Offset -> b ) -> b -> ArrayView a -> b
262- foldr f i a = unsafePerformEffect (foldrM (\x acc o -> pure (f x acc o)) i a )
331+ foldl1 :: forall a t . TypedArray a t => (t -> t -> t ) -> ArrayView a -> t
332+ foldl1 f = foldl1WithIndex (\_ a x -> f a x )
263333
264- foldl1 :: forall a t . TypedArray a t => (t -> t -> Offset -> t ) -> ArrayView a -> t
265- foldl1 f a = unsafePerformEffect ( foldl1M (\acc x o -> pure (f acc x o)) a )
334+ foldl1WithIndex :: forall a t . TypedArray a t => (Offset -> t -> t -> t ) -> ArrayView a -> t
335+ foldl1WithIndex f = unsafePerformEffect <<< foldl1M (\acc x o -> pure (f o acc x) )
266336
267- foldr1 :: forall a t . TypedArray a t => (t -> t -> Offset -> t ) -> ArrayView a -> t
268- foldr1 f a = unsafePerformEffect (foldr1M (\x acc o -> pure (f x acc o)) a )
337+ foldr1 :: forall a t . TypedArray a t => (t -> t -> t ) -> ArrayView a -> t
338+ foldr1 f = foldr1WithIndex (\_ a x -> f a x )
269339
340+ foldr1WithIndex :: forall a t . TypedArray a t => (Offset -> t -> t -> t ) -> ArrayView a -> t
341+ foldr1WithIndex f = unsafePerformEffect <<< foldr1M (\x a o -> pure (f o x a))
270342
271343foreign import copyWithinImpl :: forall a . EffectFn4 (ArrayView a ) Offset Offset (Nullable Offset ) Unit
272344
0 commit comments