Skip to content

Instantly share code, notes, and snippets.

View Porges's full-sized avatar
🏠
Working from home

George Pollard Porges

🏠
Working from home
View GitHub Profile
@Porges
Porges / PrintF.hs
Last active August 29, 2015 14:00
Statically-checked printf using Template Haskell
{-# LANGUAGE DeriveDataTypeable #-}
module PrintF
where
import Data.Generics
import qualified Language.Haskell.TH as TH
import Language.Haskell.TH.Quote
data Format = FInt Format -- %d
// This is okay:
type Regex<'T> =
| Seq of Regex<'T> list
| Alt of Regex<'T> list
| Singleton of 'T
// This is okay:
type Regex<'T> =
private Seq of Regex<'T> list
| Alt of Regex<'T> list
@Porges
Porges / typedtaglessfinal.cs
Created June 17, 2014 21:16
Encoding open recursion in C#: not recommended
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TypedTaglessFinal
{
@Porges
Porges / configureawait.linq
Last active August 29, 2015 14:03
Open me in LINQPad
<Query Kind="Program">
<Namespace>System.Threading.Tasks</Namespace>
</Query>
void Main()
{
Client().Wait();
}
async Task Client()
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
import Data.Monoid
newtype On = On [(Int, String)] deriving Monoid
on n s = On [(n, s)]
messages (On x) = cycle $ map funcFor [1..maxM]
where
maxM = foldl1 lcm $ map fst x
{-# LANGUAGE FlexibleInstances, TypeSynonymInstances #-}
import Data.List
-- unchanged from blog post apart from signature
compile :: Compilable a => String -> a
compile [] = empty
compile ('?' :cs) = anyChar `append` compile cs
compile ('*' :cs) = whatever `append` compile cs
compile ('\\':c:cs) = char c `append` compile cs
public struct Result<T>(T value)
{
public static implicit operator Result<T>(T value) => new Result<T>(value);
public static implicit operator Result<T>(Result<Any> any) => default(Result<T>);
public static implicit operator Result<T>(Result<T, Any> value) => new Result<T>(value.Value);
public bool HasValue { get; } = true;
public T Value { get; } = value;
}
@Porges
Porges / FixedVector.hs
Last active August 29, 2015 14:06
First pass of statically-lengthed vectors
{-# LANGUAGE DataKinds, KindSignatures, TypeOperators, ScopedTypeVariables, TypeFamilies #-}
import Prelude hiding (length)
import qualified Data.Vector as V
import qualified GHC.TypeLits as T
import qualified Data.Proxy as P
newtype FVec a (n :: T.Nat) = FVec (V.Vector a) deriving (Show, Eq)
type a ^ n = FVec a n
@Porges
Porges / raise.cs
Last active August 29, 2015 14:07
Simple safe event raising
using System;
class Foo
{
event EventHandler DoAThing;
void WhyYouEvenGottaDoAThing()
{
DoAThing.Raise(this);
}
module F
module State =
type State<'s, 'r> =
// A stateful computation takes an initial state of type 's
// and returns a result of some type 'r and a new state of type 's.
private State of ('s -> 'r * 's)
// You can run a stateful computation by supplying the state value: