Skip to content

Commit a22f493

Browse files
committed
Export specialized any and all from Data.Array and Data.Array.NonEmpty
1 parent c5ae83f commit a22f493

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

src/Data/Array.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,30 @@ exports.zipWith = function (f) {
307307
};
308308
};
309309

310+
//------------------------------------------------------------------------------
311+
// Folding ---------------------------------------------------------------------
312+
//------------------------------------------------------------------------------
313+
314+
exports.any = function (p) {
315+
return function (xs) {
316+
var len = xs.length;
317+
for (var i = 0; i < len; i++) {
318+
if (p(xs[i])) return true;
319+
}
320+
return false;
321+
};
322+
};
323+
324+
exports.all = function (p) {
325+
return function (xs) {
326+
var len = xs.length;
327+
for (var i = 0; i < len; i++) {
328+
if (!p(xs[i])) return false;
329+
}
330+
return true;
331+
};
332+
};
333+
310334
//------------------------------------------------------------------------------
311335
// Partial ---------------------------------------------------------------------
312336
//------------------------------------------------------------------------------

src/Data/Array.purs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ module Data.Array
115115
, zip
116116
, unzip
117117

118+
, any
119+
, all
120+
118121
, foldM
119122
, foldRecM
120123

@@ -134,7 +137,7 @@ import Data.Array.NonEmpty.Internal (NonEmptyArray(..))
134137
import Data.Array.ST as STA
135138
import Data.Array.ST.Iterator as STAI
136139
import Data.Foldable (class Foldable, foldl, foldr, traverse_)
137-
import Data.Foldable (foldl, foldr, foldMap, fold, intercalate, any, all) as Exports
140+
import Data.Foldable (foldl, foldr, foldMap, fold, intercalate) as Exports
138141
import Data.Maybe (Maybe(..), maybe, isJust, fromJust, isNothing)
139142
import Data.Traversable (sequence, traverse)
140143
import Data.Tuple (Tuple(..), fst, snd)
@@ -1035,7 +1038,7 @@ nubByEq :: forall a. (a -> a -> Boolean) -> Array a -> Array a
10351038
nubByEq eq xs = ST.run do
10361039
arr <- STA.empty
10371040
ST.foreach xs \x -> do
1038-
e <- not <<< Exports.any (_ `eq` x) <$> (STA.unsafeFreeze arr)
1041+
e <- not <<< any (_ `eq` x) <$> (STA.unsafeFreeze arr)
10391042
when e $ void $ STA.push x arr
10401043
STA.unsafeFreeze arr
10411044

@@ -1192,6 +1195,12 @@ unzip xs =
11921195
snds' <- STA.unsafeFreeze snds
11931196
pure $ Tuple fsts' snds'
11941197

1198+
-- | Returns true if at least one array element satisfy the given predicate.
1199+
foreign import any :: forall a. (a -> Boolean) -> Array a -> Boolean
1200+
1201+
-- | Returns true if all the array elements satisfy the given predicate.
1202+
foreign import all :: forall a. (a -> Boolean) -> Array a -> Boolean
1203+
11951204
-- | Perform a fold using a monadic step function.
11961205
-- |
11971206
-- | ```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
@@ -415,6 +415,16 @@ testArray = do
415415
log "unzip should deconstruct a list of tuples into a tuple of lists"
416416
assert $ A.unzip [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"] == Tuple [1, 2, 3] ["a", "b", "c"]
417417

418+
log "any should return true if at least one array element satisfy the given predicate"
419+
assert $ not $ A.any (_ > 2) []
420+
assert $ A.any (_ > 2) [1, 2, 3]
421+
assert $ not $ A.any (_ > 3) [1, 2, 3]
422+
423+
log "all should return true if all the array elements satisfy the given predicate"
424+
assert $ A.all (_ > 0) []
425+
assert $ A.all (_ > 0) [1, 2, 3]
426+
assert $ not $ A.all (_ > 1) [1, 2, 3]
427+
418428
log "foldM should perform a fold using a monadic step function"
419429
assert $ A.foldM (\x y -> Just (x + y)) 0 (A.range 1 10) == Just 55
420430
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
@@ -285,6 +285,14 @@ testNonEmptyArray = do
285285
log "unzip should deconstruct a list of tuples into a tuple of lists"
286286
assert $ NEA.unzip (fromArray [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"]) == Tuple (fromArray [1, 2, 3]) (fromArray ["a", "b", "c"])
287287

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

0 commit comments

Comments
 (0)