Skip to content

Instantly share code, notes, and snippets.

(*
WHAT'S GOING ON HERE?!
Sometimes you don't care about a particular type, you're interested in one field only, let's say `EntityId`.
Instead of using interface (which isn't even possible if don't own a type),
we can do structural typing in F# using SRTP and Active Patterns.
Active patterns are not required for this, but they do make code much easier to use.
*)
// So we have 2 types with field `EntityId: string`:
@medigor
medigor / code-gen2.fs
Created September 17, 2019 09:02 — forked from Szer/code-gen2.fs
Generate type serialization tests because I'm lazy
#r @"C:\Users\ayrat.hudaygulov\.nuget\packages\fsharp.compiler.service\26.0.1\lib\net45\FSharp.Compiler.Service.dll"
open System.IO
open Microsoft.FSharp.Compiler.Ast
open Microsoft.FSharp.Compiler.SourceCodeServices
let parse text =
let fileName = "whatever.fs"
let checker = FSharpChecker.Create()
let opts =
type EventStream<'T> ()=
let evt=Channel.CreateUnbounded<'T>()
let writer = evt.Writer
let reader = evt.Reader
let mutable key = 0
let mutable subscriptions = Map.empty : Map<int, IObserver<'T>>
let thisLock = new obj()
let obs =
@medigor
medigor / stepfulBuilder.fs
Created April 30, 2020 15:16 — forked from Szer/stepfulBuilder.fs
Computational expression with state being passed through steps
type StepfulBuilder() =
member _.Zero() = ()
member _.Yield x = x
[<CustomOperation("toInt")>]
member inline _.ToInt(_, value) =
int value
[<CustomOperation("transform")>]
member _.Transform(x, f) =
@medigor
medigor / rant.md
Created August 14, 2020 19:30 — forked from dsyme/rant.md
@medigor
medigor / ConstrainedTypesExamples.fsx
Created October 2, 2020 12:47 — forked from swlaschin/ConstrainedTypesExamples.fsx
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").
using System;
using System.Threading.Tasks;
namespace System.Collections.Concurrent
{
public static class ConcurrentDictionaryExtensions
{
/// <summary>
/// Provides an alternative to <see cref="ConcurrentDictionary{TKey, TValue}.GetOrAdd(TKey, Func{TKey, TValue})"/> that disposes values that implement <see cref="IDisposable"/>.
/// </summary>