repos / handle-examples.hs.git


handle-examples.hs.git / backpack-handles / impl
Evgenii Akentev  ·  2021-01-09

SuperWeatherReporter.hs

 1module SuperWeatherReporter where
 2
 3import qualified WeatherProvider
 4
 5type WeatherReport = String
 6
 7type WeatherProviderHandle = WeatherProvider.Handle
 8
 9-- | We hide dependencies in the handle
10data Handle = Handle { weatherProvider :: WeatherProvider.Handle }
11
12-- | Constructor for Handle
13new :: WeatherProvider.Handle -> Handle
14new = Handle
15
16-- | Domain logic. Usually some pure code that might use mtl, free monads, etc.
17createWeatherReport :: WeatherProvider.WeatherData -> WeatherReport
18createWeatherReport (WeatherProvider.WeatherData temp) =
19  "The current temperature in London is " ++ (show temp)
20
21-- | Domain logic that uses external dependency to get data and process it.
22getCurrentWeatherReportInLondon :: Handle -> IO WeatherReport
23getCurrentWeatherReportInLondon (Handle wph) = do
24  weatherData <- WeatherProvider.getWeatherData wph "London" "now"
25  return $ createWeatherReport weatherData