Skip to content

Instantly share code, notes, and snippets.

@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 / 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
runKoan :: Koan -> IO Bool
runUntilFail :: [Koan] -> IO ()
runUntilFail [] = return ()
runUntilFail (x:xs) = do
continue <- runKoan x
when continue $ runUntilFail xs
@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
});
@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 / 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");
error FS0193: internal error: Value cannot be null.
Parameter name: con
@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)
@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
"Is Clojure code hard to understand? Imagine if every time you read Java source code and encountered syntax elements like if statements, for loops, and anonymous classes, you had to pause and puzzle over what they mean. There are certain things that must be obvious to a person who wants to be a productive Java developer. Likewise there are parts of Clojure syntax that must be obvious for one to efficiently read and understand code. Examples include being comfortable with the use of let, apply, map, filter, reduce and anonymous functions ..."
-- R. Mark Volkmann, http://java.ociweb.com/mark/clojure/article.html