Skip to content

Instantly share code, notes, and snippets.

View rjchatfield's full-sized avatar

Smiley Rob rjchatfield

View GitHub Profile
@rjchatfield
rjchatfield / plusMinus.swift
Created August 22, 2015 12:26
Adding generic ± operator in Swift
typealias PlusMinusable = protocol<IntegerArithmeticType, ForwardIndexType>
infix operator ± {}
struct PlusMinus<T: PlusMinusable> {
let value: T
let variance: T
var range: Range<T> {
let lower = value - variance
let upper = value + variance
@rjchatfield
rjchatfield / unwrapping.swift
Created July 28, 2015 14:00
Many ways to unwrap a cat
let unwrapMe: [[Int]] = []
// IF LET
func unwrapWithIfLet (arrArr: [[Int]]) -> Int? {
if let x = arrArr.first?.first {
return x + 1
}
return nil
}
@rjchatfield
rjchatfield / functors.js
Created February 1, 2015 13:52
Maybe, Either & Try Functors in ES6
//--
//-- FUNCTORS in ES6
//--
//-- BOILER PLATE
let fmap = (f) => (functor) => functor.map(f);
let c2 = (f2, f1) => (x) => f2(f1(x));
let c3 = (f3, f2, f1) => (x) => f3(f2(f1(x)));
@rjchatfield
rjchatfield / highOrderFunc.js
Created January 24, 2015 09:16
Clean error handling network calls using high order functions in ES6
/*
==============================================================
Clean error handling network calls using high order functions.
ES6 version of:
http://blog.carbonfive.com/2015/01/05/tidying-up-a-
javascript-application-with-higher-order-functions/
==============================================================
*/
@rjchatfield
rjchatfield / transducers.swift
Last active January 13, 2019 13:41
Transducers & Reducers in Swift 2
//-- This is an introduction to Transducers in Swift, inspired by
// Talk: https://www.youtube.com/watch?v=estNbh2TF3E
// Code: https://github.com/mbrandonw/learn-transducers-playground
// Updated with Swift 2
/**
* Define a few test methods
*/
/// REDUCER
func append <A> (xs: [A], x: A) -> [A] { return xs + [x] }
@rjchatfield
rjchatfield / transducers.js
Last active August 1, 2019 10:47
Transducers & Reducers in JavaScript
//-- This is an introduction to Transducers in Javascript, inspired by
// Talk: https://www.youtube.com/watch?v=estNbh2TF3E
// Code: https://github.com/mbrandonw/learn-transducers-playground
//-- Let's start by defining some reusable functions
// Mappers // A => B
square = (n) => n * n
incr = (n) => n + 1
// Filterers // A => Bool
@rjchatfield
rjchatfield / do_if_else_repeat_while_for.swift
Last active November 7, 2015 06:53
Do, If/ElseIf/If, While, Repeat/While, For in Swift (only using switch cases and recursion)
typealias VoidFuncType = () -> Void
typealias BoolFuncType = () -> Bool
/**
IF/ELSE STATEMENT
*/
typealias ElseIfType = (Bool, VoidFuncType) -> IfElse
typealias ElseType = (VoidFuncType) -> Void
struct IfElse {
@rjchatfield
rjchatfield / waterfall.coffee
Created December 8, 2014 22:05
Functional Waterfall Method
# functional waterfall method
waterfall = (scope) -> deploy(test(develop(design(analyse(scope)))))
# or
waterfall = (scope) -> compose(deploy, test, develop, design, analyse)(scope)
# or
waterfall = (scope) -> scope.analyse().design().develop().test().deploy()
@rjchatfield
rjchatfield / change_problem.coffee
Created December 6, 2014 09:18
FP solve the Change Problem
#
# FP solve the Change Problem
# eg. myChange = calcChange(cost, myPayment)
#
# Used in fold() to build change object
# Should be nested in calcChange, but is lifted for cleanliness
coinAccStructure = (acc, coin) ->
change = acc["payment"]
acc[coin] = change % coin
@rjchatfield
rjchatfield / funfuns.coffee
Last active August 29, 2015 14:09
Functional Functions - "Quick look at common higher-order functions found in functional programs." https://bitbucket.org/rjchatfield/functional-functions
#-- CONTENT
$ ->
___ ''
___ ' ███████╗██╗ ██╗███╗ ██╗ ██████╗████████╗██╗ ██████╗ ███╗ ██╗ █████╗ ██╗'
___ ' ██╔════╝██║ ██║████╗ ██║██╔════╝╚══██╔══╝██║██╔═══██╗████╗ ██║██╔══██╗██║'
___ ' █████╗ ██║ ██║██╔██╗ ██║██║ ██║ ██║██║ ██║██╔██╗ ██║███████║██║'
___ ' ██╔══╝ ██║ ██║██║╚██╗██║██║ ██║ ██║██║ ██║██║╚██╗██║██╔══██║██║'
___ ' ██║ ╚██████╔╝██║ ╚████║╚██████╗ ██║ ██║╚██████╔╝██║ ╚████║██║ ██║███████╗'
___ ' ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═╝╚══════╝'