This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |