Skip to content

Instantly share code, notes, and snippets.

@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
@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)
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; }
package futureeitherapplicativestack
import cats._
import cats.data._
import cats.implicits._
import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext.Implicits.global
#load @"paket-files/fsprojects/Chessie/src/Chessie/ErrorHandling.fs"
open System
open Chessie.ErrorHandling
type Person = { FirstName: string; LastName: string }
let id1 = Guid.NewGuid()
let id2 = Guid.NewGuid()
@battermann
battermann / io.fsx
Last active July 6, 2020 15:21
IO Monad in F#
[<AutoOpen>]
module IO =
type IO<'a> =
private
| Return of (unit -> 'a)
| Suspend of (unit -> IO<'a>)
let rec run x =
match x with
| Return v -> v()
@battermann
battermann / free.fsx
Last active February 17, 2022 23:26
Free Monad like pattern in F#
#load @"paket-files/fsprojects/Chessie/src/Chessie/ErrorHandling.fs"
type Continuation<'output, 'next> = 'output -> 'next
module TerminalDsl =
open Chessie.ErrorHandling
type Terminal<'next> =
| WriteLine of string * Continuation<unit, 'next>
| ReadLine of unit * Continuation<string, 'next>
@battermann
battermann / replaceoptionwithnull.md
Last active October 23, 2018 23:21
Replace Optional with null Refactoring

Replace Option with null refactoring

You are forced to do checks for None and Some and methods like map or flatMap make your code uglier.

Replace the optional value with null:

val customer: Option[Customer] = getCustomer(customerId)

val plan = customer.map(_.plan)
@battermann
battermann / replaceEither.md
Last active October 23, 2018 23:31
Replace Either with Throwing Exceptions

Replace Either with Throwing Exceptions

It is annoying to always unwrap a value from an Either only to be forced to handle the failure case.

Replace the Either value of the failure case with throwing an exception:

// ...
if (numberOfSeats > 1) {
 Right(makeReservation(numberOfSeats))