Skip to content

Instantly share code, notes, and snippets.


Cameron Presley cameronpresley

View GitHub Profile
View common-mistakes-where.cs
// Generate numbers from -10 .. 100
var numbers = Enumerable.Range(-10, 111);
// Determines all positive numbers that are divisible by 6
var positiveDivisbleBySix = numbers
.Where(x=>x > 0) // iterates through the whole list (111 comparisons, returning 100 results)
.Where(x=>x % 2 == 0) // iterates through the new list (100 comparisons, returning 50 results)
.Where(x=>x % 3 == 0); // iterates through the new list (50 comparisons, returning 16 results)
// Overall metrics: 261 comparisons over 111 total elements)
cameronpresley / example.js
Last active Nov 23, 2020
Lodash Example of Building a Generic Filter
View example.js
const _ = require("lodash");
const items = [
{id:1, name: "fluffy", animal: "cat"},
{id: 2, name: "spot", animal: "dog"},
{id: 3, name: "clifford", animal: "dog"},
{id: 4, name: "tony", animal: "tiger"}
const isAnimal = (animal, item) => item.animal === animal;
cameronpresley /
Created Mar 18, 2019
Learning Functional Programming Through Construction: First Principles

Learning Functional Programming Through Construction: First Principles

In the past five years, functional programming has increased dramatically in popularity which has lead to an explosion of resources in learning these concepts. But, between languages (Haskell, Elm, PureScript, F#), libraries (Ramda, fp-ts), and concepts (Monads, Monoids, Functors), it can be overwhelming in determining where to start and how to begin.

In this talk, I'm going to show you three fundamental concepts of functional programming: pure functions, immutability, and composition by not only explanation, but through building these concepts through code and application. As we explore each concept, I'll show you the advantages of following these principles, how they will improve your development experience, and how they will set the stage for more advanced ideas.

Intended for those who have experience with TypeScript or C#, by the end of this presentation, you will understand how pure functions lead to easier to test code, how imm

cameronpresley / memzip.cs
Created Nov 26, 2018
Zip/Unzip a compressed folder in memory
View memzip.cs
using System;
using System.IO.Compression;
using System.Text;
public class InMemoryZipCompresser
public byte[] Compress (string fileContents)
using (var ms = new MemoryStream())
View functional-fizzbuzz.cs
Func<int, string> fizzBuzz = i =>
if (i % 5 == 0 && i % 3 == 0) return "FizzBuzz";
else if (i % 3 == 0) return "Fizz";
else if (i % 5 == 0) return "Buzz";
return i.ToString();
Func<StringBuilder, string, StringBuilder> reduce = (sb, s) => sb.AppendLine(s);
cameronpresley / chapter-6.hs
Created Jun 21, 2018
Chapter 6 Exercises of Haskell Book
View chapter-6.hs
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)
cameronpresley / eq-instances.hs
Created Jun 20, 2018
Deriving Example Eq instances from Haskell Book
View eq-instances.hs
data TisAnInteger = TisAn Integer
instance Eq TisAnInteger where
(==) (TisAn a) (TisAn b) = a == b
data TwoIntegers = Two Integer Integer
instance Eq TwoIntegers where
(==) (Two a b) (Two c d) = (a == c) && (b == d)
data StringOrInt = TisAnInt Int | TisAString String
instance Eq StringOrInt where
View FunctionExtensions.cs
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);
View Program.fs
// Learn more about F# at
open System
type Record = {x:int; y:int}
type Nested = {number:int; record:Record}
let main argv =
let record = {x=10; y=20}
printfn "Creating a record with x=%i and y=%i" record.x record.y
cameronpresley / Example.cs
Last active Dec 19, 2017
Function Extensions with Examples
View Example.cs
public class Example
public void DemonstrateMap()
Func<int, string> tostring = s => s.ToString();
public void DemonstrateCompose()