Skip to content

Instantly share code, notes, and snippets.

// Based on http://www.nut-cracker.com.ar/index.php/overloading-in-f
type IVec<'T> =
abstract member Add : 'T -> 'T
abstract member Inv : unit -> 'T
type Vector2D = Vector2D of float * float with
interface IVec<Vector2D> with
member x.Add (Vector2D(b1, b2)) = match x with Vector2D(a1,a2) -> Vector2D(a1+b1, a2+b2)
member x.Inv () = match x with Vector2D(a1, a2) -> Vector2D(-a1, -a2)
@dtchepak
dtchepak / compose.swift
Last active August 29, 2015 14:02
Function compsition in swift (based on https://twitter.com/jckarter/status/473642680782573569)
operator infix • { associativity right }
@infix func •<A,B,C> (f : B->C, g : A->B) -> (A->C) {
return { (x : A) in f(g(x)) }
}
func inc(a : Int) -> Int { return a+1 }
func show(a : Int) -> String { return String(a) }
let f = show • inc • inc
-- http://comonad.com/reader/wp-content/uploads/2009/08/A-Parsing-Trifecta.pdf
--
import Prelude hiding (null)
import Data.ByteString
data Input = Chunk ByteString | EOF
data Iteratee a
= Return a Input
| Cont (Input -> Iteratee a)
@dtchepak
dtchepak / stateExample.hs
Last active August 29, 2015 14:01
Example of getting values out of a computation that are not explicitly passed to a function
import Control.Monad (foldM)
import Control.Applicative
-- Given an initial state return a value based on that state and the new state
newtype State s a = State { runState :: s -> (a, s) }
-- Get the value of the state
get :: State s s
get = State $ \s -> (s,s)
#!/bin/sh
# Attempts to build a cabal binary package and symlink it in ~/.cabal/bin/
# Don't do this for binaries that rely on other assets! (such as pandoc)
# USE AT YOUR OWN RISK!
set -e
ws="[^a-zA-Z0-9\-\.]"
if [ "$#" -ne 1 ]
then
public IEnumerable<string> GetSamples(string s) {
var re = @"\{%\s*(?<tag>\w+)\s*\%\}(?<contents>(?s:.*?))\{%\s*end\1\s*%\}";
return Regex.Matches(s, re)
.Cast<Match>()
.Where(m => m.Groups["tag"].Value == "sample")
.Select(m => m.Groups["contents"].Value.Trim());
}
@dtchepak
dtchepak / a.fs
Last active August 29, 2015 14:00
//Experiment, based on http://withouttheloop.com/articles/2014-04-18-fsharp-csharp-statemachines/
open System
type Particulars = { buyer: string; seller: string }
type Draft = private Draft of Particulars
type Approved = private Approved of (Particulars * DateTime)
type Historical = private Historical of (Particulars * DateTime)
let createDraft = Draft
Given:
pure ∷ a → f a
⊛ ∷ f (a → b) → f a → f b
(.) ∷ (b → c) → (a → b) → a → c
show:
pure (.) ⊛ u ⊛ v ⊛ = u ⊛ (v ⊛ w)
@dtchepak
dtchepak / Either.cs
Last active August 29, 2015 13:57
Quick sketch of Either type in C#
public class Either<TLeft, TRight>
{
private readonly bool isLeft;
private readonly TLeft left;
private readonly TRight right;
private Either(bool isLeft, TLeft a, TRight b)
{
this.isLeft = isLeft;
this.left = a;
public static class ReaderExtensions
{
public static Func<T, B> Select<T, A, B>(this Func<T, A> reader, Func<A, B> f)
{
return t => f(reader(t));
}
public static Func<T, B> SelectMany<T, A, B>(this Func<T, A> reader, Func<A, Func<T, B>> f)
{
return t =>f(reader(t))(t);