Skip to content

Instantly share code, notes, and snippets.

-- 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 / 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
// 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)
module DaveSquared.Sample
open Xunit
let incr x = x+1
// Test using Unquote
open Swensen.Unquote
[<Fact>]
@dtchepak
dtchepak / Console.swift
Last active August 29, 2015 14:15
Part of a series of exercises for experimenting with `flatMap` in Swift. Paste into an XCode test project and start filling in the `<TODO />`s. Previous exercises covered the implementation and use of `map` and `flatMap` for `Optional`, `Array`, and `Result` / `Either`. The aim of this exercise is to experiment with using the pattern to combine …
import Foundation
import UIKit
import XCTest
// INTRODUCTION
// Console<T> is a type representing programs that can interact with the console (i.e. read from stdin, write to stdout),
// to produce a value of some type T.
//
// A Console<()> program is one that returns void ( a.k.a. Unit, written "()" ).
@dtchepak
dtchepak / replsession.hs
Created May 21, 2015 04:30
Reader to construct a function that reads values from a single value
-- Some function that takes a number and a string
ghci> let f a b = show (a+1) ++ " " ++ b
ghci> :t f
f :: (Show a, Num a) => a -> [Char] -> [Char]
-- Some type with a number of fields we want to read
ghci> data Option = Option { number :: Int, label :: String } deriving (Show, Eq)
ghci> let o = Option 1 "hi"
-- Read value of each field from Option and pass to `f`
public interface IFoo { void MyMethod(Action<int[]> callback); }
[Test]
public void TestCallback()
{
var sub = Substitute.For<IFoo>();
var callback = Substitute.For<Action<int[]>>();
var data = new[] { 1, 2, 3 };
sub.When(x => x.MyMethod(callback))
@dtchepak
dtchepak / gist:665388
Created November 6, 2010 12:47
Fixed undefined local variable or method `docs' for #<DocsToCode:0x358cb30> by closing over local vars
require 'spec'
class DocsToCode
def docs_in(path)
[]
end
def code_in(doc)
""
end
def extract(path)
@dtchepak
dtchepak / test.cs
Created May 9, 2011 12:27
Serializing and .NET mock frameworks
public interface IRoot { string Data { get; set; } }
[Serializable]
public abstract class Root : IRoot { public abstract string Data { get; set; } }
[Test]
public void FailingTest()
{
var root = CreateMock(); // <-- create a mock with your favourite mocking framework for Root or IRoot
var formatter = new BinaryFormatter();
@dtchepak
dtchepak / Form.cs
Created June 28, 2011 03:42
Observing combinations of events where another event does not occur
var mouseDown = Observable.FromEventPattern(firstButton, "MouseDown");
var mouseUp = Observable.FromEventPattern(firstButton, "MouseUp");
var mouseMove = Observable.FromEventPattern(firstButton, "MouseMove");
mouseDown.Subscribe(x => Debug.WriteLine(">>> mouse down"));
mouseUp.Subscribe(x => Debug.WriteLine(">>> mouse up"));
(from down in mouseDown
from up in mouseUp.TakeUntil(mouseMove)
select up)