Skip to content

Instantly share code, notes, and snippets.

// https://twitter.com/__josejuan__/status/445564129521434624
// dummy definitions:
type Variable() = class end
type Expression() = class end
type Term() = class end
// overloads:
type E =
static member Sum(x: Variable list) = 0
// broken fmap for option
let fmap f =
function
| None -> None
| Some a ->
let b = f a
if box b = null then None else Some b
// second functor law, see http://www.haskell.org/haskellwiki/Functor
let fmapL p q = fmap (p << q)
using System;
using System.Collections.Generic;
using System.Linq;
static class Extensions {
public static Func<A, IEnumerable<C>> Kleisli<A,B,C>(this Func<A, IEnumerable<B>> f, Func<B, IEnumerable<C>> g) {
return a => from b in f(a) from c in g(b) select c;
}
}
calculate :: String -> IO Int
calculate a = return 0 -- dummy placeholder for implementation
update :: String -> IO ()
update a = print ("update " ++ a)
foo :: IO ()
foo = print "foo!"
main = do
class MyOld5700LoCBusinessClass { // actually 5715 lines
bool flag1, flag2, ..., flag15;
string message1, message2, ... message15;
// more instance variables
void Method1() {...}
...
void Method30() {...}
type Address = {
Name: string
Address1: string option
Address2: string option
Code: string option
City: string option
Country: string option
}
type Client = {
// see http://bugsquash.blogspot.com/2014/05/mapping-objects-to-json-with-fleece.html for some background
open System
type AccountIdentifier = AccountIdentifier of Guid
type Account = {
Identifier : AccountIdentifier
Name : string
}
using System;
using Newtonsoft.Json;
class Foo {
public override string ToString() {
return "Foo";
}
}
class Bar<T> where T : Foo {
open System
type StringToExnClass = StringToExnClass with
static member StringToExn (_: ArgumentNullException) = fun (s: string) -> ArgumentNullException s
static member StringToExn (_: AccessViolationException) = fun (s: string) -> AccessViolationException s
static member StringToExn (_: ArrayTypeMismatchException) = fun (s: string) -> ArrayTypeMismatchException s
// etc (need to wrap all relevant types)
let inline iStringToExn (a: ^a, b: ^b) =
((^a or ^b) : (static member StringToExn: ^b -> (string -> ^b)) b)
type Either<'a, 'b, 't> = ('a -> 't) -> ('b -> 't) -> 't // equivalent to Choice<'a, 'b>
type EitherStringInt<'t> = (string -> 't) -> (int -> 't) -> 't // equivalent to Choice<string, int>