Skip to content

Instantly share code, notes, and snippets.

Avatar

Aaron Connolly chefnobody

View GitHub Profile
@chefnobody
chefnobody / Finding Happiness in Functional Programming
Last active Aug 31, 2020
Notes from Brandon William's Finding Happiness in Functional Programming
View 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.
View gist:41f8e6bc6e529710514ca18dab052fa8
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:
View Study-Combine-Classic-Approach
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 {
View Trivial Arithmetic Expression Parser
/*
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
View Processes on Mac OS running on port 3000
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 Mar 18, 2018
Quick demo of creating and breaking a retain cycle
View RetainCycles.swift
//: 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 Mar 18, 2018
Calculates the greatest common denominator of two numbers
View GreatestCommonDenominator.swift
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 Mar 18, 2018
A custom CALayer (review from NSScreencast)
View CustomCALayer.swift
//: 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 Mar 18, 2018
A Binary Search Tree implementation in Swift
View BinarySearchTree.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 Mar 18, 2018
Slow and Fast Fibonacci
View Fibonacci.swift
//: 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)
}