Skip to content

Instantly share code, notes, and snippets.

View chefnobody's full-sized avatar

Aaron Connolly chefnobody

View GitHub Profile
@chefnobody
chefnobody / Finding Happiness in Functional Programming
Last active August 31, 2020 17:55
Notes from Brandon William's Finding Happiness in Functional Programming
https://www.youtube.com/watch?v=A0VaIKK2ijM
SEPARATION OF EFFECTS FROM PURITY
Part 1:
Isolation of side-effects
An expression is said to have a side-effect if its execution makes an observable change to the outside world.
The goal is to make side-effects understandable, not make them go away.
@chefnobody
chefnobody / gist:41f8e6bc6e529710514ca18dab052fa8
Created May 5, 2020 21:16
Study-Combine-Refactor-To-Combine
import Combine
import PlaygroundSupport
import UIKit
/// A line by line walk through of the following Combine example:
/// https://useyourloaf.com/blog/getting-started-with-combine/
class TermsOfUseViewController: UIViewController {
// Some state:
import PlaygroundSupport
import UIKit
/// A line by line walk through of the following Combine example:
/// https://useyourloaf.com/blog/getting-started-with-combine/
class TermsOfUseViewController: UIViewController {
// Some state:
private var acceptedSwitch1: Bool = false {
/*
Write a function that takes an expression and evaluates it. The expression is a different notation of an operand working over two operands. For example:
"2 + 5", in this prefix expression language could be written "(+ 2 5)"
The grammar for this expression syntax works like so:
expression := "( operator operand operand )"
operator: "*" | "+"
operand: expression | integer
lsof -n -i4TCP:3000
# source:
# https://blog.jayway.com/2012/09/08/finding-the-pid-listening-on-a-specific-port-on-mac-os-x/
@chefnobody
chefnobody / RetainCycles.swift
Created March 18, 2018 17:59
Quick demo of creating and breaking a retain cycle
//: Playground - noun: a place where people can play
import Foundation
protocol SendDataDelegate: class {}
// Only need to use "weak" if the delegate is of type `class`? structs and enums are value types, not reference types so they don't create retain cycles.
class SendingVC {
weak var delegate: SendDataDelegate? // Remove "weak" and you create a strong reference between both these *VC objects
}
@chefnobody
chefnobody / GreatestCommonDenominator.swift
Created March 18, 2018 17:57
Calculates the greatest common denominator of two numbers
import Foundation
func gcd(_ a:Int, _ b:Int) -> Int {
let r = a % b
if r != 0 {
return gcd(b, r)
} else {
return b
}
}
@chefnobody
chefnobody / CustomCALayer.swift
Created March 18, 2018 17:55
A custom CALayer (review from NSScreencast)
//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport
private final class ProgressLayer: CALayer {
@NSManaged var progress: CGFloat
private let fillColor = UIColor.blue.cgColor
@chefnobody
chefnobody / BinarySearchTree.swift
Created March 18, 2018 17:54
A Binary Search Tree implementation in Swift
//: Playground - noun: a place where people can play
// Inspiration: http://cslibrary.stanford.edu/110/BinaryTrees.html
import UIKit
var values = [10, 5, 6, 8, 9, 22, 3, 2, 4, 7, 14]
class Node {
var value:Int = 0
var left:Node? = nil
@chefnobody
chefnobody / Fibonacci.swift
Created March 18, 2018 17:49
Slow and Fast Fibonacci
//: Playground - noun: a place where people can play
import Foundation
import UIKit
func fib(_ n: Int) -> Int {
if n == 0 { return 0 }
if n == 1 { return 1 }
return fib(n - 1) + fib(n - 2)
}