Skip to content

Instantly share code, notes, and snippets.

@battermann
battermann / PrimeGenerator.cs
Last active August 29, 2015 14:07
Prime Generator aus Agile Principles, Patterns and Practices in C# von Robert C. Martin refactored nach IOSP
///<remark>
/// Refactored Version (following IOSP)
///</remark>
using System;
using System.Collections.Generic;
using System.Linq;
namespace primegenerator
{
@battermann
battermann / CSharpToFSharpConverter.cs
Last active August 29, 2015 14:22
How to pass an argument to a F# function that takes ('a -> unit) from C#
using System;
using Microsoft.FSharp.Core;
namespace FSharpConverter
{
public static class ToFSharpFuncConverterExtensions
{
private static readonly Unit Unit = (Unit)Activator.CreateInstance(typeof(Unit), true);
public static Func<T, Unit> ToFunc<T>(this Action<T> action)
@battermann
battermann / FunctionCompositionExamples.cs
Created June 10, 2015 10:34
Function composition in C#
using System;
using System.Collections.Generic;
using NUnit.Framework;
using System.Linq;
namespace function_composition_in_csharp
{
// The NUnit Nuget package is required for this
// To install NUnit, run the following command in the Package Manager Console
// PM> Install-Package NUnit -Version 2.6.4

db scripts

CREATE TABLE [dbo].[UniqueIdentifier]
(
	[Typ] NVARCHAR(50) NOT NULL PRIMARY KEY,
	[CurrentMaxId] NVARCHAR(max) NOT NULL
)
@battermann
battermann / romNumParser.hs
Last active December 3, 2015 09:51
Parser for Roman Numerals
import Text.ParserCombinators.Parsec (char, many1, string, choice, try, parse)
import Text.Parsec.Prim (parserReturn, parserFail, ParsecT)
import Data.Functor
import Data.Functor.Identity
import Data.Either
import Test.Hspec
sat :: String -> (a -> Bool) -> ParsecT s u m a -> ParsecT s u m a
sat msg predicate parser = parser >>= (\x -> if predicate x then parserReturn x else parserFail msg)
@battermann
battermann / TicTacToe.cs
Last active March 10, 2016 12:01
Model for Tic-Tac-Toe
using System.Collections.Generic;
namespace TicTacToe
{
public enum Player
{
PlayerX,
PlayerO
}
@battermann
battermann / QuickSort.cs
Created April 15, 2016 22:16
Recursive implementation of QuickSort in C#. It's really slow and absolutely not recommended to use it!! It's only meant for demonstration purposes.
static class Program
{
static void Main(string[] args)
{
var list = new List<int> {4, 5, 4, 7, 9, 1, 6, 1, 0, -99, 10000, 3, 2};
var sorted = list.QuickSort();
Console.WriteLine(String.Join(", ", sorted));
// output: -99, 0, 1, 1, 2, 3, 4, 4, 5, 6, 7, 9, 10000
module Git =
open System
open System.Diagnostics
let private runCommand cmd args =
let startInfo = new ProcessStartInfo()
startInfo.FileName <- cmd
startInfo.Arguments <- args
startInfo.UseShellExecute <- false
startInfo.RedirectStandardOutput <- true
public class EditedPair<T>
{
public EditedPair(T old, T @new)
{
Old = old;
New = @new;
}
public T Old { get; private set; }
public T New { get; private set; }
@battermann
battermann / ConditionalsExtensions.cs
Last active November 1, 2016 21:56
CondictionalsExtensions
public static class CondictionalsExtensions
{
public static Tuple<bool, T> If<T>(this T src, Predicate<T> p)
{
return p(src)
? Tuple.Create(true, src)
: Tuple.Create(false, src);
}
public static Tuple<bool, T, R> Then<T, R>(this Tuple<bool, T> src, Func<T, R> f)