Skip to content

Commit 15bfb98

Browse files
committed
Action/Server/dedupeTake: rename to takeAndGroup & document
The function does not really deduplicate the elements, it takes and groups. :)
1 parent 173952e commit 15bfb98

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/Action/Server.hs

+15-10
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ replyServer log local links haddock store cdn home htmlDir scope Input{..} = cas
115115
| local -> IsLocalUrl
116116
| otherwise -> IsOtherUrl
117117
let body = showResults urlOpts links (filter ((/= "mode") . fst) inputArgs) q2 $
118-
dedupeTake 25 (\t -> t{targetURL="",targetPackage=Nothing, targetModule=Nothing}) results
118+
takeAndGroup 25 (\t -> t{targetURL="",targetPackage=Nothing, targetModule=Nothing}) results
119119
case lookup "mode" inputArgs of
120120
Nothing | qSource /= [] -> fmap OutputHTML $ templateRender templateIndex
121121
[("tags", html $ tagOptions qScope)
@@ -193,16 +193,21 @@ replyServer log local links haddock store cdn home htmlDir scope Input{..} = cas
193193
templateLogJs = templateFile (htmlDir </> "log.js") `templateApply` params
194194

195195

196-
dedupeTake :: Ord k => Int -> (v -> k) -> [v] -> [[v]]
197-
dedupeTake n key = f [] Map.empty
196+
-- | Take from the list until we’ve seen `n` different keys,
197+
-- and group all values by their respective key.
198+
--
199+
-- Will keep the order of elements for each key the same.
200+
takeAndGroup :: Ord k => Int -> (v -> k) -> [v] -> [[v]]
201+
takeAndGroup n key = f [] Map.empty
198202
where
199-
-- map is Map k [v]
200-
f res mp []
201-
= map (reverse . (Map.!) mp) $ reverse res
202-
f res mp _ | Map.size mp >= n
203-
= map (reverse . (Map.!) mp) $ reverse res
204-
f res mp (x:xs) | Just vs <- Map.lookup k mp = f res (Map.insert k (x:vs) mp) xs
205-
| otherwise = f (k:res) (Map.insert k [x] mp) xs
203+
-- mp is Map k [v]
204+
f keys mp []
205+
= map (\k -> reverse $ mp Map.! k) $ reverse keys
206+
f keys mp _ | Map.size mp >= n
207+
= map (\k -> reverse $ mp Map.! k) $ reverse keys
208+
f keys mp (x:xs)
209+
| Just vs <- Map.lookup k mp = f keys (Map.insert k (x:vs) mp) xs
210+
| otherwise = f (k:keys) (Map.insert k [x] mp) xs
206211
where k = key x
207212

208213
data UrlOpts = IsHaddockUrl | IsLocalUrl | IsOtherUrl

0 commit comments

Comments
 (0)