Skip to content

Instantly share code, notes, and snippets.

@panesofglass
panesofglass / README.md
Last active March 7, 2018 21:54
Use cases for .NET Interface Extensions

Static Methods on Interfaces

Use Cases

  1. Define related sets or sequences of algorithms to be defined together (need explicit example)
  2. Specify required static extension methods for implementing things like LINQ or Rx.
  3. Transducers cannot currently be implemented efficiently in .NET. Transducers would need to use IEnumerable<_> as a base collection type rather than potentially more efficient implementations (or retaining the input's stype, e.g. List<_>, etc).
  4. F# modules such as List, Array, and Seq all provide functions that are the same in nature but differ by wrapper type.
@panesofglass
panesofglass / inference.fsx
Last active August 25, 2017 01:43
inference and builders
type Op =
| Add
| Multiply
static member TryParse input =
match input with
| "add" -> Some Add
| "multiply" -> Some Multiply
| _ -> None
type Command<'a> =
@panesofglass
panesofglass / apply.fsx
Last active August 21, 2017 22:34
F# async applicative using a custom operation
open System
type FSharp.Control.AsyncBuilder with
[<CustomOperation("and!", IsLikeZip=true)>]
member __.Merge(x, y, f) =
async {
let! token = Async.CancellationToken
let! x' = Async.StartChildAsTask x
let! y' = Async.StartChildAsTask y
do System.Threading.Tasks.Task.WaitAll([|x';y'|], cancellationToken = token)
@panesofglass
panesofglass / Test.fsx
Created August 10, 2017 16:10
TPL + single SqlConnection
#load "load-references-debug.fsx"
open System.Data.SqlClient
open FSharp.Data
[<Literal>]
let MasterCS = "Server=.;Database=master;Integrated Security=true;"
let createDB () =
use cmd = new SqlCommandProvider<"create database Test", MasterCS>(MasterCS)
cmd.Execute() |> ignore
@panesofglass
panesofglass / fizzbuzz.fsx
Last active August 8, 2017 04:37
Iron Yard FizzBuzz
// type for fizzbuzz results
type FizzBuzzResult =
| FizzBuzz
| Fizz
| Buzz
| Number of int
let fizzbuzz n =
match n % 3, n % 5 with
| 0, 0 -> FizzBuzz
@panesofglass
panesofglass / Specification.cs
Created October 26, 2009 16:16
A generic specification base class.
using System;
using System.Linq.Expressions;
namespace Foundation.Specifications
{
/// <summary>
/// Defines an abstract class from which to create conditional specifications.
/// </summary>
/// <typeparam name="T">The type of entity used in the specification.</typeparam>
/// <seealso href="http://ubik.com.au/article/named/implementing_the_specification_pattern_with_linq" />
@panesofglass
panesofglass / Test.fsx
Created January 14, 2017 21:44
Define generic delegate type with F#
> defineGenericDelegate [| typeof<Collections.Generic.IDictionary<string, obj>>; typeof<Threading.Tasks.Task> |];;
val it : Type =
System.Func`2[System.Collections.Generic.IDictionary`2[System.String,System.Object],System.Thread
ing.Tasks.Task]
using System;
using System.Linq;
using System.Reactive.Linq;
using System.Windows.Forms;
using DictionarySuggest.DictionarySuggestService;
using System.Reactive;
namespace DictionarySuggest
{
class Program
namespace WebSocket
// Appache 2.0 license
// References:
// [1] Proposed WebSockets Spec December 2011 http://tools.ietf.org/html/rfc6455
// [2] John McCutchan (Google Dart Team Member) http://www.altdevblogaday.com/2012/01/23/writing-your-own-websocket-server/
// [3] A pretty good Python implemenation by mrrrgn https://github.com/mrrrgn/websocket-data-frame-encoder-decoder/blob/master/frame.py
// [4] WebSockets Organising body http://www.websocket.org/echo.html
// [5] AndrewNewcomb's Gist (starting point) https://gist.github.com/AndrewNewcomb/711664
@panesofglass
panesofglass / Formatting.fs
Created February 28, 2016 21:01
Server MVC OWIN Formatting
open Newtonsoft.Json
let serializerSettings = JsonSerializerSettings(ContractResolver = Serialization.CamelCasePropertyNamesContractResolver())
serializerSettings.Converters.Add(OptionConverter())
let serialize data =
JsonConvert.SerializeObject(data, serializerSettings)
|> Text.Encoding.UTF8.GetBytes
let deserialize<'T> (stream: Stream) =