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 / 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

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 / 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 / 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 }
@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:4f41882c8e96876c6252
Created August 28, 2015 17:13
Specifying a Parameter Default Value
CREATE PROCEDURE [dbo].[spGetAutomobiles]
@Id int = 0,
@Make nvarchar(100) = ''
AS
BEGIN
SELECT *
FROM Automobiles a with (nolock)
WHERE (@Id = 0 OR a.Id = @Id)
AND (@Make = '' OR a.Make = @Make)
END
[ DateTime.Today.AddDays(-5.0) .. TimeSpan.FromDays(1.0) .. DateTime.Today ]
@dmitry-a-morozov
dmitry-a-morozov / gist:874d65a88d6bee6abaa7
Last active August 29, 2015 14:24
object expression with interface chain
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