Skip to content

Commit

Permalink
Add Set.Extra.any and Set.Extra.all (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
miniBill authored Mar 21, 2024
1 parent 59a6247 commit 278a187
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs.json

Large diffs are not rendered by default.

42 changes: 40 additions & 2 deletions src/Set/Extra.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Set.Extra exposing
( toggle
, isSupersetOf, isSubsetOf, areDisjoint
, isSupersetOf, isSubsetOf, areDisjoint, any, all
, symmetricDifference
, concatMap, filterMap
)
Expand All @@ -15,7 +15,7 @@ module Set.Extra exposing
# Predicates
@docs isSupersetOf, isSubsetOf, areDisjoint
@docs isSupersetOf, isSubsetOf, areDisjoint, any, all
# Set operations
Expand Down Expand Up @@ -160,6 +160,44 @@ areDisjoint a b =
not (Set.foldl (\x so -> so || Set.member x b) False a)


{-| Determine if any elements satisfy some test.
import Set exposing (Set)
Set.Extra.any (\n -> modBy 2 n == 0) (Set.fromList [ 2, 3 ])
--> True
Set.Extra.any (\n -> modBy 2 n == 0) (Set.fromList [ 1, 3 ])
--> False
Set.Extra.any (\n -> modBy 2 n == 0) Set.empty
--> False
-}
any : (a -> Bool) -> Set a -> Bool
any isOkay set =
Set.foldl (\x acc -> acc || isOkay x) False set


{-| Determine if all elements satisfy some test.
import Set exposing (Set)
Set.Extra.all (\n -> modBy 2 n == 0) (Set.fromList [ 2, 4 ])
--> True
Set.Extra.all (\n -> modBy 2 n == 0) (Set.fromList [ 2, 3 ])
--> False
Set.Extra.all (\n -> modBy 2 n == 0) Set.empty
--> True
-}
all : (a -> Bool) -> Set a -> Bool
all isOkay set =
Set.foldl (\x acc -> acc && isOkay x) True set


{-| The symmetric difference between two sets is a set that contains all the elements that are in one of the two sets, but not both.
import Set exposing (Set)
Expand Down

0 comments on commit 278a187

Please sign in to comment.