Skip to content

Instantly share code, notes, and snippets.

@dogwith1eye
Last active March 22, 2017 16:23
Show Gist options
  • Save dogwith1eye/5b3eaed4a14bc21787948f031f1deb54 to your computer and use it in GitHub Desktop.
Save dogwith1eye/5b3eaed4a14bc21787948f031f1deb54 to your computer and use it in GitHub Desktop.
An API Implementing the Reader Pattern
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