Skip to content

Instantly share code, notes, and snippets.

@dtchepak
dtchepak / gist:3049721
Created July 4, 2012 21:54
What is OO?

I've been having a look around at various definitions of OO. Here's some of the ideas google turned up:

@dtchepak
dtchepak / Node.cs
Created July 8, 2012 13:10
Monadic parser combinators in C#, attempting to parse some basic text markup
using System;
using System.Collections.Generic;
using System.Linq;
namespace MarkupParser
{
public abstract class Node
{
public static Parser<String> TextParser()
{
@dtchepak
dtchepak / gist:3087638
Created July 11, 2012 02:46
compose arbitrary number of functions (a->a)
-- Want :: [a -> a] -> a -> a
-- Emulates: an . … . a3 . a2 . a1
Prelude> let a = [reverse, (++ " world"), (++ "!")]
-- Given "olleh", it should:
-- * reverse it: "hello"
-- * append " world": "hello world"
-- * append "!": "hello world!"
@dtchepak
dtchepak / gist:3163428
Created July 23, 2012 12:39
SPJ on Category Theory and FP
"I say “surprising” because anything with as exotic a name as “monad” — derived from category theory, one of the most abstract branches of mathematics — is unlikely to be very useful to red-blooded programmers. But one of the joys of functional programming is the way in which apparently exotic theory can have a direct and practical application, and the monadic story is a good example."

Simon PEYTON JONES, Tackling the Awkward Squad

@dtchepak
dtchepak / gist:3226421
Created August 1, 2012 12:29
Simple reader monad
instance Functor ((->) t) where
--fmap :: (a -> b) -> (t -> a) -> (t -> b)
fmap = (.)
instance Monad ((->) t) where
return = const
--(>>=) :: (t -> a) -> (a -> t -> b) -> t -> b
f >>= g = \t -> let a = f t
in g a t
@dtchepak
dtchepak / lens.cs
Created August 3, 2012 07:04
Attempt at lens in c#
public class Lens<T, TValue>
{
public Func<T, TValue> Get { get; private set; }
public Func<TValue, T, T> Set { get; private set; }
public Lens(Func<T, TValue> get, Func<TValue, T, T> set)
{
Get = get;
Set = set;
}
@dtchepak
dtchepak / sample.cs
Created August 14, 2012 11:59
SAMPLE: Sub properties using reflection
// SAMPLE ONLY -- DON'T USE FOR REAL STUFF UNLESS YOU'VE REVIEWED AND TESTED :)
// Needs work to make robust: check property type's class has default ctor and so on.
// Can also filter out interfaces as these will be auto-subbed.
public static T SubFor<T>() where T : class
{
var gettableVirtualProps = typeof(T).GetProperties()
.Where(x => x.CanRead && x.GetGetMethod().IsVirtual)
.Select(x => x);
var sub = Substitute.For<T>();
@dtchepak
dtchepak / gist:3401408
Created August 20, 2012 05:51
projeuler prob 3 hack
import Control.Monad
import Data.List
import Data.Maybe
factorBy :: Int -> Int -> Maybe (Int, Int)
factorBy n factor = if snd result == 0 then Just (fst result, factor)
else Nothing
where result = n `divMod` factor
firstFactor :: Int -> Maybe (Int, Int)
@dtchepak
dtchepak / AboutAsserts.hs
Created August 22, 2012 11:49
Koan spikes
module HaskellKoans.AboutAsserts where
import HaskellKoans.KoanBase
aboutAsserts =
Koans "HaskellKoans.AboutAsserts"
[ koanAboutQuestion
, koanAboutBool
, koanAboutEquality
]
@dtchepak
dtchepak / randomString.hs
Created September 1, 2012 13:15
random strings (w/out quickcheck)
randomChar :: (RandomGen g) => State g Char
randomChar = state $ randomR ('a','z')
randomStringWithLength :: (RandomGen g) => Int -> State g String
randomStringWithLength i = replicateM i randomChar
randomString :: (RandomGen g) => State g String
randomString = state $ \g ->
let (charsToGen, g') = randomR (1,10) g
in runState (randomStringWithLength charsToGen) g'