SHARE
TWEET

Untitled

a guest Aug 13th, 2016 96 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. open System
  2.  
  3. [<AutoOpen>]
  4. module LoggerModule =
  5.  
  6.     /// A fake logger type.
  7.     type Logger =
  8.         private { __ : unit }
  9.        
  10.     [<RequireQualifiedAccess; CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
  11.     module Logger =
  12.         let info (_ : string) (_ : Logger) = ()
  13.         let warn (_ : string) (_ : Logger) = ()
  14.         let error (_ : string) (_ : Logger) = ()
  15.         let make () = { __ = () }
  16.  
  17. [<AutoOpen>]
  18. module DatabaseConnectionModule =
  19.  
  20.     /// A fake database connection type.
  21.     type DatabaseConnection =
  22.         private { __ : unit }
  23.        
  24.     [<RequireQualifiedAccess; CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
  25.     module DatabaseConnection =
  26.         let getName (_ : DatabaseConnection) = "MyDatabase"
  27.         let getValue (_ : DatabaseConnection) = 10
  28.         let make (_ : string) = { __ = () }
  29.  
  30. [<AutoOpen>]
  31. module FrobServiceModule =
  32.  
  33.     /// A service that can frobnicate at the given sanitation capacity and finalization rate.
  34.     type FrobService =
  35.         private
  36.             { SanitationCapacity : int
  37.               FinalizationRate : int
  38.               Logger : Logger
  39.               Database : DatabaseConnection }
  40.    
  41.     [<RequireQualifiedAccess; CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
  42.     module FrobService =
  43.    
  44.         let private queryFrobData frobService =
  45.             DatabaseConnection.getValue frobService.Database
  46.        
  47.         let private trySanitizeFrobData frobData frobservice =
  48.             Some <| max frobData frobservice.SanitationCapacity
  49.        
  50.         let private finalizeFrobData frobData frobService =
  51.             frobData * frobService.FinalizationRate
  52.        
  53.         /// Attempt to frobnicate.
  54.         let tryFrobnicate frobService =
  55.        
  56.             // query frob data
  57.             Logger.info ("Querying frob data from source " + DatabaseConnection.getName frobService.Database) frobService.Logger
  58.             let frobData = queryFrobData frobService
  59.            
  60.             // attempt to sanitize frob data
  61.             match trySanitizeFrobData frobData frobService with
  62.             | Some frobData ->
  63.            
  64.                 // finalize frob data
  65.                 let frobData = finalizeFrobData frobData frobService
  66.                 Some frobData
  67.            
  68.             // warn of failure!
  69.             | None ->
  70.                 Logger.warn ("Failed to sanitize frob data " + string frobData) frobService.Logger
  71.                 None
  72.            
  73.         /// Make a frob service.
  74.         let make sanitationCapacity finalizationRate logger database =
  75.             { SanitationCapacity = sanitationCapacity; FinalizationRate = finalizationRate; Logger = logger; Database = database }
  76.  
  77. /// Program entry point.
  78. let [<EntryPoint>] main _ =
  79.  
  80.     // build frob service
  81.     let logger = Logger.make ()
  82.     let database = DatabaseConnection.make "DB=MyDatabase;User=Me;Password=12345"
  83.     let frobService = FrobService.make 20 60 logger database
  84.    
  85.     // attempt to frobnicate
  86.     match FrobService.tryFrobnicate frobService with
  87.     | Some frobData -> printfn "Frobnication result: %i frobs." frobData
  88.     | None -> Logger.error "Frobnication failure!" logger
  89.    
  90.     // return an integer exit code
  91.     0
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top