Skip to content

Instantly share code, notes, and snippets.

@dtchepak
dtchepak / Sample.fsproj
Created February 7, 2014 06:28
Microsoft.FSharp.Targets in fsproj
<Choose>
<When Condition="Exists('$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.1\Framework\v4.0\Microsoft.FSharp.Targets')">
<PropertyGroup>
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.1\Framework\v4.0\Microsoft.FSharp.Targets</FSharpTargetsPath>
</PropertyGroup>
</When>
<When Condition="Exists('$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets')">
<PropertyGroup>
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets</FSharpTargetsPath>
</PropertyGroup>
@dtchepak
dtchepak / 00_background.cs
Last active August 29, 2015 13:56
Injection samples
/* Dependency */
public class AnotherClass {
public virtual string DoSomething(string str1) { return str1.ToUpper(); } /* virtual so we can change/mock it.
* Alternatively use an interface, or abstract. */
}
/* Class we want to test */
public class MyClass {
public string str1;
@dtchepak
dtchepak / sample.fs
Created February 23, 2014 10:55
Private constructor in sum type crashes `sprintf "%A"`?
module MyModule
open Xunit
open FsUnit.Xunit
type Sample = Sample of int
with override x.ToString() = sprintf "%A" x
[<Fact>]
let ``sample to string`` ()=
let actual = Sample 1
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);
@dtchepak
dtchepak / Either.cs
Last active August 29, 2015 13:57
Quick sketch of Either type in C#
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;
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)
@dtchepak
dtchepak / a.fs
Last active August 29, 2015 14:00
//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
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());
}
#!/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
@dtchepak
dtchepak / stateExample.hs
Last active August 29, 2015 14:01
Example of getting values out of a computation that are not explicitly passed to a function
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)