Add README and more examples.
authorEvgenii Akentev <i@ak3n.com>
Thu, 7 Jan 2021 15:49:51 +0000 (20:49 +0500)
committerEvgenii Akentev <i@ak3n.com>
Thu, 7 Jan 2021 15:52:04 +0000 (20:52 +0500)
32 files changed:
.gitignore
README.md
backpack-handle/CHANGELOG.md [deleted file]
backpack-handle/Main.hs
backpack-handle/backpack-handle.cabal
backpack-handle/domain/WeatherReporter.hs
backpack-handle/test/Test.hs
backpack-handles/LICENSE [new file with mode: 0644]
backpack-handles/Main.hs [new file with mode: 0644]
backpack-handles/Setup.hs [new file with mode: 0644]
backpack-handles/backpack-handles.cabal [new file with mode: 0644]
backpack-handles/domain/WeatherProvider.hsig [new file with mode: 0644]
backpack-handles/domain/WeatherReporter.hsig [new file with mode: 0644]
backpack-handles/impl/SuperWeatherProvider.hs [new file with mode: 0644]
backpack-handles/impl/SuperWeatherReporter.hs [new file with mode: 0644]
backpack-handles/test-impl/TestWeatherProvider.hs [new file with mode: 0644]
backpack-handles/test/Test.hs [new file with mode: 0644]
records-handle/CHANGELOG.md [deleted file]
records-handle/Main.hs
records-handle/domain/WeatherReporter.hs
records-handle/records-handle.cabal
records-handle/test/Test.hs
simple-handle/CHANGELOG.md [deleted file]
simple-handle/Main.hs
simple-handle/domain/WeatherReporter.hs
simple-handle/simple-handle.cabal
simple/LICENSE [new file with mode: 0644]
simple/Main.hs [new file with mode: 0644]
simple/Setup.hs [new file with mode: 0644]
simple/domain/WeatherProvider.hs [new file with mode: 0644]
simple/domain/WeatherReporter.hs [new file with mode: 0644]
simple/simple.cabal [new file with mode: 0644]

index 4c9e245b5d804bf398f9e0eb6c9366774cb404d3..bbdbf825e5b313a94f1c46a6b30ce939e0c2c5a9 100644 (file)
@@ -1,3 +1,4 @@
+.DS_Store
 dist
 dist-*
 cabal-dev
index 8b12c7ca3c686373db5d3d5552209645391956f6..d77edfb0114d040f5d6c55e6b40785ccc5aa7032 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1,3 +1,11 @@
-# backpack-handle-example
+This repository contains examples of the Handle pattern. We start from `simple` that contains domain logic and iterate trying to use records and backpack as a way to test the domain logic.
 
-The examples of the Handle pattern in simple case, with records, and backpack.
+- `simple` is a library with two modules: `WeatherProvider` (provides data) and `WeatherReporter` (uses the data to create a report).
+
+- `simple-handle` introduces a single-implementation Handle to both modules.
+
+- `records-handle` implements a polymorphic Handle for `WeatherProvider` allowing us to replace the implementation and write tests for domain logic.
+
+- `backpack-handle` does the same thing as `records-handle` but using Backpack instead. It allows us to specialize function calls.
+
+- `backpack-handles` goes further and makes both `WeatherProvider` and `WeatherReporter` signatures. Unfortunately, it doesn't work yet.
diff --git a/backpack-handle/CHANGELOG.md b/backpack-handle/CHANGELOG.md
deleted file mode 100644 (file)
index 806c22c..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-# Revision history for backpack-handle-pattern
-
-## 0.1.0.0 -- YYYY-mm-dd
-
-* First version. Released on an unsuspecting world.
index 07cd3deccac06c9450e842a8a0a7c56d02dddfed..197df484b328e34b6dba9023417f988288efe959 100644 (file)
@@ -9,5 +9,6 @@ import qualified WeatherReporter
 main :: IO ()
 main = do
   let wph = SuperWeatherProvider.new
-  weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon wph
+  let wrh = WeatherReporter.new wph
+  weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon wrh
   putStrLn weatherReportInLondon
index c4277296b8005cd81e743ea6cc2213292892f435..3bf756bbfa9376719948c8055e5e24bd91c12921 100644 (file)
@@ -28,7 +28,7 @@ library test-impl
   default-language: Haskell2010
   build-depends:    base
 
-executable backpack-handle-exe
+executable main
   main-is:             Main.hs
   build-depends:       base >=4.13 && <4.14
                      , impl
index e0967500890a6ed727d698875227d75451549502..8a521e98d89f2b07c2aa3db4b52cbe84a0147634 100644 (file)
@@ -1,11 +1,23 @@
 module WeatherReporter where
 
-import WeatherProvider
+import qualified WeatherProvider
 
 type WeatherReport = String
 
--- | This is domain logic. It uses `WeatherProvider` to get the actual data.
-getCurrentWeatherReportInLondon :: WeatherProvider.Handle -> IO WeatherReport
-getCurrentWeatherReportInLondon wph = do
+-- | We hide dependencies in the handle
+data Handle = Handle { weatherProvider :: WeatherProvider.Handle }
+
+-- | Constructor for Handle
+new :: WeatherProvider.Handle -> Handle
+new = Handle
+
+-- | Domain logic. Usually some pure code that might use mtl, free monads, etc.
+createWeatherReport :: WeatherProvider.WeatherData -> WeatherReport
+createWeatherReport (WeatherProvider.WeatherData temp) =
+  "The current temperature in London is " ++ (show temp)
+
+-- | Domain logic that uses external dependency to get data and process it.
+getCurrentWeatherReportInLondon :: Handle -> IO WeatherReport
+getCurrentWeatherReportInLondon (Handle wph) = do
   weatherData <- WeatherProvider.getWeatherData wph "London" "now"
-  return $ "Current temperature in London is " ++ (show $ temperature weatherData)
\ No newline at end of file
+  return $ createWeatherReport weatherData
index 18dd2e5feba14a2ea18cee36a270047b5f44a801..83d1e92c4f689480aaca64d70768a6a3697de34d 100644 (file)
@@ -1,22 +1,23 @@
 import Test.Hspec
 
-import qualified TestWeatherProvider
 import qualified WeatherProvider
 import qualified WeatherReporter
 
 main :: IO ()
 main = hspec spec
 
-weatherWithTemp :: WeatherProvider.Temperature -> WeatherProvider.Handle
-weatherWithTemp = TestWeatherProvider.new . TestWeatherProvider.Config 
+weatherWithTemp :: WeatherProvider.Temperature -> WeatherReporter.Handle
+weatherWithTemp = WeatherReporter.new
+  . WeatherProvider.new
+  . WeatherProvider.Config
 
 spec :: Spec
 spec = describe "WeatherReporter" $ do
   it "weather in London is 0" $ do
     weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon $
       weatherWithTemp 0
-    weatherReportInLondon `shouldBe` "Current temperature in London is 0"
+    weatherReportInLondon `shouldBe` "The current temperature in London is 0"
   it "weather in London is -5" $ do
     weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon $
       weatherWithTemp (-5)
-    weatherReportInLondon `shouldBe` "Current temperature in London is -5"
+    weatherReportInLondon `shouldBe` "The current temperature in London is -5"
diff --git a/backpack-handles/LICENSE b/backpack-handles/LICENSE
new file mode 100644 (file)
index 0000000..9eea539
--- /dev/null
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2021 Evgenii Akentev
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/backpack-handles/Main.hs b/backpack-handles/Main.hs
new file mode 100644 (file)
index 0000000..0e6baa0
--- /dev/null
@@ -0,0 +1,13 @@
+module Main where
+
+import qualified SuperWeatherProvider
+import qualified SuperWeatherReporter
+
+-- | This is an actual application where we use
+-- our concrete implementation of `WeatherProvider`.
+main :: IO ()
+main = do
+  let wph = SuperWeatherProvider.new
+  let wpr = SuperWeatherReporter.Handle wph
+  weatherReportInLondon <- SuperWeatherReporter.getCurrentWeatherReportInLondon wpr
+  putStrLn weatherReportInLondon
diff --git a/backpack-handles/Setup.hs b/backpack-handles/Setup.hs
new file mode 100644 (file)
index 0000000..9a994af
--- /dev/null
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
diff --git a/backpack-handles/backpack-handles.cabal b/backpack-handles/backpack-handles.cabal
new file mode 100644 (file)
index 0000000..e8ee369
--- /dev/null
@@ -0,0 +1,57 @@
+cabal-version:       >=2
+name:                backpack-handles
+version:             0.1.0.0
+license-file:        LICENSE
+author:              Evgenii Akentev
+maintainer:          i@ak3n.com
+build-type:          Simple
+extra-source-files:  CHANGELOG.md
+
+library domain
+  hs-source-dirs: domain
+  signatures:      WeatherProvider
+  default-language: Haskell2010
+  build-depends:    base
+
+library domain-reporter
+  hs-source-dirs: domain
+  signatures:      WeatherReporter
+  default-language: Haskell2010
+  build-depends:    base, domain
+
+library impl
+  hs-source-dirs: impl
+  exposed-modules: SuperWeatherProvider
+                 , SuperWeatherReporter
+  reexported-modules: SuperWeatherProvider as WeatherProvider,
+    SuperWeatherReporter as WeatherReporter
+  default-language: Haskell2010
+  build-depends:    base
+
+library test-impl
+  hs-source-dirs: test-impl
+  exposed-modules: TestWeatherProvider
+  reexported-modules: TestWeatherProvider as WeatherProvider
+  default-language: Haskell2010
+  build-depends:    base
+
+executable main
+  main-is:             Main.hs
+  build-depends:       base >=4.13 && <4.14
+                     , impl
+                     , domain
+  default-language:    Haskell2010
+
+test-suite spec
+  type:             exitcode-stdio-1.0
+  hs-source-dirs:   test
+  main-is:          Test.hs
+  default-language:   Haskell2010
+  build-depends:       base >= 4.7 && < 5
+                     , QuickCheck
+                     , hspec
+                     , domain
+                     , test-impl
+                     , impl
+  mixins:
+    impl (WeatherProvider as UnusedWeatherProvider, WeatherReporter)
diff --git a/backpack-handles/domain/WeatherProvider.hsig b/backpack-handles/domain/WeatherProvider.hsig
new file mode 100644 (file)
index 0000000..56de95a
--- /dev/null
@@ -0,0 +1,14 @@
+signature WeatherProvider where
+
+data Temperature
+instance Show Temperature
+
+data WeatherData = WeatherData { temperature :: Temperature }
+
+type Location = String
+type Day = String
+
+data Handle
+
+-- | The interface of `WeatherProvider` with available methods.
+getWeatherData :: Handle -> Location -> Day -> IO WeatherData
diff --git a/backpack-handles/domain/WeatherReporter.hsig b/backpack-handles/domain/WeatherReporter.hsig
new file mode 100644 (file)
index 0000000..17ea47b
--- /dev/null
@@ -0,0 +1,10 @@
+signature WeatherReporter where
+
+import qualified WeatherProvider
+
+type WeatherReport = String
+
+data Handle = Handle { weatherProvider :: WeatherProvider.Handle }
+
+-- | This is domain logic. It uses `WeatherProvider` to get the actual data.
+getCurrentWeatherReportInLondon :: Handle -> IO WeatherReport
diff --git a/backpack-handles/impl/SuperWeatherProvider.hs b/backpack-handles/impl/SuperWeatherProvider.hs
new file mode 100644 (file)
index 0000000..1a29069
--- /dev/null
@@ -0,0 +1,16 @@
+module SuperWeatherProvider where
+
+type Temperature = Int
+data WeatherData = WeatherData { temperature :: Temperature }
+
+type Location = String
+type Day = String
+
+data Handle = Handle
+
+new :: Handle
+new = Handle
+
+-- | This is some concrete implementation `WeatherProvider` interface
+getWeatherData :: Handle -> Location -> Day -> IO WeatherData
+getWeatherData _ _ _ = return $ WeatherData 30
diff --git a/backpack-handles/impl/SuperWeatherReporter.hs b/backpack-handles/impl/SuperWeatherReporter.hs
new file mode 100644 (file)
index 0000000..4a6ed2b
--- /dev/null
@@ -0,0 +1,25 @@
+module SuperWeatherReporter where
+
+import qualified SuperWeatherProvider
+
+type WeatherReport = String
+
+type WeatherProviderHandle = SuperWeatherProvider.Handle
+
+-- | We hide dependencies in the handle
+data Handle = Handle { weatherProvider :: SuperWeatherProvider.Handle }
+
+-- | Constructor for Handle
+new :: SuperWeatherProvider.Handle -> Handle
+new = Handle
+
+-- | Domain logic. Usually some pure code that might use mtl, free monads, etc.
+createWeatherReport :: SuperWeatherProvider.WeatherData -> WeatherReport
+createWeatherReport (SuperWeatherProvider.WeatherData temp) =
+  "The current temperature in London is " ++ (show temp)
+
+-- | Domain logic that uses external dependency to get data and process it.
+getCurrentWeatherReportInLondon :: Handle -> IO WeatherReport
+getCurrentWeatherReportInLondon (Handle wph) = do
+  weatherData <- SuperWeatherProvider.getWeatherData wph "London" "now"
+  return $ createWeatherReport weatherData
diff --git a/backpack-handles/test-impl/TestWeatherProvider.hs b/backpack-handles/test-impl/TestWeatherProvider.hs
new file mode 100644 (file)
index 0000000..900b102
--- /dev/null
@@ -0,0 +1,23 @@
+module TestWeatherProvider where
+
+type Temperature = Int
+data WeatherData = WeatherData { temperature :: Temperature }
+
+type Location = String
+type Day = String
+
+-- | This is a configuration that allows to setup the provider for tests.
+data Config = Config
+  { initTemperature :: Temperature
+  }
+
+data Handle = Handle
+  { config :: Config
+  }
+
+new :: Config -> Handle
+new = Handle
+
+-- | This is an implementation `WeatherProvider` interface for tests
+getWeatherData :: Handle -> Location -> Day -> IO WeatherData
+getWeatherData (Handle conf) _ _ = return $ WeatherData $ initTemperature conf
diff --git a/backpack-handles/test/Test.hs b/backpack-handles/test/Test.hs
new file mode 100644 (file)
index 0000000..b329480
--- /dev/null
@@ -0,0 +1,23 @@
+import Test.Hspec
+
+import qualified WeatherProvider
+import qualified WeatherReporter
+
+main :: IO ()
+main = hspec spec
+
+weatherWithTemp :: WeatherProvider.Temperature -> WeatherReporter.Handle
+weatherWithTemp t = WeatherReporter.new
+  $ WeatherProvider.new
+  $ WeatherProvider.Config t
+
+spec :: Spec
+spec = describe "WeatherReporter" $ do
+  it "weather in London is 0" $ do
+    weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon $
+      weatherWithTemp 0
+    weatherReportInLondon `shouldBe` "The current temperature in London is 0"
+  it "weather in London is -5" $ do
+    weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon $
+      weatherWithTemp (-5)
+    weatherReportInLondon `shouldBe` "The current temperature in London is -5"
diff --git a/records-handle/CHANGELOG.md b/records-handle/CHANGELOG.md
deleted file mode 100644 (file)
index 806c22c..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-# Revision history for backpack-handle-pattern
-
-## 0.1.0.0 -- YYYY-mm-dd
-
-* First version. Released on an unsuspecting world.
index 07cd3deccac06c9450e842a8a0a7c56d02dddfed..197df484b328e34b6dba9023417f988288efe959 100644 (file)
@@ -9,5 +9,6 @@ import qualified WeatherReporter
 main :: IO ()
 main = do
   let wph = SuperWeatherProvider.new
