Skip to content

Instantly share code, notes, and snippets.

View cameronpresley's full-sized avatar

Cameron Presley cameronpresley

View GitHub Profile
@cameronpresley
cameronpresley / performanceLogger.fs
Created February 8, 2016 18:43
Performance Logger - Computational Expression that logs how long a function ran
open System.Diagnostics
open System.Threading
type PerformanceLogger (printer:string -> unit) =
member this.Bind(x, func) =
let stopWatch = new Stopwatch()
stopWatch.Start ()
func x
stopWatch.Stop ()
printer (sprintf "Took %i seconds to run %A" (stopWatch.ElapsedMilliseconds/1000L) (func))
using System;
namespace Option
{
public class Option<T>
{
private readonly T _value;
private readonly bool _hasValue;
private Option(T value, bool hasValue)
public class Result<T, U>
{
private readonly T _left;
private readonly U _right;
private readonly bool _isLeft;
private Result(T left, U right, bool isLeft)
{
_left = left;
_right = right;
_isLeft = isLeft;
@cameronpresley
cameronpresley / NullableExtensions.cs
Last active July 16, 2017 22:42
Extension method for Nullable to Option
public static class NullableExtensions
{
public static Option<T> ToOption<T>(this T? nullable) where T : struct
{
if (nullable == null) return Option<T>.None();
return Option<T>.Some(nullable.Value);
}
}
@cameronpresley
cameronpresley / terminology.md
Created September 3, 2017 14:19
Functional Terminology

Semigroup

Follows Associativity Law and has an append method (traditionally known as mappend)

Monoid

Is a Semigroup that also defines an identity element such that for any a and operation mappend, the following is true: mappend a identity = mappend identity a = a

Catamorphism

Refers to using Reduce instead of recursion to reduce one or more items to a single item

@cameronpresley
cameronpresley / Example.cs
Last active December 19, 2017 12:51
Function Extensions with Examples
public class Example
{
public void DemonstrateMap()
{
Func<int, string> tostring = s => s.ToString();
4.Map(tostring);
}
public void DemonstrateCompose()
{
@cameronpresley
cameronpresley / Program.fs
Last active April 26, 2018 15:45
F# Lenses
// Learn more about F# at http://fsharp.org
open System
type Record = {x:int; y:int}
type Nested = {number:int; record:Record}
[<EntryPoint>]
let main argv =
let record = {x=10; y=20}
printfn "Creating a record with x=%i and y=%i" record.x record.y
@cameronpresley
cameronpresley / mars_rover.elm
Last active May 8, 2018 11:16
Sample solution of Mars Rover kata
import Html exposing (div, button, text, h3, h2, input)
import Html.Events exposing (onClick, onInput)
import Html.Attributes exposing(..)
type Direction = North | South | East | West
type alias Location =
{ x: Int
, y: Int
public static class FunctionExtensions
{
public static Func<T2> Apply<T1, T2>(this Func<T1, T2> func, T1 input) => () => func(input);
public static Func<T2, T3> Apply<T1, T2, T3>(this Func<T1, T2, T3> func, T1 input) => x => func(input, x);
public static Func<T2, T3, T4> Apply<T1, T2, T3, T4>(this Func<T1, T2, T3, T4> func, T1 input) =>
(a, b) => func(input, a, b);
public static Action Apply<T1>(this Action<T1> action, T1 input) => () => action(input);
public static Action<T2> Apply<T1, T2>(this Action<T1, T2> action, T1 input) => a => action(input, a);
@cameronpresley
cameronpresley / chapter-6.hs
Created June 21, 2018 21:00
Chapter 6 Exercises of Haskell Book
import qualified Data.List (sort)
data Person = Person Bool
instance Show Person where
show (Person b) = show b
printPerson :: Person -> IO ()
printPerson person = putStrLn (show person)
data Mood = Blah | Woot deriving (Show, Eq)