Skip to content

Instantly share code, notes, and snippets.

@lunaris
Created August 2, 2016 14:28
Show Gist options
  • Save lunaris/a496625f069394a53339925cdcc2b07d to your computer and use it in GitHub Desktop.
Save lunaris/a496625f069394a53339925cdcc2b07d to your computer and use it in GitHub Desktop.
Resolving trees with maps
module Maps where
import qualified Data.Maybe as Mb
import qualified Data.Map.Strict as M
data Plugin
= Plugin { pName :: String, pDependencies :: [String] }
deriving (Eq, Show)
plugins :: [Plugin]
plugins
= [ Plugin "thing" ["thang", "thung"]
, Plugin "thung" ["thang"]
, Plugin "thang" ["foo", "bar"]
, Plugin "foo" []
, Plugin "bar" ["baz"]
, Plugin "baz" []
]
data ResolvedPlugin
= ResolvedPlugin { rpName :: String, rpDependencies :: [ResolvedPlugin] }
deriving (Eq, Show)
resolvePlugins :: [Plugin] -> [ResolvedPlugin]
resolvePlugins ps
= map snd (M.toList rpm)
where
pm = M.fromList (map (\p -> (pName p, p)) ps)
rpm = M.map (resolvePlugin rpm) pm
resolvePlugin :: M.Map String ResolvedPlugin -> Plugin -> ResolvedPlugin
resolvePlugin m (Plugin name deps)
= ResolvedPlugin name (Mb.catMaybes (map (flip M.lookup m) deps))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment