Skip to content

Instantly share code, notes, and snippets.

[<AutoOpen>]
module UoM =
// Units of measure for other than integral types
let inline (++) (w: ^W when ^W: (static member IsMeasureAbbrev: ^tm * ^t -> unit)) (t: ^t) = (# "" t: ^tm #)
let inline (--) (w: ^W when ^W: (static member IsMeasureAbbrev: ^tm * ^t -> unit)) (tm: ^tm) = (# "" tm: ^t #)
[<MeasureAnnotatedAbbreviation>]
type Guid<[<Measure>] 'm> = Guid
@cloudRoutine
cloudRoutine / 01_folds.fs
Last active February 25, 2025 10:07
F# Transducers - they work for the most part
open System.Collections.Generic
open Microsoft.FSharp.Collections
[<RequireQualifiedAccess>]
module Folds =
// These are the fast implementations we actually want to use
@cloudRoutine
cloudRoutine / Microsoft.PowerShell_profile.ps1
Last active December 8, 2024 03:19
Powershell Profile for use with Cmder
# If this script is throwing an error near a Unicode symbol try resaving the file as UTF-8 with BOM
$psmodules = ";~\Documents\WindowsPowerShell\Modules"
# sometimes the module paths has been fucked before posh loads, but that won't stop us
$env:PSModulePath = $env:PSModulePath + $psmodules
# Set the OutputEncoding to Unicode so that the λ renders properly
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
[<System.Runtime.CompilerServices.Extension>]
type ExtensionMethods() =
[<System.Runtime.CompilerServices.Extension>]
static member inline GetOption< ^a,'k,'v when 'a : (member TryGetValue : 'k * ('v byref) -> bool)>(this : ^a, key : 'k) =
let mutable v = Unchecked.defaultof<'v>
let scc = ( ^a : (member TryGetValue : 'k * ('v byref) -> bool) this, key, &v)
if scc then
Some v
else
None
@swlaschin
swlaschin / ConstrainedTypesExamples.fsx
Last active March 9, 2025 02:36
Examples of creating constrained types in F#
// General hints on defining types with constraints or invariants
//
// Just as in C#, use a private constructor
// and expose "factory" methods that enforce the constraints
//
// In F#, only classes can have private constructors with public members.
//
// If you want to use the record and DU types, the whole type becomes
// private, which means that you also need to provide:
// * a constructor function ("create").
@allykzam
allykzam / Errors.fs
Last active September 14, 2020 17:27
F# code for getting attribute data off discriminated union cases
type FatalError(message: string) =
inherit System.Attribute()
member __.Message = message
type Errors =
| [<FatalError("The value specified is currently not available")>] UnknownValue
| NotAnError
let PrintErrorMessage : Errors -> string =
fun err ->
@cloudRoutine
cloudRoutine / fscheck_doc_old.fsx
Created December 14, 2014 17:04
Old FsCheck Documentation (v 0.6)
(*===============================
|| ||
|| QuickStart ||
|| ||
===============================*)
// A simple example of a property definition is
let prop_RevRev xs = List.rev(List.rev xs) = xs
@AlexArchive
AlexArchive / ndc-london-recordings.txt
Last active August 29, 2015 14:10
Powershell script to parse NDC London VODs prematurely
Capability Red - Requirements at Scale by Liz Keogh
http://www.ndcvideos.com/#/app/video/2111
----
Beyond Rectangles in Web Design - CSS Shapes and CSS Masking by Razvan Caliman
http://www.ndcvideos.com/#/app/video/2121
----
Coding Culture by Sven Peters
http://www.ndcvideos.com/#/app/video/2131
----
The Ultimate Logging Architecture - You KNOW You Want It by Michele Leroux Bustamante
@cloudRoutine
cloudRoutine / ReactiveExample.fs
Last active September 22, 2015 08:32
Using FSharp.Control.Reactive to recognize keyboard input
#r "PresentationCore"
#r "PresentationFramework"
#r "WindowsBase"
#r "System.Xaml"
#I "../../packages/FSharp.Control.Reactive.2.3.1/lib/net40/"
#I "../../packages/Rx-Linq.2.2.5/lib/net45"
#I "../../packages/Rx-Core.2.2.5/lib/net45"
#I "../../packages/Rx-Interfaces.2.2.5/lib/net45"
#I "../../packages/Rx-PlatformServices.2.2.5/lib/net45"
#I "../../packages/Rx-Providers.2.2.5/lib/net45"
@cloudRoutine
cloudRoutine / RoughMetrics.fsx
Last active July 20, 2016 12:10
Since Visual Studio won't calculate code metrics for F# projects, this script will calculate some some rough stats.
open System
open System.IO
let find_files dir = Directory.GetFiles( dir, "*.fs?", SearchOption.AllDirectories )
let not_start (s:string) p = not <| s.StartsWith p
let has_type (s:string) = if s.Contains @"type" then 1 else 0
let has_module (s:string) = if s.Contains @"module" then 1 else 0
let has_binding (s:string) = if s.Contains @"let" ||
s.Contains @"member" then 1 else 0