Skip to content

Instantly share code, notes, and snippets.


Dmitry Morozov dmitry-a-morozov

View GitHub Profile
View kthLargestBST.fs
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

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 / FileReaderProvider.fs
Last active Aug 3, 2016
type provider in 20 lines
View FileReaderProvider.fs
namespace FSharp.IO.DesignTime
open System.Reflection
open System.IO
open Microsoft.FSharp.Core.CompilerServices
open ProviderImplementation.ProvidedTypes
dmitry-a-morozov / scoped_action.fs
Last active Apr 5, 2016
Scoped Action view via IDisposable object expression
View scoped_action.fs
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 / singleton.fs
Created Apr 4, 2016
thread safe singleton defined as F# record
View singleton.fs
type Configuration = {
Database: string
RetryCount: int
module Configuration =
let private singleton = ref { Database = "(local)"; RetryCount = 3 }
dmitry-a-morozov / *.fs
Created Mar 2, 2016
Descriptive assertions using new F# 4.0 feature
View *.fs
open FSharp.Quotations
open System.Diagnostics
open System
type Debug with
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?
View du.ts
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 / gist:4f41882c8e96876c6252
Created Aug 28, 2015
Specifying a Parameter Default Value
View gist:4f41882c8e96876c6252
CREATE PROCEDURE [dbo].[spGetAutomobiles]
@Id int = 0,
@Make nvarchar(100) = ''
FROM Automobiles a with (nolock)
WHERE (@Id = 0 OR a.Id = @Id)
AND (@Make = '' OR a.Make = @Make)
View gist:5d7c09d6c98ccd8019e0
[ DateTime.Today.AddDays(-5.0) .. TimeSpan.FromDays(1.0) .. DateTime.Today ]
dmitry-a-morozov / gist:874d65a88d6bee6abaa7
Last active Aug 29, 2015
object expression with interface chain
View gist:874d65a88d6bee6abaa7
type IOOP = abstract Mutate: unit -> unit
type IFunctional<'T> = abstract Reduce: 'T list * ('T -> 'T -> 'T) -> 'T
type ProgramThatMatters =
static member Run<'T, 'a when 'a :> IOOP and 'a :> IFunctional<'T>>(paradigm: 'a) = ()
let myCode = {
new IFunctional<int> with
member __.Reduce(xs, f) = List.reduce f xs
interface IOOP with