Skip to content

Commit fa53dca

Browse files
committed
filter
1 parent f2be9a4 commit fa53dca

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

src/Data/ArrayBuffer/Typed.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ exports.mapImpl = function mapImpl (a,f) {
7171
return a.map(f);
7272
};
7373

74+
exports.forEachImpl = function forEachImpl (a,f) {
75+
a.forEach(f);
76+
};
77+
78+
exports.filterImpl = function filterImpl (a,p) {
79+
return a.filter(p);
80+
};
81+
7482

7583
exports.copyWithinImpl = function copyWithinImpl (a,t,s) {
7684
a.copyWithin(t,s);

src/Data/ArrayBuffer/Typed.purs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3232
import Effect (Effect)
3333
import Effect.Uncurried
3434
( EffectFn4, EffectFn3, EffectFn2, EffectFn1
35-
, runEffectFn4, runEffectFn3, runEffectFn2, runEffectFn1)
35+
, runEffectFn4, runEffectFn3, runEffectFn2, runEffectFn1
36+
, mkEffectFn1)
3637
import Effect.Unsafe (unsafePerformEffect)
3738
import Data.Nullable (Nullable, toNullable)
3839
import Data.ArrayBuffer.Types
@@ -95,7 +96,9 @@ foreign import fillImpl :: forall a b. EffectFn2 (ArrayView a) b Unit
9596
foreign import fillImpl2 :: forall a b. EffectFn3 (ArrayView a) b ByteOffset Unit
9697
foreign 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

131140
instance 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

Comments
 (0)