-  weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon wph
+  let wrh = WeatherReporter.new wph
+  weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon wrh
   putStrLn weatherReportInLondon
index e0967500890a6ed727d698875227d75451549502..8a521e98d89f2b07c2aa3db4b52cbe84a0147634 100644 (file)
@@ -1,11 +1,23 @@
 module WeatherReporter where
 
-import WeatherProvider
+import qualified WeatherProvider
 
 type WeatherReport = String
 
--- | This is domain logic. It uses `WeatherProvider` to get the actual data.
-getCurrentWeatherReportInLondon :: WeatherProvider.Handle -> IO WeatherReport
-getCurrentWeatherReportInLondon wph = do
+-- | We hide dependencies in the handle
+data Handle = Handle { weatherProvider :: WeatherProvider.Handle }
+
+-- | Constructor for Handle
+new :: WeatherProvider.Handle -> Handle
+new = Handle
+
+-- | Domain logic. Usually some pure code that might use mtl, free monads, etc.
+createWeatherReport :: WeatherProvider.WeatherData -> WeatherReport
+createWeatherReport (WeatherProvider.WeatherData temp) =
+  "The current temperature in London is " ++ (show temp)
+
+-- | Domain logic that uses external dependency to get data and process it.
+getCurrentWeatherReportInLondon :: Handle -> IO WeatherReport
+getCurrentWeatherReportInLondon (Handle wph) = do
   weatherData <- WeatherProvider.getWeatherData wph "London" "now"
