Skip to content

Instantly share code, notes, and snippets.

public class Nav {
public static HelperResult NavAndPrint(object obj, string path) {
var printer = Nav.GetPrinter().SetValue(obj);
return printer.NavigateTo(path).TryToShowObject();
}
private static UiContext GetPrinter() {
return new UiContext(
displayRules: new Func<UiContext, Maybe<HelperResult>>[] {
@sgoguen
sgoguen / merkle-tree.fsx
Created February 16, 2017 21:30
Merkle Trees
open System.IO
open System.Collections.Concurrent
// A Merkle tree for a file system might look like this
// PASTE!!
let isDirectory(path:string) =
let attr = File.GetAttributes(path)
attr.HasFlag(FileAttributes.Directory)
@sgoguen
sgoguen / memoize-fib.fsx
Last active February 16, 2017 20:02
Memoize ala Ivanek
// Today's example is courtesy of @jindraivanek
// https://gist.github.com/jindraivanek/5ff029577d2b544b2cd739d994750a18#file-memoizerec-fsx
// As you remember, there was a warning on line 14
// For a good explaination, see @tomaspetricek's Stack Overflow post.
// http://stackoverflow.com/questions/8636630/recursive-objects-in-f
@sgoguen
sgoguen / triggered-task.cs
Last active February 16, 2017 16:52
A lightweight data structure for creating tasks that are completed elsewhere
// A poor reimplementation of IAsyncResult. It was a fun exercise though
public interface ITaskTrigger<T> {
void SetComplete(T value);
void ThrowException(Exception ex);
}
public class AsyncRequest<T, U> : ITaskTrigger<U> {
public readonly T Request;
public readonly ITaskTrigger<U> Trigger;
void Main() {
var doWork = CreatePipeLineFunc(async (int x) => {
var delay = ((x % 3) + 1) * 1000;
await Task.Delay(delay);
return new { x, delay };
}, maxDegreeOfParallelism: 10);
Enumerable.Range(1, 100).Select(m => doWork(m)).Dump();
}
void Main() {
string line;
int runningTotal = 0, input = 0;
do {
line = Console.ReadLine();
if (Int32.TryParse(line, out input)) {
runningTotal += input;
Console.WriteLine(runningTotal);
} else if (line == "quit" || line == null) {
@sgoguen
sgoguen / fluent-mongo-query.fs
Last active August 29, 2015 14:16
Quick Fluent Interface to wrap Mongo queries in an order safe interface
open System
open System.Linq
open System.Collections.Generic
open System.Linq.Expressions
type SortOrder = Asc | Desc
type MongoQuery<'a> private(queryable:IQueryable<'a>, filters, sortFn, takeQty, skipQty) as this =
new(query) = MongoQuery(query, [], None, None, None)
member this.Where(filter:Expression<Func<'a,bool>>) =
@sgoguen
sgoguen / gist:2815935
Created May 27, 2012 21:17
LINQ to Tree's - Written in F#
namespace NextWeb.Collections
open System
open System.Runtime.CompilerServices
type ITree<'a> =
abstract member Value: 'a
abstract member Children: seq<ITree<'a>>
[<Extension>]
@sgoguen
sgoguen / gist:1058974
Created July 1, 2011 17:09
EF CodeFirst - Generating a Database in F#
// This will generate a database, but EF will not materialize the objects
// because our types do not have default parameterless constructors
namespace DataModel
open System
open System.ComponentModel.DataAnnotations
open System.Data.Entity
type User = { mutable ID : int; mutable Name: string; mutable DB: DateTime; mutable Tasks: Task[];
mutable Projects: Project[] }
Option Strict On
Imports Allied
Imports BaseTest.TestExtensions
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports System.Xml.Linq
Imports System.Collections.Generic
<TestClass()> _
Public Class MParserTest