Skip to content

Instantly share code, notes, and snippets.

View cameronpresley's full-sized avatar

Cameron Presley cameronpresley

View GitHub Profile
@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 / 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 / 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);
}
}
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;
using System;
namespace Option
{
public class Option<T>
{
private readonly T _value;
private readonly bool _hasValue;
private Option(T value, bool hasValue)
@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
@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))