-  return $ "Current temperature in London is " ++ (show $ temperature weatherData)
\ No newline at end of file
+  return $ createWeatherReport weatherData
index ac1318e4206fc4eeb4cb71e9391602e16395c2a4..1540e7e2170bda3dfacae6bde74a086292b56805 100644 (file)
@@ -28,7 +28,7 @@ library test-impl
   build-depends:    base
                   , domain
 
-executable records-handle-exe
+executable main
   main-is:             Main.hs
   build-depends:       base >=4.13 && <4.14
                      , domain
index 18dd2e5feba14a2ea18cee36a270047b5f44a801..7466dd49de053378980bd12ba1c103b9fda048f8 100644 (file)
@@ -7,16 +7,18 @@ import qualified WeatherReporter
 main :: IO ()
 main = hspec spec
 
-weatherWithTemp :: WeatherProvider.Temperature -> WeatherProvider.Handle
-weatherWithTemp = TestWeatherProvider.new . TestWeatherProvider.Config 
+weatherWithTemp :: WeatherProvider.Temperature -> WeatherReporter.Handle
+weatherWithTemp = WeatherReporter.new
+  . TestWeatherProvider.new
+  . TestWeatherProvider.Config
 
 spec :: Spec
 spec = describe "WeatherReporter" $ do
   it "weather in London is 0" $ do
     weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon $
       weatherWithTemp 0
-    weatherReportInLondon `shouldBe` "Current temperature in London is 0"
+    weatherReportInLondon `shouldBe` "The current temperature in London is 0"
   it "weather in London is -5" $ do
     weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon $
       weatherWithTemp (-5)
-    weatherReportInLondon `shouldBe` "Current temperature in London is -5"
+    weatherReportInLondon `shouldBe` "The current temperature in London is -5"
diff --git a/simple-handle/CHANGELOG.md b/simple-handle/CHANGELOG.md
deleted file mode 100644 (file)
index 806c22c..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-# Revision history for backpack-handle-pattern
-
-## 0.1.0.0 -- YYYY-mm-dd
-
-* First version. Released on an unsuspecting world.
index e1d6702f50ed2c837eb2344b1d44ccf7b799837d..c32c6e92b11d6bef2371c3c881eb48f98d9fd987 100644 (file)
@@ -6,5 +6,6 @@ import qualified WeatherReporter
 main :: IO ()
 main = do
   let wph = WeatherProvider.new
-  weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon wph
+  let wrh = WeatherReporter.new wph
+  weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon wrh
   putStrLn weatherReportInLondon
index 88a9d4453dd953bb646aed1431cf64b618da38a4..8a521e98d89f2b07c2aa3db4b52cbe84a0147634 100644 (file)
@@ -1,16 +1,23 @@
 module WeatherReporter where
 
-import WeatherProvider
+import qualified WeatherProvider
 
 type WeatherReport = String
 
+-- | We hide dependencies in the handle
+data Handle = Handle { weatherProvider :: WeatherProvider.Handle }
+
+-- | Constructor for Handle
+new :: WeatherProvider.Handle -> Handle
+new = Handle
+
 -- | Domain logic. Usually some pure code that might use mtl, free monads, etc.
-createWeatherReport :: WeatherData -> WeatherReport
-createWeatherReport (WeatherData temp) =
+createWeatherReport :: WeatherProvider.WeatherData -> WeatherReport
+createWeatherReport (WeatherProvider.WeatherData temp) =
   "The current temperature in London is " ++ (show temp)
 
 -- | Domain logic that uses external dependency to get data and process it.
-getCurrentWeatherReportInLondon :: WeatherProvider.Handle -> IO WeatherReport
-getCurrentWeatherReportInLondon wph = do
+getCurrentWeatherReportInLondon :: Handle -> IO WeatherReport
+getCurrentWeatherReportInLondon (Handle wph) = do
   weatherData <- WeatherProvider.getWeatherData wph "London" "now"
   return $ createWeatherReport weatherData
index 2a349a87d26fdc67431bf8d523bfc54a00678e13..94b079b295d8fd835ac47fa40fb4ac3a92ac4969 100644 (file)
@@ -14,7 +14,7 @@ library domain
   default-language: Haskell2010
   build-depends:    base
 
-executable simple-handle-exe
+executable main
   main-is:             Main.hs
   build-depends:       base >=4.13 && <4.14
                      , domain
diff --git a/simple/LICENSE b/simple/LICENSE
new file mode 100644 (file)
index 0000000..9eea539
--- /dev/null
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2021 Evgenii Akentev
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/simple/Main.hs b/simple/Main.hs
new file mode 100644 (file)
index 0000000..c0cbd2b
--- /dev/null
@@ -0,0 +1,8 @@
+module Main where
+
+import qualified WeatherReporter
+
+main :: IO ()
+main = do
+  weatherReportInLondon <- WeatherReporter.getCurrentWeatherReportInLondon
+  putStrLn weatherReportInLondon
diff --git a/simple/Setup.hs b/simple/Setup.hs
new file mode 100644 (file)
index 0000000..9a994af
--- /dev/null
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
diff --git a/simple/domain/WeatherProvider.hs b/simple/domain/WeatherProvider.hs
new file mode 100644 (file)
index 0000000..4cb6f5c
--- /dev/null
@@ -0,0 +1,12 @@
+module WeatherProvider where
+
+type Temperature = Int
+data WeatherData = WeatherData { temperature :: Temperature }
+
+type Location = String
+type Day = String
+
+-- | This is some concrete implementation.
+-- In this example we return a constant value.
+getWeatherData :: Location -> Day -> IO WeatherData
+getWeatherData _ _ = return $ WeatherData 30
diff --git a/simple/domain/WeatherReporter.hs b/simple/domain/WeatherReporter.hs
new file mode 100644 (file)
index 0000000..bc0d214
--- /dev/null
@@ -0,0 +1,16 @@
+module WeatherReporter where
+
+import qualified WeatherProvider
+
+type WeatherReport = String
+
+-- | Domain logic. Usually some pure code that might use mtl, free monads, etc.
+createWeatherReport :: WeatherProvider.WeatherData -> WeatherReport
+createWeatherReport (WeatherProvider.WeatherData temp) =
+  "The current temperature in London is " ++ (show temp)
+
+-- | Domain logic that uses external dependency to get data and process it.
+getCurrentWeatherReportInLondon :: IO WeatherReport
+getCurrentWeatherReportInLondon = do
+  weatherData <- WeatherProvider.getWeatherData "London" "now"
+  return $ createWeatherReport weatherData
diff --git a/simple/simple.cabal b/simple/simple.cabal
new file mode 100644 (file)
index 0000000..3e84dad
--- /dev/null
@@ -0,0 +1,21 @@
+cabal-version:       >=2
+name:                simple
+version:             0.1.0.0
+license-file:        LICENSE
+author:              Evgenii Akentev
+maintainer:          i@ak3n.com
+build-type:          Simple
+extra-source-files:  CHANGELOG.md
+
+library domain
+  hs-source-dirs: domain
+  exposed-modules: WeatherProvider
+                 , WeatherReporter
+  default-language: Haskell2010
+  build-depends:    base
+
+executable main
+  main-is:             Main.hs
+  build-depends:       base >=4.13 && <4.14
+                     , domain
+  default-language:    Haskell2010