Skip to content

Instantly share code, notes, and snippets.

@eulerfx
eulerfx / HListFoldish.fs
Last active August 29, 2015 14:14
F# HList unsafe fold
// for http://www.fssnip.net/d2
type Fold<'b> = Fold of f:('b -> obj -> 'b) * z:'b
with
static member inline ($) (Fold(_,z), HNil) = z
static member inline ($) (Fold(f,z), HCons(a,xs)) = f (Fold(f,z) |*| xs) a
// use
@eulerfx
eulerfx / AsyncEvent.fs
Created February 6, 2015 14:55
AsyncEvent (an F# async based Rx)
/// An async observer.
type AsyncObserver<'a> = 'a option -> Async<unit>
module AsyncObserver =
let inline post a (obs:AsyncObserver<'a>) = obs (Some a)
let inline stop (obs:AsyncObserver<'a>) = obs None
let contramap (f:'b -> 'a) (o:AsyncObserver<'a>) : AsyncObserver<'b> =
@eulerfx
eulerfx / gist:2087870
Created March 19, 2012 00:46
Atom document
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Example Feed</title>
<subtitle>A subtitle.</subtitle>
<link href="http://example.org/feed/" rel="self" />
<id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id>
<updated>2003-12-13T18:30:02Z</updated>
<author>
<name>John Doe</name>
<email>johndoe@example.com</email>
@eulerfx
eulerfx / FuncRetryHelper.cs
Created March 22, 2012 18:50
Function retry logic
public static Func<T> Retry(Func<T> original, int retryCount)
{
return () =>
{
while (true)
{
try
{
return original();
}
@eulerfx
eulerfx / IOrderRepository.cs
Created April 25, 2012 04:32
IOrderRepository
public interface IOrderRepository
{
Order Get(string Id);
IList<Order> GetMostRecent(string customerId, int max = 20);
void Add(Order order);
void Remove(string Id);
}
@eulerfx
eulerfx / Order.cs
Created April 25, 2012 04:24
Order Model
public class Order
{
public string Id { get; private set; }
public string Number { get; private set; }
public string CustomerId { get; private set; }
public DateTime Date { get; private set; }
public ICollection<OrderLineItem> Items { get; private set; }
public decimal ShippingCharge { get; private set; }
public decimal TaxCharge { get; private set; }
public decimal Total
@eulerfx
eulerfx / OrderReadModel.cs
Created April 26, 2012 03:57
OrderReadModel
public OrderReadModel
{
public string Id { get; private set; }
public string OrderNumber { get; private set; }
public DateTime Date { get; private set; }
public decimal Total { get; private set; }
}
@eulerfx
eulerfx / IOrderRepository.GetMostRecent.sql
Created April 26, 2012 03:54
IOrderRepository.GetMostRecent SQL
select top(@max)
Id,
OrderNumber,
Date,
Total = o.TaxCharge + o.ShippingCharge + (select sum(i.Amount * i.Quantity) from OrderLineItems i where i.OrderId = o.Id)
from Orders o
where o.CustomerId = @customerId
order by o.Date desc
@eulerfx
eulerfx / UserProfile_ValidationDeferred.cs
Created May 20, 2012 01:14
Not always-valid entity
public class UserProfile
{
public string Name { get; set; }
// implementation omitted
public bool IsValid { get; }
}
var profile = new UserProfile();
// must query to ascertain validity
if (!profile.IsValid)
@eulerfx
eulerfx / UserProfile_AlwaysValid.cs
Created May 20, 2012 01:11
Always valid user profile
public class UserProfile
{
public UserProfile(string name)
{
this.Name = name;
}
public string Name
{
get { return _name; }