Last active
March 22, 2017 16:23
-
-
Save dogwith1eye/5b3eaed4a14bc21787948f031f1deb54 to your computer and use it in GitHub Desktop.
An API Implementing the Reader Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ConnectionString = CON of string | |
type EnvVariable = ENV of string | |
type RandomNum = RND of int | |
type MyApiContext = | |
{ Dep1 : ConnectionString ; | |
Dep2 : EnvVariable; | |
Dep3 : RandomNum } | |
type MyApiPart<'a> = Reader<MyApiContext -> 'a> | |
module MyApi = | |
open Reader | |
// MyApiPart<string> | |
let function1 = | |
let dostuff (CON conn) = | |
"cs:" + conn + "!" | |
pure (fun ctx -> dostuff ctx.Dep1) | |
// string -> MyApiPart<int> | |
let function2 (s:string) = | |
let dostuff (ENV env) (RND random) = | |
if random % 2 = 0 then Some random else None | |
pure (fun ctx -> match dostuff ctx.Dep2 ctx.Dep3 with | |
| Some random -> random | |
| None -> s.Length ) | |
// int -> MyApiPart<string> | |
let function3 (i: int) = | |
let dostuff (ENV env) = | |
"env:" + env + "i:" + (string i) + "!" | |
pure (fun ctx -> dostuff ctx.Dep2) | |
// string -> int | |
let function4 (s: string) = | |
let dostuff = s.Length | |
dostuff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment