Skip to content

Instantly share code, notes, and snippets.

@blair55
blair55 / post-writer.fs
Last active December 4, 2022 21:49
Logging example after applying the writer monad
let o x = x :> obj
let getSuperUserData super =
writer {
let sw = Stopwatch.StartNew()
let service = getService ()
let result = service.getSuperUserDetails super
do! write ("serviceElapsed", o sw.ElapsedMilliseconds)
match result with
@blair55
blair55 / TrivialWriterExample.fs
Created November 29, 2022 13:48
Trivial writer example
type Log = string * int
let f: Writer<Log, bool> =
writer {
let x = 1
do! write ("x", x)
let y = 2
do! write ("y", y)
let z = x + y
do! write ("result", z)
@blair55
blair55 / WriterBuilder.cs
Created November 29, 2022 13:44
Writer computation expression
type WriterBuilder() =
member __.Return(x) = retn x
member __.Bind(m, f) =
let (a, logs1) = run m
let (b, logs2) = run (f a)
Writer(fun () -> b, logs1 @ logs2)
let writer = WriterBuilder()
@blair55
blair55 / Writer.fs
Created November 29, 2022 13:18
Writer type and utility functions
type Writer<'Log, 'T> = Writer of (Unit -> 'T * 'Log list)
let retn (x: 'T) = Writer(fun () -> x, [])
let write (log: 'Log) = Writer(fun () -> (), [ log ])
let run (Writer writer) : 'T * 'Log list = writer ()
@blair55
blair55 / pre-writer.fs
Created November 29, 2022 12:55
Logging example before applying writer
let getSuperUserData (super, log: Logger) =
let sw = Stopwatch.StartNew()
let service = getService ()
let result = service.getSuperUserDetails super
log.info ({| serviceElapsed = sw.ElapsedMilliseconds |})
match result with
| Ok res ->
log.info ({| serviceResult = "success" |})
@blair55
blair55 / AsyncWriterResult.fsx
Last active August 5, 2019 16:52
Combining Monads
module Async =
let retn x = async {
return x }
let bind f m = async {
let! x = m
return! f x }
@blair55
blair55 / paket.dependencies
Last active February 6, 2018 16:44
Slow paket install
source https://www.nuget.org/api/v2
nuget Microsoft.AspNetCore
nuget Microsoft.AspNetCore.Mvc
nuget Microsoft.AspNetCore.StaticFiles
nuget Microsoft.NET.Test.Sdk
nuget Moq
nuget Newtonsoft.Json
nuget PetaPoco.NetCore
nuget Serilog.AspNetCore
@blair55
blair55 / Microsoft.PowerShell_profile.ps1
Created August 3, 2015 18:25
Clear ReSharper (8.2) Solution Cache for a repo from the command line
function Get-CurrentRepoName(){
$repoPath = git rev-parse --show-toplevel
$repoName = basename $repoPath
return $repoName
}
function ClearReSharperSolutionCache(){
$repoName = Get-CurrentRepoName
$rootPath = $env:localappdata + "\JetBrains\ReSharper\v8.2\SolutionCaches\"
$folders = get-childitem $rootPath
@blair55
blair55 / 1 - InfrastructureRegistry.cs
Last active August 29, 2015 13:58
ServiceStack.Redis and StructureMap
using System;
using System.Configuration;
using ServiceStack.Redis;
using StructureMap;
using StructureMap.Configuration.DSL;
namespace Example.IoC
{
public class InfrastructureRegistry : Registry
{
@blair55
blair55 / Deployment.ps1
Created January 28, 2014 00:09
Decoupling your Web Deploy Build Script from Team City with Powershell
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
$targets = $env:TargetDeployUrls -split ","
foreach ($t in $targets)
{