Skip to content

Instantly share code, notes, and snippets.

@dtchepak
dtchepak / Maybe.rb
Created December 17, 2012 12:31
Attempt at Maybe in Ruby
class Maybe
def self.empty
Maybe.new nil
end
def initialize(value)
@value = value
end
def bind(f)
@value.nil? ? self : f.call(@value)
end
@dtchepak
dtchepak / lazymonad.cs
Created December 12, 2012 11:30
Monad implementation for Lazy<T>
// See also: http://davesquared.net/2012/12/lazy-monad-csharp.html
public static class LazyExtensions
{
public static Lazy<TResult> Select<T, TResult>(this Lazy<T> value, Func<T, TResult> selector)
{
return new Lazy<TResult>(() => selector(value.Value));
}
public static Lazy<TResult> SelectMany<T, TResult>(this Lazy<T> value, Func<T, Lazy<TResult>> selector)
error FS0193: internal error: Value cannot be null.
Parameter name: con
@dtchepak
dtchepak / gist:4083032
Created November 16, 2012 01:17
Concat enumerables
[Test]
public void TEST()
{
var results = FirstResults().Concat(GetResults());
Console.WriteLine("results created");
Console.WriteLine("get first");
results.First();
Console.WriteLine("get last");
@dtchepak
dtchepak / gist:4031132
Created November 7, 2012 12:16
learning list zippers the hard way
import Control.Arrow ((>>>))
import Data.List.Zipper
import Data.Maybe
{- ref:
- http://www.reddit.com/r/dailyprogrammer/comments/12qi5b/1162012_challenge_111_easy_star_delete/
- -}
starDelete' :: String -> String
starDelete' [] = []
@dtchepak
dtchepak / ShouldNotReturnNullWebServiceResponse.cs
Created October 19, 2012 23:38
ShouldNotReturnNullWebServiceResponse
[Test]
public void ShouldNotReturnNullWebServiceResponse()
{
var iv_client = Substitute.For<Client>();
iv_client.GetValuation("G1|12345", 10000, 2008, "08").Returns(new WebServiceResponse()
{
Result = false,
ErrorEnum = IdResponseCode.NoDataFound
});
runKoan :: Koan -> IO Bool
runUntilFail :: [Koan] -> IO ()
runUntilFail [] = return ()
runUntilFail (x:xs) = do
continue <- runKoan x
when continue $ runUntilFail xs
@dtchepak
dtchepak / gist:3780022
Created September 25, 2012 04:45
Random date string
randomMonth :: (RandomGen g) => State g String
randomMonth =
let monthNames = [ "January", "February", "March"
, "April", "May", "June"
, "July", "August", "September"
, "October", "November", "December"
]
in (monthNames !!) <$> randomNumber (1,11)
randomNumber :: (RandomGen g) => (Int,Int) -> State g Int
@dtchepak
dtchepak / randomFields.hs
Created September 1, 2012 14:10
replacing field contents with random strings
import Control.Monad.State
import System.Random
type Field = String
replaceField :: String -> Field -> Field
replaceField newContent field =
let fieldName = takeWhile (/=':') field
in fieldName ++ ": " ++ newContent
@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'