|
4 | 4 | {-# OPTIONS_GHC -Wall #-} |
5 | 5 | {-# LANGUAGE NamedFieldPuns #-} |
6 | 6 |
|
7 | | -module Action.Server(actionServer, actionReplay, action_server_test_, action_server_test) where |
| 7 | +module Action.Server(actionServer, actionReplay, action_server_test_, action_server_test, takeAndGroup) where |
8 | 8 |
|
9 | 9 | import Data.List.Extra |
10 | 10 | import System.FilePath |
@@ -115,7 +115,7 @@ replyServer log local links haddock store cdn home htmlDir scope Input{..} = cas |
115 | 115 | | local -> IsLocalUrl |
116 | 116 | | otherwise -> IsOtherUrl |
117 | 117 | 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 |
119 | 119 | case lookup "mode" inputArgs of |
120 | 120 | Nothing | qSource /= [] -> fmap OutputHTML $ templateRender templateIndex |
121 | 121 | [("tags", html $ tagOptions qScope) |
@@ -193,16 +193,21 @@ replyServer log local links haddock store cdn home htmlDir scope Input{..} = cas |
193 | 193 | templateLogJs = templateFile (htmlDir </> "log.js") `templateApply` params |
194 | 194 |
|
195 | 195 |
|
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 |
198 | 202 | 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 |
206 | 211 | where k = key x |
207 | 212 |
|
208 | 213 | data UrlOpts = IsHaddockUrl | IsLocalUrl | IsOtherUrl |
|
0 commit comments