Skip to content

Export specialized any and all from Data.Array and Data.Array.NonEmpty #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Data/Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,30 @@ exports.zipWith = function (f) {
};
};

//------------------------------------------------------------------------------
// Folding ---------------------------------------------------------------------
//------------------------------------------------------------------------------

exports.any = function (p) {
return function (xs) {
var len = xs.length;
for (var i = 0; i < len; i++) {
if (p(xs[i])) return true;
}
return false;
};
};

exports.all = function (p) {
return function (xs) {
var len = xs.length;
for (var i = 0; i < len; i++) {
if (!p(xs[i])) return false;
}
return true;
};
};

//------------------------------------------------------------------------------
// Partial ---------------------------------------------------------------------
//------------------------------------------------------------------------------
Expand Down
29 changes: 27 additions & 2 deletions src/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ module Data.Array
, zip
, unzip

, any
, all

, foldM
, foldRecM

Expand All @@ -134,7 +137,7 @@ import Data.Array.NonEmpty.Internal (NonEmptyArray(..))
import Data.Array.ST as STA
import Data.Array.ST.Iterator as STAI
import Data.Foldable (class Foldable, foldl, foldr, traverse_)
import Data.Foldable (foldl, foldr, foldMap, fold, intercalate, any, all) as Exports
import Data.Foldable (foldl, foldr, foldMap, fold, intercalate) as Exports
import Data.Maybe (Maybe(..), maybe, isJust, fromJust, isNothing)
import Data.Traversable (sequence, traverse)
import Data.Tuple (Tuple(..), fst, snd)
Expand Down Expand Up @@ -1035,7 +1038,7 @@ nubByEq :: forall a. (a -> a -> Boolean) -> Array a -> Array a
nubByEq eq xs = ST.run do
arr <- STA.empty
ST.foreach xs \x -> do
e <- not <<< Exports.any (_ `eq` x) <$> (STA.unsafeFreeze arr)
e <- not <<< any (_ `eq` x) <$> (STA.unsafeFreeze arr)
when e $ void $ STA.push x arr
STA.unsafeFreeze arr

Expand Down Expand Up @@ -1192,6 +1195,28 @@ unzip xs =
snds' <- STA.unsafeFreeze snds
pure $ Tuple fsts' snds'

-- | Returns true if at least one array element satisfies the given predicate,
-- | iterating the array only as necessary and stopping as soon as the predicate
-- | yields true.
-- |
-- | ```purescript
-- | any (_ > 0) [] = False
-- | any (_ > 0) [-1, 0, 1] = True
-- | any (_ > 0) [-1, -2, -3] = False
-- | ```
foreign import any :: forall a. (a -> Boolean) -> Array a -> Boolean

-- | Returns true if all the array elements satisfy the given predicate.
-- | iterating the array only as necessary and stopping as soon as the predicate
-- | yields false.
-- |
-- | ```purescript
-- | all (_ > 0) [] = True
-- | all (_ > 0) [1, 2, 3] = True
-- | all (_ > 0) [-1, -2, -3] = False
-- | ```
foreign import all :: forall a. (a -> Boolean) -> Array a -> Boolean

-- | Perform a fold using a monadic step function.
-- |
-- | ```purescript
Expand Down
9 changes: 9 additions & 0 deletions src/Data/Array/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ module Data.Array.NonEmpty
, zip
, unzip

, any
, all

, foldM
, foldRecM

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

any :: forall a. (a -> Boolean) -> NonEmptyArray a -> Boolean
any p = adaptAny $ A.any p

all :: forall a. (a -> Boolean) -> NonEmptyArray a -> Boolean
all p = adaptAny $ A.all p

foldM :: forall m a b. Monad m => (b -> a -> m b) -> b -> NonEmptyArray a -> m b
foldM f acc = adaptAny $ A.foldM f acc

Expand Down
10 changes: 10 additions & 0 deletions test/Test/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,16 @@ testArray = do
log "unzip should deconstruct a list of tuples into a tuple of lists"
assert $ A.unzip [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"] == Tuple [1, 2, 3] ["a", "b", "c"]

log "any should return true if at least one array element satisfy the given predicate"
assert $ not $ A.any (_ > 0) []
assert $ A.any (_ > 0) [-1, 0, 1]
assert $ not $ A.any (_ > 0) [-1, -2, -3]

log "all should return true if all the array elements satisfy the given predicate"
assert $ A.all (_ > 0) []
assert $ A.all (_ > 0) [1, 2, 3]
assert $ not $ A.all (_ > 0) [-1, -2, -3]

log "foldM should perform a fold using a monadic step function"
assert $ A.foldM (\x y -> Just (x + y)) 0 (A.range 1 10) == Just 55
assert $ A.foldM (\_ _ -> Nothing) 0 (A.range 1 10) == Nothing
Expand Down
8 changes: 8 additions & 0 deletions test/Test/Data/Array/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ testNonEmptyArray = do
log "unzip should deconstruct a list of tuples into a tuple of lists"
assert $ NEA.unzip (fromArray [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"]) == Tuple (fromArray [1, 2, 3]) (fromArray ["a", "b", "c"])

log "any should return true if at least one array element satisfy the given predicate"
assert $ NEA.any (_ > 0) $ fromArray [-1, 0, 1]
assert $ not $ NEA.any (_ > 0) $ fromArray [-1, -2, -3]

log "all should return true if all the array elements satisfy the given predicate"
assert $ NEA.all (_ > 0) $ fromArray [1, 2, 3]
assert $ not $ NEA.all (_ > 0) $ fromArray [-1, -2, -3]

log "fromFoldable"
for_ (fromArray [[], [1], [1,2], [1,2,3,4,5]]) \xs -> do
assert $ NEA.fromFoldable xs == NEA.fromArray xs
Expand Down