Skip to content

Commit aa126bc

Browse files
committed
Made some filters/reducers effectful
1 parent e1aead1 commit aa126bc

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

src/Data/ArrayBuffer/Typed.purs

+18-18
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ foreign import newFloat64Array :: forall a. EffectFn3 a (Nullable ByteOffset) (N
7171

7272
-- ----
7373

74-
foreign import everyImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean
75-
foreign import someImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean
74+
foreign import everyImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean
75+
foreign import someImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) Boolean
7676

7777
foreign import fillImpl :: forall a b. EffectFn4 (ArrayView a) b Offset Offset Unit
7878

7979
foreign import mapImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset b) (ArrayView a)
8080
foreign import forEachImpl :: forall a b. EffectFn2 (ArrayView a) (EffectFn2 b Offset Unit) Unit
81-
foreign import filterImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) (ArrayView a)
82-
foreign import includesImpl :: forall a b. Fn3 (ArrayView a) b (Nullable Offset) Boolean
81+
foreign import filterImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (ArrayView a)
82+
foreign import includesImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) Boolean
8383
foreign import reduceImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c
8484
foreign import reduce1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b
8585
foreign import reduceRightImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 c b Offset c) c c
@@ -227,38 +227,38 @@ traverseWithIndex_' :: forall a t. TypedArray a t => (t -> Offset -> Effect Unit
227227
traverseWithIndex_' f a = runEffectFn2 forEachImpl a (mkEffectFn2 f)
228228

229229
-- | Test a predicate to pass on all values
230-
all :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Boolean
230+
all :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect Boolean
231231
all = every <<< ap1
232232

233-
allWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Boolean
233+
allWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Effect Boolean
234234
allWithIndex = every <<< flip
235235

236-
every :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Boolean
237-
every p a = runFn2 everyImpl a (mkFn2 p)
236+
every :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect Boolean
237+
every p a = runEffectFn2 everyImpl a (mkFn2 p)
238238

239239
-- | Test a predicate to pass on any value
240-
any :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Boolean
240+
any :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect Boolean
241241
any = some <<< ap1
242242

243-
anyWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Boolean
243+
anyWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Effect Boolean
244244
anyWithIndex = some <<< flip
245245

246-
some :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Boolean
247-
some p a = runFn2 someImpl a (mkFn2 p)
246+
some :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect Boolean
247+
some p a = runEffectFn2 someImpl a (mkFn2 p)
248248

249249
-- | Returns a new typed array with all values that pass the predicate
250-
filter :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> ArrayView a
250+
filter :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect (ArrayView a)
251251
filter = filterWithIndex' <<< ap1
252252

253-
filterWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> ArrayView a
253+
filterWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Effect (ArrayView a)
254254
filterWithIndex = filterWithIndex' <<< flip
255255

256-
filterWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> ArrayView a
257-
filterWithIndex' p a = runFn2 filterImpl a (mkFn2 p)
256+
filterWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect (ArrayView a)
257+
filterWithIndex' p a = runEffectFn2 filterImpl a (mkFn2 p)
258258

259259
-- | Tests if a value is an element of the typed array
260-
elem :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Boolean
261-
elem x mo a = runFn3 includesImpl a x (toNullable mo)
260+
elem :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Effect Boolean
261+
elem x mo a = runEffectFn3 includesImpl a x (toNullable mo)
262262

263263
-- | Fetch element at index.
264264
unsafeAt :: forall a t. TypedArray a t => Partial => ArrayView a -> Offset -> Effect t

test/Properties/TypedArray.purs

+24-17
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ allAreFilledTests count = overAll count allAreFilled
213213
let x = fromMaybe zero e
214214
l = TA.length xs
215215
TA.fill x 0 l xs
216-
let b = TA.all (_ == x) xs
216+
b <- TA.all (_ == x) xs
217217
pure (b <?> "All aren't the filled value")
218218

219219

@@ -237,23 +237,25 @@ allImpliesAnyTests :: Ref Int -> Effect Unit
237237
allImpliesAnyTests count = overAll count allImpliesAny
238238
where
239239
allImpliesAny :: forall a b t. TestableArrayF a b D0 t Result
240-
allImpliesAny (WithOffset _ xs) =
240+
allImpliesAny (WithOffset _ xs) = do
241241
let pred x = x /= zero
242-
all' = TA.all pred xs <?> "All don't satisfy the predicate"
243-
any' = TA.any pred xs <?> "None satisfy the predicate"
244-
in pure $ (TA.length xs === zero) |=| all' ==> any'
242+
all'' <- TA.all pred xs
243+
let all' = all'' <?> "All don't satisfy the predicate"
244+
any'' <- TA.any pred xs
245+
let any' = any'' <?> "None satisfy the predicate"
246+
pure $ (TA.length xs === zero) |=| all' ==> any'
245247

246248

247249
-- | Should work with any arbitrary predicate, but we can't generate them
248250
filterImpliesAllTests :: Ref Int -> Effect Unit
249251
filterImpliesAllTests count = overAll count filterImpliesAll
250252
where
251253
filterImpliesAll :: forall a b t. TestableArrayF a b D0 t Result
252-
filterImpliesAll (WithOffset _ xs) =
254+
filterImpliesAll (WithOffset _ xs) = do
253255
let pred x = x /= zero
254-
ys = TA.filter pred xs
255-
all' = TA.all pred ys
256-
in pure $ all' <?> "Filter doesn't imply all"
256+
ys <- TA.filter pred xs
257+
all' <- TA.all pred ys
258+
pure $ all' <?> "Filter doesn't imply all"
257259

258260

259261
-- | Should work with any arbitrary predicate, but we can't generate them
@@ -263,8 +265,8 @@ filterIsTotalTests count = overAll count filterIsTotal
263265
filterIsTotal :: forall a b t. TestableArrayF a b D0 t Result
264266
filterIsTotal (WithOffset _ xs) = do
265267
let pred x = x /= zero
266-
ys = TA.filter pred xs
267-
zs = TA.filter (not pred) ys
268+
ys <- TA.filter pred xs
269+
zs <- TA.filter (not pred) ys
268270
azs <- TA.toArray zs
269271
pure $ azs === []
270272

@@ -276,8 +278,8 @@ filterIsIdempotentTests count = overAll count filterIsIdempotent
276278
filterIsIdempotent :: forall a b t. TestableArrayF a b D0 t Result
277279
filterIsIdempotent (WithOffset _ xs) = do
278280
let pred x = x /= zero
279-
ys = TA.filter pred xs
280-
zs = TA.filter pred ys
281+
ys <- TA.filter pred xs
282+
zs <- TA.filter pred ys
281283
azs <- TA.toArray zs
282284
ays <- TA.toArray ys
283285
pure $ azs === ays
@@ -295,9 +297,13 @@ withOffsetElemTests :: Ref Int -> Effect Unit
295297
withOffsetElemTests count = overAll1 count withOffsetElem
296298
where
297299
withOffsetElem :: forall a b t. TestableArrayF a b D5 t Result
298-
withOffsetElem (WithOffset os xs) = pure $
299-
Array.all (\o -> TA.elem (unsafePartial $ unsafePerformEffect $ TA.unsafeAt xs o) Nothing xs) os
300-
<?> "All doesn't have an elem of itself"
300+
withOffsetElem (WithOffset os xs) = do
301+
let valid :: TA.Offset -> Boolean
302+
valid o = unsafePerformEffect do
303+
e <- unsafePartial $ TA.unsafeAt xs o
304+
b <- TA.elem e Nothing xs
305+
pure b
306+
pure $ Array.all valid os <?> "All doesn't have an elem of itself"
301307

302308

303309
-- | Should work with any arbitrary predicate, but we can't generate them
@@ -307,7 +313,8 @@ anyImpliesFindTests count = overAll count anyImpliesFind
307313
anyImpliesFind :: forall a b t. TestableArrayF a b D0 t Result
308314
anyImpliesFind (WithOffset _ xs) = do
309315
let pred x = x /= zero
310-
p = TA.any pred xs <?> "All don't satisfy the predicate"
316+
a <- TA.any pred xs
317+
let p = a <?> "All don't satisfy the predicate"
311318
idx <- TA.find pred xs
312319
let q = case idx of
313320
Nothing -> Failed "Doesn't have a value satisfying the predicate"

0 commit comments

Comments
 (0)