Skip to content

Commit

Permalink
Flips set predicates, adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gampleman committed Feb 22, 2024
1 parent ea253b0 commit 0c49670
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs.json

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions src/Set/Extra.elm
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,13 @@ concatMap f s =
import Set exposing (Set)
Set.Extra.isSubsetOf
(Set.fromList [1,2,3])
(Set.fromList [1,2,3,4,5])
Set.fromList [ 1, 2, 3 ]
|> Set.Extra.isSubsetOf (Set.fromList [1,2,3,4,5])
--> True
-}
isSubsetOf : Set comparable -> Set comparable -> Bool
isSubsetOf s1 s2 =
isSubsetOf s2 s1 =
Set.size s1
<= Set.size s2
&& Set.foldl (\x acc -> acc && Set.member x s2) True s1
Expand All @@ -92,9 +91,9 @@ isSubsetOf s1 s2 =
import Set exposing (Set)
Set.Extra.isSupersetOf
(Set.fromList [1,2,3])
(Set.fromList [1,2,3,4,5])
Set.fromList [ 1, 2, 3 ]
|> Set.Extra.isSupersetOf (Set.fromList [1,2,3,4,5])
--> False
Note: This is just isSubsetOf with arguments reversed. It can be handy for dealing with pipelines.
Expand Down Expand Up @@ -183,7 +182,7 @@ areDisjoint a b =
-}
subset : Set comparable -> Set comparable -> Bool
subset =
isSubsetOf
isSupersetOf


{-| 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.
Expand Down
28 changes: 23 additions & 5 deletions tests/SetTests.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module SetTests exposing (all)

import Basics.Extra exposing (flip)
import Expect
import Fuzz
import Fuzz exposing (Fuzzer)
import Set exposing (Set)
import Set.Extra
import Test exposing (Test, describe, fuzz, fuzz2, test)
Expand Down Expand Up @@ -45,23 +45,23 @@ all =
)
)
]
, describe "#subset"
, describe "#isSubsetOf"
[ fuzz2 (Fuzz.list Fuzz.int) (Fuzz.list Fuzz.int) "Same as List.Extra.isInfixOf" <|
\xs ys ->
Set.fromList xs
|> flip Set.Extra.subset (Set.fromList ys)
|> Set.Extra.isSubsetOf (Set.fromList ys)
|> Expect.equal
(List.all (flip List.member ys) xs)
, test "checks if a set is a subset of another set" <|
\() ->
Set.fromList [ 2, 4, 6 ]
|> flip Set.Extra.subset (Set.fromList [ 1, 2, 3, 4, 5, 6, 7, 8 ])
|> Set.Extra.isSubsetOf (Set.fromList [ 1, 2, 3, 4, 5, 6, 7, 8 ])
|> Expect.equal True
|> Expect.onFail "Expected the Set to be a subset"
, test "checks if a set isn't a subset of another set" <|
\() ->
Set.fromList [ 2, 4, 10 ]
|> flip Set.Extra.subset (Set.fromList [ 1, 2, 3, 4, 5, 6, 7, 8 ])
|> Set.Extra.isSubsetOf (Set.fromList [ 1, 2, 3, 4, 5, 6, 7, 8 ])
|> Expect.equal False
|> Expect.onFail "Expected the Set to not be a subset"
]
Expand Down Expand Up @@ -106,9 +106,27 @@ all =
|> Set.fromList
)
]
, describe "#areDisjoint"
[ fuzz2 (fuzzSet Fuzz.int) (fuzzSet Fuzz.int) "disjoint is equivalent to empty intersection" <|
\a b ->
Set.intersect a b
|> Set.isEmpty
|> Expect.equal (Set.Extra.areDisjoint a b)
]
, describe "#symmetricDifference"
[ fuzz2 (fuzzSet Fuzz.int) (fuzzSet Fuzz.int) "equivalent to diff (union a b) (intersect a b)" <|
\a b ->
Set.diff (Set.union a b) (Set.intersect a b)
|> Expect.equal (Set.Extra.symmetricDifference a b)
]
]


fuzzSet : Fuzzer comparable -> Fuzzer (Set comparable)
fuzzSet fuzz =
Fuzz.map Set.fromList (Fuzz.list fuzz)


doubleList : Int -> List Int
doubleList a =
[ a, 2 * a ]
Expand Down
14 changes: 13 additions & 1 deletion upgrade/src/ReviewConfig.elm
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,19 @@ v1To2 =
, Upgrade.reference { old = ( "Maybe.Extra", "traverseArray" ), new = ( "Maybe.Extra", "combineMapArray" ) }
, Upgrade.reference { old = ( "Result.Extra", "singleton" ), new = ( "Result", "Ok" ) }
, Upgrade.reference { old = ( "String.Extra", "removeAccents" ), new = ( "String.Extra", "removeDiacritics" ) }
, Upgrade.reference { old = ( "Set.Extra", "subset" ), new = ( "Set.Extra", "isSubsetOf" ) }
, Upgrade.application
{ oldName = ( "Set.Extra", "subset" )
, oldArgumentNames = [ "set1", "set2" ]
, oldArgumentsToNew =
\oldArguments ->
case oldArguments of
[ set1, set2 ] ->
Upgrade.call ( "Set.Extra", "isSubsetOf" ) [ set2, set1 ]
|> Just

_ ->
Nothing
}
, Upgrade.reference { old = ( "Tuple.Extra", "sequenceMaybe" ), new = ( "Maybe.Extra", "combineBoth" ) }
, Upgrade.reference { old = ( "Tuple.Extra", "sequenceFirstMaybe" ), new = ( "Maybe.Extra", "combineFirst" ) }
, Upgrade.reference { old = ( "Tuple.Extra", "sequenceSecondMaybe" ), new = ( "Maybe.Extra", "combineSecond" ) }
Expand Down

0 comments on commit 0c49670

Please sign in to comment.