on master help sign in
Remove Dict.{filter,partition}WithKey
5df3524Harry Garrood
libraries/Dict.elm+9−20
@@ -1,8 +1,8 @@
1module Dict (empty,singleton,insert,update
1module Dict (empty,singleton,insert,update
2 ,lookup,findWithDefault
2 ,lookup,findWithDefault
3 ,remove,member
3 ,remove,member
4− ,filter,filterWithKey
4+ ,filter
5− ,partition,partitionWithKey
5+ ,partition
6 ,foldl,foldr,map
6 ,foldl,foldr,map
7 ,union,intersect,diff
7 ,union,intersect,diff
8 ,keys,values
8 ,keys,values
@@ -329,7 +329,7 @@ union t1 t2 = foldl insert t2 t1
329{-| Keep a key-value pair when its key appears in the second dictionary.
329{-| Keep a key-value pair when its key appears in the second dictionary.
330Preference is given to values in the first dictionary. -}
330Preference is given to values in the first dictionary. -}
331intersect : Dict comparable v -> Dict comparable v -> Dict comparable v
331intersect : Dict comparable v -> Dict comparable v -> Dict comparable v
332−intersect t1 t2 = filterWithKey (\k _ -> k `member` t2) t1
332+intersect t1 t2 = filter (\k _ -> k `member` t2) t1
333
333
334{-| Keep a key-value pair when its key does not appear in the second dictionary.
334{-| Keep a key-value pair when its key does not appear in the second dictionary.
335Preference is given to the first dictionary. -}
335Preference is given to the first dictionary. -}
@@ -352,31 +352,20 @@ toList t = foldr (\k v acc -> (k,v) :: acc) [] t
352fromList : [(comparable,v)] -> Dict comparable v
352fromList : [(comparable,v)] -> Dict comparable v
353fromList assocs = List.foldl (\(k,v) d -> insert k v d) empty assocs
353fromList assocs = List.foldl (\(k,v) d -> insert k v d) empty assocs
354
354
355−{-| Keep only key-value pairs where the value satisfies a predicate. -}
355+{-| Keep a key-value pair when it satisfies a predicate. -}
356−filter : (v -> Bool) -> Dict comparable v -> Dict comparable v
357−filter p = filterWithKey (\_ v -> p v)
358−
359−{-| Keep only key-value pairs which satisfy a predicate. -}
360−filterWithKey : (comparable -> v -> Bool)
356+filter : (comparable -> v -> Bool)
361 -> Dict comparable v
357 -> Dict comparable v
362 -> Dict comparable v
358 -> Dict comparable v
363−filterWithKey p =
359+filter p =
364 let add k v t = if p k v then insert k v t else t
360 let add k v t = if p k v then insert k v t else t
365 in foldl add empty
361 in foldl add empty
366
362
367{-| Partition the Dict according to a predicate. The first Dict contains all
363{-| Partition the Dict according to a predicate. The first Dict contains all
368−values which satisfy a predicate; the second contains the rest. -}
364+key-value pairs which satisfy it; the second contains the rest. -}
369−partition : (v -> Bool)
365+partition : (comparable -> v -> Bool)
370 -> Dict comparable v
366 -> Dict comparable v
371 -> (Dict comparable v, Dict comparable v)
367 -> (Dict comparable v, Dict comparable v)
372−partition p = partitionWithKey (\_ v -> p v)
368+partition p =
373−
374−{-| Partition the Dict according to a predicate. The first Dict contains all
375−key-value pairs which satisfy a predicate; the second contains the rest. -}
376−partitionWithKey : (comparable -> v -> Bool)
377− -> Dict comparable v
378− -> (Dict comparable v, Dict comparable v)
379−partitionWithKey p =
380 let add k v (t1, t2) = if p k v
369 let add k v (t1, t2) = if p k v
381 then (insert k v t1, t2)
370 then (insert k v t1, t2)
382 else (t1, insert k v t2)
371 else (t1, insert k v t2)
libraries/Set.elm+2−2
@@ -92,11 +92,11 @@ map f s = fromList (List.map f (toList s))
92
92
93{-| Create a new set consisting only of elements which satisfy a predicate. -}
93{-| Create a new set consisting only of elements which satisfy a predicate. -}
94filter : (comparable -> Bool) -> Set comparable -> Set comparable
94filter : (comparable -> Bool) -> Set comparable -> Set comparable
95−filter p = Dict.filterWithKey (\k _ -> p k)
95+filter p = Dict.filter (\k _ -> p k)
96
96
97{-| Create two new sets; the first consisting of elements which satisfy a
97{-| Create two new sets; the first consisting of elements which satisfy a
98predicate, the second consisting of elements which do not. -}
98predicate, the second consisting of elements which do not. -}
99partition : (comparable -> Bool)
99partition : (comparable -> Bool)
100 -> Set comparable
100 -> Set comparable
101 -> (Set comparable, Set comparable)
101 -> (Set comparable, Set comparable)
102−partition p = Dict.partitionWithKey (\k _ -> p k)
102+partition p = Dict.partition (\k _ -> p k)
tests/elm/Test/Dict.elm+2−16
@@ -21,23 +21,9 @@ dict1Part1 = Dict.fromList (take 2 dict1List)
21dict1Part2 : Dict Int Char
21dict1Part2 : Dict Int Char
22dict1Part2 = Dict.fromList (drop 2 dict1List)
22dict1Part2 = Dict.fromList (drop 2 dict1List)
23
23
24−dict2List : Dict Int Char
25−dict2List = [(1,'2'),(3,'3'),(7,'4'),(8,'3')]
26−
27−dict2 : Dict Int Char
28−dict2 = Dict.fromList dict2List
29−
30−dict2Part1 : Dict Int Char
31−dict2Part1 = Dict.fromList (take 2 dict2List)
32−
33−dict2Part2 : Dict Int Char
34−dict2Part2 = Dict.fromList (drop 2 dict2List)
35−
36tests : [Test]
24tests : [Test]
37tests =
25tests =
38 let
26 let
39− partitionTests = [assertEqual (dict1Part1, dict1Part2) <| partition (\v -> v != "3" && v != "5") dict1]
40− partitionWithKeyTests = [assertEqual (dict2Part1, dict2Part2) <| partition (\k v -> k <= parseDigit v)]
41 filterTests = [assertEqual dict1Part2 <| filter (\v -> parseDigit v > 2) dict1]
27 filterTests = [assertEqual dict1Part2 <| filter (\v -> parseDigit v > 2) dict1]
42− filterWithKeyTests = [assertEqual dict2Part2 <| filterWithKey (\k _ -> k > 6) dict2]
28+ partitionTests = [assertEqual (dict1Part1, dict1Part2) <| partition (\v -> v != '3' && v != '5') dict1]
43− in List.concat [partitionTests, partitionWithKeyTests, filterTests, filterWithKeyTests]
29+ in List.concat [filterTests, partitionTests]