@@ -12,7 +12,7 @@ module Data.ArrayBuffer.Typed
1212 , length
1313 , class ValuesPer
1414 , whole , remainder , part , empty , fromArray , all , any , fill , fillRemainder , fillPart , set , set'
15- , map'
15+ , map' , traverse' , traverse_' , filter
1616 , copyWithin , copyWithinPart
1717 , reverse
1818 , setTyped , setTyped'
@@ -32,7 +32,8 @@ import Prelude
3232import Effect (Effect )
3333import Effect.Uncurried
3434 ( EffectFn4 , EffectFn3 , EffectFn2 , EffectFn1
35- , runEffectFn4 , runEffectFn3 , runEffectFn2 , runEffectFn1 )
35+ , runEffectFn4 , runEffectFn3 , runEffectFn2 , runEffectFn1
36+ , mkEffectFn1 )
3637import Effect.Unsafe (unsafePerformEffect )
3738import Data.Nullable (Nullable , toNullable )
3839import Data.ArrayBuffer.Types
@@ -95,7 +96,9 @@ foreign import fillImpl :: forall a b. EffectFn2 (ArrayView a) b Unit
9596foreign import fillImpl2 :: forall a b . EffectFn3 (ArrayView a ) b ByteOffset Unit
9697foreign import fillImpl3 :: forall a b . EffectFn4 (ArrayView a ) b ByteOffset ByteOffset Unit
9798
98- foreign import mapImpl :: forall a b . Fn2 (ArrayView a ) (b -> b ) (ArrayView a )
99+ foreign import mapImpl :: forall a b . EffectFn2 (ArrayView a ) (EffectFn1 b b ) (ArrayView a )
100+ foreign import forEachImpl :: forall a b . EffectFn2 (ArrayView a ) (EffectFn1 b Unit ) Unit
101+ foreign import filterImpl :: forall a b . Fn2 (ArrayView a ) (b -> Boolean ) (ArrayView a )
99102
100103
101104-- TODO use purescript-quotient
@@ -127,6 +130,12 @@ class ValuesPer (a :: ArrayViewType) (t :: Type) | a -> t where
127130 set' :: ArrayView a -> ByteOffset -> Array t -> Effect Unit
128131 -- | Maps a new value over the typed array, creating a new buffer and typed array as well.
129132 map' :: (t -> t ) -> ArrayView a -> ArrayView a
133+ -- | Traverses over each value, returning a new one
134+ traverse' :: (t -> Effect t ) -> ArrayView a -> Effect (ArrayView a )
135+ -- | Traverses over each value
136+ traverse_' :: (t -> Effect Unit ) -> ArrayView a -> Effect Unit
137+ -- | Returns a new typed array with all values that pass the predicate
138+ filter :: (t -> Boolean ) -> ArrayView a -> ArrayView a
130139
131140instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where
132141 whole a = unsafePerformEffect (runEffectFn1 newUint8ClampedArray a)
@@ -141,7 +150,10 @@ instance valuesPerUint8Clamped :: ValuesPer Uint8Clamped Int where
141150 fillPart = runEffectFn4 fillImpl3
142151 set a x = runEffectFn3 setImpl a (toNullable Nothing ) x
143152 set' a o x = runEffectFn3 setImpl a (toNullable (Just o)) x
144- map' f a = runFn2 mapImpl a f
153+ map' f a = unsafePerformEffect (runEffectFn2 mapImpl a (mkEffectFn1 (pure <<< f)))
154+ traverse' f a = runEffectFn2 mapImpl a (mkEffectFn1 f)
155+ traverse_' f a = runEffectFn2 forEachImpl a (mkEffectFn1 f)
156+ filter p a = runFn2 filterImpl a p
145157-- instance valuesPerUint32 :: ValuesPer Uint32 Number
146158-- instance valuesPerUint16 :: ValuesPer Uint16 Int
147159-- instance valuesPerUint8 :: ValuesPer Uint8 Int
0 commit comments