Skip to content

Instantly share code, notes, and snippets.

Avatar

Joshua Sullivan JoshuaSullivan

View GitHub Profile
@JoshuaSullivan
JoshuaSullivan / GenerateRequest.swft
Created May 12, 2015
Challenge Accepted #2 - Signed API Request
View GenerateRequest.swft
let inputDictionary = [
"author_name": "Robert Jordan",
"book_title": "Knife of Dreams",
"series": "The Wheel of Time, Book 11",
"publisher": "Tor Fantasy",
"published_date": "November 28, 2006"
]
let sortedKeys = inputDictionary.keys.array.sorted(<)
var queryTerms = Array<String>()
@JoshuaSullivan
JoshuaSullivan / LazyInstantiation.swift
Last active Aug 29, 2015
This is a Swift 2.0 Playground that demonstrates some use cases for lazy instantiation of object properties.
View LazyInstantiation.swift
//: # Lazy Instantiation
//:
//: This playground demonstrates situations where lazily instantiating properties is valuable.
//: **NOTE:** This is a Swift 2.0 playground and must be opened in Xcode 7.
import UIKit
import CoreImage
//: A helper function to produce a random CGFloat in the range 0..<1
func randomCGFloat() -> CGFloat {
@JoshuaSullivan
JoshuaSullivan / ForgotCaptureSemantics.swift
Last active Dec 24, 2015
An Exploration of Capture Semantics. Read the blog post: http://www.chibicode.org/?p=28
View ForgotCaptureSemantics.swift
func attemptLogin(user: String, password: String) {
self.loginRequest = APIClient.sharedClient().createLoginRequest(user:user, password:password) {
result in
switch result {
case .Success(let data):
parseLoginData(data) // Compiler error: implicit reference to self
case .Failure(let error):
errorHandlingMethod(error) // Compiler error: implicit reference to self
}
}
@JoshuaSullivan
JoshuaSullivan / DidSetExample.swift
Last active Dec 31, 2015
Swift's didSet property observer is a great way to dynamically configure a view at runtime, but there are limits to what you should do with it. Read the blog post here: http://www.chibicode.org/?p=32
View DidSetExample.swift
class MyClass {
@IBOutlet weak var outputLabel: UILabel! {
didSet {
// Ensure that the label wasn't just set to nil.
guard let outputLabel = self.outputLabel else { return }
// Set the text color based on the user's style choices.
outputLabel.textColor = StyleManager.sharedManager().outputLabelColor
// Set the label to use fixed-width numbers.
@JoshuaSullivan
JoshuaSullivan / Confusion.cikernel
Created Jan 26, 2016
A confusing issue with the CIKernel's sample() method. Assigning the result of samplerTransform() to a variable and then using it in the sample() method breaks everything.
View Confusion.cikernel
// This works.
kernel vec4 simpleFilter(sampler p)
{
vec2 dc = destCoord();
return sample(p, samplerTransform(p, dc));
}
// This does not. It produces [0, 0, 0, 255] for the first 255 pixels and then [0, 0, 0, 0] thereafter.
kernel vec4 simpleFilter(sampler p)
{
@JoshuaSullivan
JoshuaSullivan / TouchCaptureControl.swift
Created Feb 18, 2016
This is a simple control which maps touches within its bounds to a 0.0 - 1.0 range on the horizontal and vertical axes, returned as a CIVector.
View TouchCaptureControl.swift
class TouchCaptureControl: UIControl {
var value: CIVector = CIVector(x: 0.0, y: 0.0)
@IBInspectable var horizontalMinimumValue: CGFloat = 0.0
@IBInspectable var horizontalMaximumValue: CGFloat = 1.0
@IBInspectable var verticalMinimumValue: CGFloat = 0.0
@IBInspectable var verticalMaximumValue: CGFloat = 1.0
@IBInspectable var flipHorizontalAxis: Bool = false
@IBInspectable var flipVerticalAxis: Bool = false
@JoshuaSullivan
JoshuaSullivan / MakingWaves.pde
Created Feb 19, 2016
A Processing 3.0 sketch animating some waves!
View MakingWaves.pde
class SingleWave {
float SEGMENTS_PER_CYCLE = 48.0;
float wavelength, r, dr;
color drawColor;
SingleWave(float wavelength, color drawColor) {
this.wavelength = wavelength;
this.drawColor = drawColor;
@JoshuaSullivan
JoshuaSullivan / SparkBall.pde
Created Feb 25, 2016
A Processing 3 sketch animating sprites moving smoothly about the interior of an imaginary cube.
View SparkBall.pde
static float FOCAL_LENGTH = 200.0;
static float ORBIT_RADIUS = 180.0;
static int SPARK_COUNT = 200;
class Spark {
color clr;
float orbitRadius;
float rx, ry, rz, drx, dry, drz;
Spark(color c, float orbitRadius) {
this.clr = c;
@JoshuaSullivan
JoshuaSullivan / OptionalReturn.swift
Created Mar 3, 2016
Code snippets for my blog post about functions which return optional values vs functions that throw errors.
View OptionalReturn.swift
import Swift
/// This method returns half of the input value if the number is even. It returns nil if the number is odd.
func evenHalfOrNil(input: Int) -> Int? {
if (input % 2 == 0) {
return input / 2
} else {
return nil
}
}
@JoshuaSullivan
JoshuaSullivan / PerlinNoise.cikernel
Last active Mar 31, 2016
My attempt at writing a Perlin Noise function.
View PerlinNoise.cikernel
// Improved Noise - Copyright 2002 Ken Perlin.
// Adapted and updated for iOS by Joshua Sullivan, 2016.01.12
// Apply the function 6t^5 - 15t^4 + 10t^3
float fade(float t)
{
return t * t * t * (t * (t * 6.0 - 15.0) + 10.0);
}
// I'm keeping this around for reference.
You can’t perform that action at this time.