on master help sign in
Add Set.{filter,partition} with tests
a24ef36Harry Garrood
libraries/Set.elm+12
@@ -2,6 +2,7 @@
2module Set (empty,singleton,insert,remove
2module Set (empty,singleton,insert,remove
3 ,member
3 ,member
4 ,foldl,foldr,map
4 ,foldl,foldr,map
5+ ,filter,partition
5 ,union,intersect,diff
6 ,union,intersect,diff
6 ,toList,fromList
7 ,toList,fromList
7 ) where
8 ) where
@@ -88,3 +89,14 @@ foldr f b s = Dict.foldr (\k _ b -> f k b) b s
88{-| Map a function onto a set, creating a new set with no duplicates. -}
89{-| Map a function onto a set, creating a new set with no duplicates. -}
89map : (comparable -> comparable') -> Set comparable -> Set comparable'
90map : (comparable -> comparable') -> Set comparable -> Set comparable'
90map f s = fromList (List.map f (toList s))
91map f s = fromList (List.map f (toList s))
92+
93+{-| Create a new set consisting only of elements which satisfy a predicate. -}
94+filter : (comparable -> Bool) -> Set comparable -> Set comparable
95+filter p = Dict.filter p
96+
97+{-| Create two new sets; the first consisting of elements which satisfy a
98+predicate, the second consisting of elements which do not. -}
99+partition : (comparable -> Bool)
100+ -> Set comparable
101+ -> (Set comparable, Set comparable)
102+partition p = Dict.partition p
tests/elm/Test/Set.elmnew file+26
@@ -0,0 +1,26 @@
1+module Test.Set (tests) where
2+
3+import Set
4+import List
5+
6+import ElmTest.Assertion (..)
7+import ElmTest.Test (..)
8+
9+set : Set
10+set = Set.fromList [1..100]
11+
12+setPart1 : Set
13+setPart1 = Set.fromList [1..50]
14+
15+setPart2 : Set
16+setPart2 = Set.fromList [51..100]
17+
18+pred : Int -> Bool
19+pred x = x <= 50
20+
21+tests : [Test]
22+tests =
23+ let
24+ filterTests = [assertEqual setPart1 <| filter pred set]
25+ partitionTests = [assertEqual (setPart1, setPart2) <| partition pred set]
26+ in List.concat [partitionTests, filterTests]