Skip to content

Instantly share code, notes, and snippets.

Andy Dent AndyDentFree

Block or report user

Report or block AndyDentFree

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@AndyDentFree
AndyDentFree / demoDefer.cpp
Last active Aug 29, 2015
Shows how modern C++ can use RAII helper objects to provide the "defer" feature just introduced in Swift 2.0
View demoDefer.cpp
// shows how C++11 can implement the "defer" introduced in Swift 2.0
#include <iostream>
using simpleClosure = std::function<void()>; // new typedef syntax used for closure
// class only needs to be declared once then used all over to wrap closures
// see http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
class deferRAII {
public:
deferRAII(simpleClosure cl) : runOnDestruction(cl) {}
@AndyDentFree
AndyDentFree / typeVariantsDemo.swift
Created May 20, 2015
typeVariantsDemo in Swift
View typeVariantsDemo.swift
// the Swift direct equivalent of the F# gist https://gist.github.com/AndyDentFree/46ac476a31b07d103a03
import Darwin
enum Shape {
case Circle (radius:Double)
case Rectangle (width:Double, height:Double)
case Triangle (tbase:Double, height:Double)
}
func area(shape:Shape) -> Double {
@AndyDentFree
AndyDentFree / typeVariantsDemo
Created May 20, 2015
Working example (from Xamarin Studio) demonstrating Type Variants and discriminated unions
View typeVariantsDemo
open Microsoft.FSharp.Math
type Shape =
| Circle of radius:float
| Rectangle of width:float * height:float // tuple of float, float
| Triangle of tbase:float * height:float
let area shape =
match shape with
| Circle radius -> Math.PI * radius * radius
| Rectangle (width, height) -> width * height
@AndyDentFree
AndyDentFree / safeParentViewController.h
Last active Aug 29, 2015
Using Objective-C++ for type-safe walking up parent views
View safeParentViewController.h
/*
Sometimes in iOS code, you need to be able to walk up the chain of parent views, especially if working on legacy code.
This has been hardcoded in the past with horrible assumptions like this, which broke again in iOS 8:
if (IS_IOS7_AND_UP) {
spdVC = (SuperDuperViewController*)[(UITableView*)self.superview.superview.superview.superview.superview delegate];
} else {
spdVC = (SuperDuperViewController*)[(UITableView*)self.superview.superview.superview delegate];
}
You can’t perform that action at this time.