Skip to content

Instantly share code, notes, and snippets.

@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>>) =
// Copy and Paste this code into LINQPad and use the Nutshell database
void Main()
{
var text = @"$Purchase.Customer.Name purchased $Detail on $Purchase.Date for $$$Purchase.Price";
var Hello = new ExpressionTemplate<PurchaseItem>(text);
var query = PurchaseItems.Select(Hello.ToExpr());
query.ToString().Dump("SQL");
/// <summary>
/// This snippet extends Matthias Hertel's Difference algorithm and extends it to a
/// LINQ provider proof-of-concept. To run, simply copy+paste into LINQPad.
///
/// http://www.mathertel.de/Diff/
///
/// Copyright (c) by Matthias Hertel, http://www.mathertel.de
/// This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
///
/// </summary>
void Main()
{
var r = new Random().Values(0, 10).Let(list => list);
var l1 = r.Take(10);
var l2 = r.Take(10);
l1.SequenceEqual(l2).Dump("Should be equal");
var l3 = r.Take(20);
l3.Take(10).SequenceEqual(l1).Dump("Should start with same sequence");
var l4 = r.Take(20);
Public MustInherit Class LazyXMLBase
Implements IEnumerable(Of XNode)
Public MustOverride Function Render() As XNode
Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of System.Xml.Linq.XNode) Implements System.Collections.Generic.IEnumerable(Of System.Xml.Linq.XNode).GetEnumerator
Return Enumerable.Repeat(Me.Render(), 1).GetEnumerator()
End Function
Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
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
@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>]
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) {
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();
}
@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;