Created
August 2, 2016 14:28
-
-
Save lunaris/a496625f069394a53339925cdcc2b07d to your computer and use it in GitHub Desktop.
Resolving trees with maps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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