Skip to content

Commit d7771c2

Browse files
committed
Effectful finds
1 parent 5e7ac0d commit d7771c2

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

src/Data/ArrayBuffer/Typed.purs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ foreign import reduceImpl :: forall a b c. EffectFn3 (ArrayView a) (EffectFn3 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
8686
foreign import reduceRight1Impl :: forall a b. EffectFn2 (ArrayView a) (EffectFn3 b b Offset b) b
87-
foreign import findImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable b)
88-
foreign import findIndexImpl :: forall a b. Fn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable Offset)
89-
foreign import indexOfImpl :: forall a b. Fn3 (ArrayView a) b (Nullable Offset) (Nullable Offset)
90-
foreign import lastIndexOfImpl :: forall a b. Fn3 (ArrayView a) b (Nullable Offset) (Nullable Offset)
87+
foreign import findImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable b)
88+
foreign import findIndexImpl :: forall a b. EffectFn2 (ArrayView a) (Fn2 b Offset Boolean) (Nullable Offset)
89+
foreign import indexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) (Nullable Offset)
90+
foreign import lastIndexOfImpl :: forall a b. EffectFn3 (ArrayView a) b (Nullable Offset) (Nullable Offset)
9191

9292

9393
-- | Value-oriented array offset
@@ -281,26 +281,26 @@ foldr1M :: forall a t. TypedArray a t => (t -> t -> Offset -> Effect t) -> Array
281281
foldr1M f a = runEffectFn2 reduceRight1Impl a (mkEffectFn3 (\acc x o -> f x acc o))
282282

283283
-- | Returns the first value satisfying the predicate
284-
find :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Maybe t
284+
find :: forall a t. TypedArray a t => (t -> Boolean) -> ArrayView a -> Effect (Maybe t)
285285
find = findWithIndex' <<< ap1
286286

287-
findWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Maybe t
287+
findWithIndex :: forall a t. TypedArray a t => (Offset -> t -> Boolean) -> ArrayView a -> Effect (Maybe t)
288288
findWithIndex = findWithIndex' <<< flip
289289

290-
findWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Maybe t
291-
findWithIndex' f a = toMaybe (runFn2 findImpl a (mkFn2 f))
290+
findWithIndex' :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect (Maybe t)
291+
findWithIndex' f a = toMaybe <$> runEffectFn2 findImpl a (mkFn2 f)
292292

293293
-- | Returns the first index of the value satisfying the predicate
294-
findIndex :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Maybe Offset
295-
findIndex f a = toMaybe (runFn2 findIndexImpl a (mkFn2 f))
294+
findIndex :: forall a t. TypedArray a t => (t -> Offset -> Boolean) -> ArrayView a -> Effect (Maybe Offset)
295+
findIndex f a = toMaybe <$> runEffectFn2 findIndexImpl a (mkFn2 f)
296296

297297
-- | Returns the first index of the element, if it exists, from the left
298-
indexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Maybe Offset
299-
indexOf x mo a = toMaybe (runFn3 indexOfImpl a x (toNullable mo))
298+
indexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Effect (Maybe Offset)
299+
indexOf x mo a = toMaybe <$> runEffectFn3 indexOfImpl a x (toNullable mo)
300300

301301
-- | Returns the first index of the element, if it exists, from the right
302-
lastIndexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Maybe Offset
303-
lastIndexOf x mo a = toMaybe (runFn3 lastIndexOfImpl a x (toNullable mo))
302+
lastIndexOf :: forall a t. TypedArray a t => t -> Maybe Offset -> ArrayView a -> Effect (Maybe Offset)
303+
lastIndexOf x mo a = toMaybe <$> runEffectFn3 lastIndexOfImpl a x (toNullable mo)
304304

305305
foldl :: forall a b t. TypedArray a t => (b -> t -> b) -> b -> ArrayView a -> b
306306
foldl f = foldlWithIndex' (\a x _ -> f a x)

test/Properties/TypedArray.purs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -305,16 +305,16 @@ anyImpliesFindTests :: Ref Int -> Effect Unit
305305
anyImpliesFindTests count = overAll count anyImpliesFind
306306
where
307307
anyImpliesFind :: forall a b t. TestableArrayF a b D0 t Result
308-
anyImpliesFind (WithOffset _ xs) =
308+
anyImpliesFind (WithOffset _ xs) = do
309309
let pred x = x /= zero
310310
p = TA.any pred xs <?> "All don't satisfy the predicate"
311-
q =
312-
case TA.find pred xs of
311+
idx <- TA.find pred xs
312+
let q = case idx of
313313
Nothing -> Failed "Doesn't have a value satisfying the predicate"
314314
Just z -> if pred z
315315
then Success
316316
else Failed "Found value doesn't satisfy the predicate"
317-
in pure $ p ==> q
317+
pure $ p ==> q
318318

319319

320320
-- | Should work with any arbitrary predicate, but we can't generate them
@@ -324,7 +324,7 @@ findIndexImpliesAtTests count = overAll count findIndexImpliesAt
324324
findIndexImpliesAt :: forall a b t. TestableArrayF a b D0 t Result
325325
findIndexImpliesAt (WithOffset _ xs) = do
326326
let pred x _ = x /= zero
327-
mo = TA.findIndex pred xs
327+
mo <- TA.findIndex pred xs
328328
case mo of
329329
Nothing -> pure Success
330330
Just o -> do
@@ -338,16 +338,18 @@ findIndexImpliesAtTests count = overAll count findIndexImpliesAt
338338
indexOfImpliesAtTests :: Ref Int -> Effect Unit
339339
indexOfImpliesAtTests count = overAll count indexOfImpliesAt
340340
where
341-
indexOfImpliesAt :: forall a b t. TestableArrayF a b D0 t Result
341+
indexOfImpliesAt :: forall a b t. TestableArrayF a b D1 t Result
342342
indexOfImpliesAt (WithOffset _ xs) = do
343343
e <- TA.at xs 0
344344
case e of
345345
Nothing -> pure Success
346-
Just y -> case TA.indexOf y Nothing xs of
347-
Nothing -> pure $ Failed "no index of"
348-
Just o -> do
349-
e' <- TA.at xs o
350-
pure $ e' === Just y
346+
Just y -> do
347+
idx <- TA.indexOf y Nothing xs
348+
case idx of
349+
Nothing -> pure $ Failed "no index of"
350+
Just o -> do
351+
e' <- TA.at xs o
352+
pure $ e' === Just y
351353

352354

353355
lastIndexOfImpliesAtTests :: Ref Int -> Effect Unit
@@ -358,11 +360,13 @@ lastIndexOfImpliesAtTests count = overAll count lastIndexOfImpliesAt
358360
e <- TA.at xs 0
359361
case e of
360362
Nothing -> pure Success
361-
Just y -> case TA.lastIndexOf y Nothing xs of
362-
Nothing -> pure $ Failed "no lastIndex of"
363-
Just o -> do
364-
e' <- TA.at xs o
365-
pure $ e' === Just y
363+
Just y -> do
364+
idx <- TA.lastIndexOf y Nothing xs
365+
case idx of
366+
Nothing -> pure $ Failed "no lastIndex of"
367+
Just o -> do
368+
e' <- TA.at xs o
369+
pure $ e' === Just y
366370

367371

368372
foldrConsIsToArrayTests :: Ref Int -> Effect Unit

0 commit comments

Comments
 (0)