Skip to content

Commit fab2c23

Browse files
authored
Merge pull request #193 from kl0tl/specialized-any-and-all
Export specialized any and all from Data.Array and Data.Array.NonEmpty
2 parents e13c01f + 7bcd572 commit fab2c23

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
lines changed

src/Data/Array.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,30 @@ exports.zipWith = function (f) {
359359
};
360360
};
361361

362+
//------------------------------------------------------------------------------
363+
// Folding ---------------------------------------------------------------------
364+
//------------------------------------------------------------------------------
365+
366+
exports.any = function (p) {
367+
return function (xs) {
368+
var len = xs.length;
369+
for (var i = 0; i < len; i++) {
370+
if (p(xs[i])) return true;
371+
}
372+
return false;
373+
};
374+
};
375+
376+
exports.all = function (p) {
377+
return function (xs) {
378+
var len = xs.length;
379+
for (var i = 0; i < len; i++) {
380+
if (!p(xs[i])) return false;
381+
}
382+
return true;
383+
};
384+
};
385+
362386
//------------------------------------------------------------------------------
363387
// Partial ---------------------------------------------------------------------
364388
//------------------------------------------------------------------------------

src/Data/Array.purs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ module Data.Array
117117
, zip
118118
, unzip
119119

120+
, any
121+
, all
122+
120123
, foldM
121124
, foldRecM
122125

@@ -136,7 +139,7 @@ import Data.Array.NonEmpty.Internal (NonEmptyArray(..))
136139
import Data.Array.ST as STA
137140
import Data.Array.ST.Iterator as STAI
138141
import Data.Foldable (class Foldable, foldl, foldr, traverse_)
139-
import Data.Foldable (foldl, foldr, foldMap, fold, intercalate, any, all) as Exports
142+
import Data.Foldable (foldl, foldr, foldMap, fold, intercalate) as Exports
140143
import Data.Maybe (Maybe(..), maybe, isJust, fromJust, isNothing)
141144
import Data.Traversable (sequence, traverse)
142145
import Data.Tuple (Tuple(..), fst, snd)
@@ -1051,7 +1054,7 @@ nubByEq :: forall a. (a -> a -> Boolean) -> Array a -> Array a
10511054
nubByEq eq xs = ST.run do
10521055
arr <- STA.empty
10531056
ST.foreach xs \x -> do
1054-
e <- not <<< Exports.any (_ `eq` x) <$> (STA.unsafeFreeze arr)
1057+
e <- not <<< any (_ `eq` x) <$> (STA.unsafeFreeze arr)
10551058
when e $ void $ STA.push x arr
10561059
STA.unsafeFreeze arr
10571060

@@ -1208,6 +1211,28 @@ unzip xs =
12081211
snds' <- STA.unsafeFreeze snds
12091212
pure $ Tuple fsts' snds'
12101213

1214+
-- | Returns true if at least one array element satisfies the given predicate,
1215+
-- | iterating the array only as necessary and stopping as soon as the predicate
1216+
-- | yields true.
1217+
-- |
1218+
-- | ```purescript
1219+
-- | any (_ > 0) [] = False
1220+
-- | any (_ > 0) [-1, 0, 1] = True
1221+
-- | any (_ > 0) [-1, -2, -3] = False
1222+
-- | ```
1223+
foreign import any :: forall a. (a -> Boolean) -> Array a -> Boolean
1224+
1225+
-- | Returns true if all the array elements satisfy the given predicate.
1226+
-- | iterating the array only as necessary and stopping as soon as the predicate
1227+
-- | yields false.
1228+
-- |
1229+
-- | ```purescript
1230+
-- | all (_ > 0) [] = True
1231+
-- | all (_ > 0) [1, 2, 3] = True
1232+
-- | all (_ > 0) [-1, -2, -3] = False
1233+
-- | ```
1234+
foreign import all :: forall a. (a -> Boolean) -> Array a -> Boolean
1235+
12111236
-- | Perform a fold using a monadic step function.
12121237
-- |
12131238
-- | ```purescript

src/Data/Array/NonEmpty.purs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ module Data.Array.NonEmpty
8989
, zip
9090
, unzip
9191

92+
, any
93+
, all
94+
9295
, foldM
9396
, foldRecM
9497

@@ -438,6 +441,12 @@ zip xs ys = unsafeFromArray $ toArray xs `A.zip` toArray ys
438441
unzip :: forall a b. NonEmptyArray (Tuple a b) -> Tuple (NonEmptyArray a) (NonEmptyArray b)
439442
unzip = bimap unsafeFromArray unsafeFromArray <<< A.unzip <<< toArray
440443

444+
any :: forall a. (a -> Boolean) -> NonEmptyArray a -> Boolean
445+
any p = adaptAny $ A.any p
446+
447+
all :: forall a. (a -> Boolean) -> NonEmptyArray a -> Boolean
448+
all p = adaptAny $ A.all p
449+
441450
foldM :: forall m a b. Monad m => (b -> a -> m b) -> b -> NonEmptyArray a -> m b
442451
foldM f acc = adaptAny $ A.foldM f acc
443452

test/Test/Data/Array.purs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,16 @@ testArray = do
423423
log "unzip should deconstruct a list of tuples into a tuple of lists"
424424
assert $ A.unzip [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"] == Tuple [1, 2, 3] ["a", "b", "c"]
425425

426+
log "any should return true if at least one array element satisfy the given predicate"
427+
assert $ not $ A.any (_ > 0) []
428+
assert $ A.any (_ > 0) [-1, 0, 1]
429+
assert $ not $ A.any (_ > 0) [-1, -2, -3]
430+
431+
log "all should return true if all the array elements satisfy the given predicate"
432+
assert $ A.all (_ > 0) []
433+
assert $ A.all (_ > 0) [1, 2, 3]
434+
assert $ not $ A.all (_ > 0) [-1, -2, -3]
435+
426436
log "foldM should perform a fold using a monadic step function"
427437
assert $ A.foldM (\x y -> Just (x + y)) 0 (A.range 1 10) == Just 55
428438
assert $ A.foldM (\_ _ -> Nothing) 0 (A.range 1 10) == Nothing

test/Test/Data/Array/NonEmpty.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,14 @@ testNonEmptyArray = do
287287
log "unzip should deconstruct a list of tuples into a tuple of lists"
288288
assert $ NEA.unzip (fromArray [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"]) == Tuple (fromArray [1, 2, 3]) (fromArray ["a", "b", "c"])
289289

290+
log "any should return true if at least one array element satisfy the given predicate"
291+
assert $ NEA.any (_ > 0) $ fromArray [-1, 0, 1]
292+
assert $ not $ NEA.any (_ > 0) $ fromArray [-1, -2, -3]
293+
294+
log "all should return true if all the array elements satisfy the given predicate"
295+
assert $ NEA.all (_ > 0) $ fromArray [1, 2, 3]
296+
assert $ not $ NEA.all (_ > 0) $ fromArray [-1, -2, -3]
297+
290298
log "fromFoldable"
291299
for_ (fromArray [[], [1], [1,2], [1,2,3,4,5]]) \xs -> do
292300
assert $ NEA.fromFoldable xs == NEA.fromArray xs

0 commit comments

Comments
 (0)