Skip to content

Instantly share code, notes, and snippets.

View rjchatfield's full-sized avatar

Smiley Rob rjchatfield

View GitHub Profile
@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
$ ->
___ ''
___ ' ███████╗██╗ ██╗███╗ ██╗ ██████╗████████╗██╗ ██████╗ ███╗ ██╗ █████╗ ██╗'
___ ' ██╔════╝██║ ██║████╗ ██║██╔════╝╚══██╔══╝██║██╔═══██╗████╗ ██║██╔══██╗██║'
___ ' █████╗ ██║ ██║██╔██╗ ██║██║ ██║ ██║██║ ██║██╔██╗ ██║███████║██║'
___ ' ██╔══╝ ██║ ██║██║╚██╗██║██║ ██║ ██║██║ ██║██║╚██╗██║██╔══██║██║'
___ ' ██║ ╚██████╔╝██║ ╚████║╚██████╗ ██║ ██║╚██████╔╝██║ ╚████║██║ ██║███████╗'
___ ' ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═╝╚══════╝'
@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 / 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 / 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 / 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 / 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 / FluxDescription.swift
Created September 8, 2015 15:54
Explaining Flux data flow in Swift
// Line 109 is where the magic is!
import Foundation
import XCPlayground
func setInterval(seconds interval: Int, f: ()->()) {
let delta = Int64(1) * Int64(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, delta)
dispatch_after(time, dispatch_get_main_queue(), {
@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 / types.md
Last active December 13, 2015 20:15
Types are Coming

Types are coming

Last week I had a crash course in PureScript - a Haskell-like language that compiles to JavaScript. I’m not about to preach that you should write your web apps in Haskell (though I strongly recommend learning Haskell), however, I do wish to explore an important part of any Functional Programming language that has eluded Javascript...

Types.

There is a lot to be said about Javascript and its lack of static typing. We’ve built the whole web with it! But this post will explore the benefits of a compile-time strong static type system.

“But Javascript does have types!?”

@rjchatfield
rjchatfield / linkedList.swift
Created August 16, 2014 12:21
LinkedList in Swift
// LINKED LIST
/**
* NODE
*/
private class Node<T:protocol<Printable, Comparable>>: Printable, Comparable {
/**
* PROPERTIES
*/
var info:T