Skip to content

Instantly share code, notes, and snippets.

@benkolera
Created November 2, 2011 12:14
Show Gist options
  • Save benkolera/1333486 to your computer and use it in GitHub Desktop.
Save benkolera/1333486 to your computer and use it in GitHub Desktop.
Runtime Dependency Resolution ( by a haskell noob )
import qualified Data.Map as Map
import Data.List
import Control.Applicative
englishTranslator = ("ENGLISH: " ++)
germanTranslator = ("GERMAN: " ++)
windowsUnlines = intercalate "\r\n"
unixUnlines = intercalate "\n"
translators = Map.fromList [
("english",englishTranslator),
("german",germanTranslator)
]
unliners = Map.fromList [
("windows",windowsUnlines),
("unix",unixUnlines)
]
processString :: ([String] -> String) -> ( String -> String ) -> [String] -> String
processString unliner translator = translator . unliner
makeStringProcessor osType language =
let
unliner = Map.lookup osType unliners
translator = Map.lookup language translators
in
case processString <$> unliner <*> translator of
Nothing -> error $ intercalate "" [
"Cannot construct processor for os '",
osType,
"' and language '",
language,
"'"
]
Just f -> f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment