daily pastebin goal
1%
SHARE
TWEET

Untitled

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