Ticket #1816: mapMaybe.dpatch

File mapMaybe.dpatch, 1.2 KB (added by guest, 7 years ago)
Line 
1
2New patches:
3
4[Add 'mapMaybe' function to Data.Set.
5Thomas Schilling <[email protected]>**20071031194807] {
6hunk ./Data/Set.hs 71
7+            , mapMaybe
8hunk ./Data/Set.hs 421
9+-- | /O(n*log n)/.  Map over all elements and collect the 'Just' results.
10+--
11+-- This function is equivalent to, but slightly more efficient than:
12+--
13+-- > mapMaybe f = fromList . catMaybes . map f . toList
14+
15+mapMaybe :: (Ord a, Ord b) => (a -> Maybe b) -> Set a -> Set b
16+mapMaybe f = foldlStrict g empty . toList
17+  where
18+    g s x = case f x of
19+              Nothing -> s
20+              Just y  -> insert y s
21hunk ./Data/Set.hs 1162
22+
23+{--------------------------------------------------------------------
24+  Utils
25+--------------------------------------------------------------------}
26+prop_MapMaybe f s
27+  = mapMaybe f s == (fromList . cat . List.map f . toList)  s
28+  where
29+    cat [] = []
30+    cat (Nothing:xs) = cat xs
31+    cat (Just x:xs)  = x : cat xs
32}
33
34Context:
35
36[Specify build-type: Simple
37Duncan Coutts <[email protected]>**20071018125404]
38[Add a boring file
39Ian Lynagh <[email protected]>**20070913204647]
40[TAG 2007-09-13
41Ian Lynagh <[email protected]>**20070913215901]
42Patch bundle hash:
4307d0a43017a56610c76ca85a6cbc83bf8df296c6