Skip to content

Instantly share code, notes, and snippets.

@dogwith1eye
dogwith1eye / QueryPatternStack.cs
Created August 15, 2017 13:20
QueryPatternStack.cs
using System;
using QueryPattern.Test1;
using QueryPattern.Test2;
using LaYumba.Functional;
namespace QueryPattern
{
using System.Threading.Tasks;
using static Console;
using static F;
@dogwith1eye
dogwith1eye / QueryPattern.cs
Created August 14, 2017 19:14
Ambiguous call to SelectMany
using System;
using System.Threading.Tasks;
using LaYumba.Functional;
namespace QueryPattern
{
using static Console;
using static F;
class Program
{
@dogwith1eye
dogwith1eye / Functional.cs
Last active March 23, 2017 14:22
Functional Design
// IMPORTANT! Learn this when teaching yourself f#
// BAD! Encourages a non functional request/response design
class MyParametricApi
{
public MyParametricApi(IDep1 dep1, IDep2 dep2, IDep3 dep3) {}
public string Function1(int arg1) { ... }
public string Function2(string arg2) { ... }
}
@dogwith1eye
dogwith1eye / MyApiExample.fs
Last active March 22, 2017 16:37
Using MyApi
let context =
{ Dep1 = CON ".\SqlExpress";
Dep2 = ENV "DEP_2";
Dep3 = RND (System.Random().Next()); }
let app2 = (function2 "test"
>>=. function1
>>- function4
>>= (function3 >=> function2))
@dogwith1eye
dogwith1eye / Reader.fs
Created March 22, 2017 16:23
Reader with additional combinators
type Reader<'x> = Reader of 'x
module Reader =
let pure x = Reader x
let run xR ctx =
match xR with
| Reader x -> x ctx
@dogwith1eye
dogwith1eye / MyApiReader.fs
Last active March 22, 2017 16:23
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>
@dogwith1eye
dogwith1eye / ReaderTest.fs
Created March 22, 2017 15:53
Test the Reader Pattern
let config = "world"
let greeter =
pure (fun config -> "hello" + config + "!")
let calc (s:string) =
pure (fun (config:string) -> s.Length - config.Length)
let app1 = (greeter >>= calc)
let r1 = run app1 config
@dogwith1eye
dogwith1eye / Reader.fs
Last active March 22, 2017 15:46
Reader Pattern
type Reader<'x> = Reader of 'x
module Reader =
let pure x = Reader x
let run xR ctx =
match xR with
| Reader x -> x ctx
@dogwith1eye
dogwith1eye / ValueParametricModule.fs
Created March 22, 2017 14:58
Classes as Value Parametric Modules
type MyParametricApi(dep1, dep2, dep3) =
let doStuffWith dep1 dep2 dep3 arg1 = printfn "stuff:%A" arg1
let doStuffWith' dep1 dep2 dep3 arg2 = printfn "'stuff:%A" arg2
member __.Function1 arg1 = doStuffWith dep1 dep2 dep3 arg1
member __.Function2 arg2 = doStuffWith' dep1 dep2 dep3 arg2
@dogwith1eye
dogwith1eye / ShowVisitor.cs
Last active March 19, 2017 17:09
Visit a type and returns a string representation of the type
interface IShowable<T>
{
T Value { get; }
U Accept<U>(IVisitor<T, U> visitor);
}
class ShowVisitor : IVisitor<string, string>,
IVisitor<int, string>,
IVisitor<float, string>
{