Skip to content

Instantly share code, notes, and snippets.

@panesofglass
panesofglass / FetchData.fs
Created April 9, 2018 15:29
FetchData component using Trail
namespace BlazorApp1.Pages
open System
open System.Collections.Generic
open System.Linq
open System.Threading.Tasks
open System.Net.Http
open Microsoft.AspNetCore.Blazor
open Microsoft.AspNetCore.Blazor.Components
open Microsoft.AspNetCore.Blazor.Layouts
@panesofglass
panesofglass / fetch-tags.js
Created April 7, 2018 04:49
Fetch all tag urls on stackoverflow.com
fetch ('https://stackoverflow.com/').then(response => response.text()).then(html => {
let fragment = document.createRange().createContextualFragment(html);
let content = fragment.querySelectorAll('[rel=tag]');
for (let el of content) {
console.log(el.getAttribute('href'));
}
}).catch(error => console.error(error));
@panesofglass
panesofglass / free.fs
Created April 3, 2018 22:06 — forked from johnazariah/free.fs
Free Monad with Trampoline Infrastructure and Computation Builder
// F<'a> is any type with member 'map' of type ('a -> 'b) -> F<'a> -> F<'b>
type F<'a> = QIL<'a>
and S<'a> = F<Q<'a>>
and Q<'a> =
private
| Step of Step<'a>
| Bind of IBind<'a>
with
static member lift (k : F<'a>) : Q<'a> = Step (Suspend (fun () -> S<_>.map (Yield >> Step) k))
@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 / 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]
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