Skip to content

Instantly share code, notes, and snippets.

View dmitry-a-morozov's full-sized avatar

Dmitry Morozov dmitry-a-morozov

View GitHub Profile
@dmitry-a-morozov
dmitry-a-morozov / singleton.fs
Created April 4, 2016 23:26
thread safe singleton defined as F# record
type Configuration = {
Database: string
RetryCount: int
}
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
[<AutoOpen>]
module Configuration =
let private singleton = ref { Database = "(local)"; RetryCount = 3 }

Assert On Steroids

I find it useful to have assert statements in my code. It has several benefits:

  • Self documents code expectations
  • Establishes boundary checks between component
  • Helps to pinpoint problems by failing fast, especially when you just started dealing with a new library
  • Here is a good summary on using assertions.

In a dream world all this can be addressed by sophisticated type system but we all know it’s not going to happen any time soon.

@dmitry-a-morozov
dmitry-a-morozov / kthLargestBST.fs
Last active February 20, 2019 18:29
Kth largest in BST
type Node<'T> =
| Node of value: 'T * left: Node<'T> * right: Node<'T>
| Empty
let getKthLargest(root, n) =
let rec loop = function
| Empty -> Seq.empty
| Node(value, left, right) ->
seq {
yield! loop right
@dmitry-a-morozov
dmitry-a-morozov / FileReaderProvider.fs
Last active August 3, 2016 21:29
type provider in 20 lines
namespace FSharp.IO.DesignTime
open System.Reflection
open System.IO
open Microsoft.FSharp.Core.CompilerServices
open ProviderImplementation.ProvidedTypes
[<assembly:TypeProviderAssembly()>]
do()
@dmitry-a-morozov
dmitry-a-morozov / scoped_action.fs
Last active April 5, 2016 00:29
Scoped Action view via IDisposable object expression
open System
open System.Data
open System.Data.SqlClient
type SqlConnection with
//1. Open/close connection if it was not opened
//2. address an issue when regular Dispose on SqlConnection wipes out all properties like ConnectionString in addition to closing connection to db
member this.UseLocally() =
@dmitry-a-morozov
dmitry-a-morozov / *.fs
Created March 2, 2016 23:46
Descriptive assertions using new F# 4.0 feature
open FSharp.Quotations
open System.Diagnostics
open System
type Debug with
[<Conditional("DEBUG")>]
static member AssertThat([<ReflectedDefinition(true)>] condition: Expr<bool>) =
match condition with
| Patterns.WithValue(value, t, expr) when t = typeof<bool> ->
let message = string expr //how do I get unparsed F# here?
class C1 { item: string }
class C2 { item: string[] }
class C3 { item: string }
type C1_C2_C3 = C1 | C2 | C3;
function Foo(x: C1_C2_C3): string {
if (x instanceof C1) return x.item;
else if (x instanceof C2) return x.item[0];
else if (x instanceof C3) return x.item;
@dmitry-a-morozov
dmitry-a-morozov / gist:7994192
Created December 16, 2013 20:52
WinForms + Async
open System
open System.Net
open Microsoft.FSharp.Control.WebExtensions
open System.Windows.Forms
open System.Threading
let form = new Form()
let text = new Label()
let button = new Button()
@dmitry-a-morozov
dmitry-a-morozov / gist:7679567
Created November 27, 2013 17:22
Why out ref will be an issue
namespace Test
type MyType =
static member AsyncWithRefParams(i : int byref) =
async {
i <- 5
}
module Program =
[<EntryPoint>]
@dmitry-a-morozov
dmitry-a-morozov / gist:6701202
Last active December 23, 2015 22:09
Using Single-Case Active Patterns in real application. Parsing CSV files with possible formatting errors and bogus data. Pay attention to implementation of (|OrderStatusLine|_|), (|AccountPnlLine|_|) and (|PositionPnlLine|_|).
module Parse =
let inline tryParse<'T when ^T : (static member TryParse: string * ^T byref -> bool)> s =
let mutable x = Unchecked.defaultof<_>
if (^T: (static member TryParse: string * ^T byref -> bool) (s, &x)) then Some x else None
let (|Int|_|) = tryParse<int>
let (|Int64|_|) = tryParse<int64>
let (|Decimal|_|) = tryParse<decimal>
let (|DateTime|_|) = tryParse<DateTime>