Skip to content

Instantly share code, notes, and snippets.

View JoshuaSullivan's full-sized avatar

Joshua Sullivan JoshuaSullivan

View GitHub Profile
@JoshuaSullivan
JoshuaSullivan / RepeatingSequence.swift
Created December 8, 2016 15:49
A new Sequence type that returns a repeating sequence of values.
/// The RepeatingSequence accepts a collection and returns the values sequentially until the
/// final element is returned, at which point the sequence wraps around to the first element
/// of the collection and continues from there.
struct RepeatingSequence<T>: Sequence {
/// The Collection that we base our sequence on. We use a Collection and not
/// another Sequence because Sequences are not guaranteed to be repeatedly iterated.
let data: AnyCollection<T>
/// We can optionally specify a maximum number of iterations. This is necessary
@JoshuaSullivan
JoshuaSullivan / ObfuscationDecoder.swift
Last active May 6, 2019 11:48
This playground shows how to use a RepeatingSequence type to create strong string obfuscation using a multi-byte nonce.
import Foundation
public struct ObfuscationDecoder {
public enum DeobfuscationError: Error {
case invalidStringBytes
}
/// The multi-byte nonce used to encode strings.
private static let nonce: [UInt8] = [199, 152, 254, 45, 85, 241, 134, 185, 22, 249, 182, 208, 43, 176, 143, 252]
@JoshuaSullivan
JoshuaSullivan / ProtocolsAndInheritance.swift
Last active October 28, 2016 18:27
This playground demonstrates a potentially confusing gotcha when using protocols that have default method implementations along with class inheritance.
//: # Protocols and Inheritance
//: This playground demonstrates a gotcha in the use of protcols with default
//: method implementations along with class inheritance.
import UIKit
//: This protocol defines a single method that takes an `Int` and returns an `Int`.
protocol Doable {
func doSomething() -> Int
}
@JoshuaSullivan
JoshuaSullivan / ColorCubeHelper-swift2.swift
Last active September 12, 2024 10:24
Here are the Swift 2.3 and Swift 3.0 versions of the ColorCubeHelper class.
//
// ColorCubeHelper.swift
//
// Created by Joshua Sullivan on 10/01/16.
// Copyright © 2016 Joshua Sullivan. All rights reserved.
//
import UIKit
import Accelerate
@JoshuaSullivan
JoshuaSullivan / WUndergroundAPIClient.swift
Last active June 23, 2016 15:20
This is a Swift Playground file implementing a solution to Challenge Accepted #49.
//: # Challenge Accepted #49
//: ## WUnderground API Client
//: ### Version 2.0
//:
//: Implement the networking layer for the Umbrella weather app(iOS or Android),
//: using a similar architecture to the one demonstrated in the first episode of Swift Talks.
//:
//: - note:
//: This is my updated solution to the challenge which demonstrates a more powerful and flexible
//: approach to customizing the request sent to the API through the use of closures rather than
@JoshuaSullivan
JoshuaSullivan / ColorCubeImageCreator-swift2.swift
Last active April 25, 2023 16:45
The Color Cube Image Creator creates the specially formatted images needed to create data for the CIColorCube filter.
//
// ColorCubeImageCreator.swift
// ColorCubeImageCreator
//
// Created by Joshua Sullivan on 4/25/16.
// Copyright © 2016 Joshua Sullivan. All rights reserved.
//
import UIKit
@JoshuaSullivan
JoshuaSullivan / MTKViewController.swift
Last active April 21, 2016 18:06
Trying to figure out why the View Controller using a MTKView and a Metal-backed CIContext runs at 1/3 of the speed of an GLKView + OpenGLES2-backed CIContext.
//
// ViewController.swift
// MetalCoreImage
//
// Created by Joshua Sullivan on 4/21/16.
// Copyright © 2016 The Nerdery. All rights reserved.
//
import UIKit
import MetalKit
@JoshuaSullivan
JoshuaSullivan / OptionalReturn.swift
Created March 3, 2016 16:37
Code snippets for my blog post about functions which return optional values vs functions that throw errors.
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 / SparkBall.pde
Created February 25, 2016 22:56
A Processing 3 sketch animating sprites moving smoothly about the interior of an imaginary cube.
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 / MakingWaves.pde
Created February 19, 2016 20:49
A Processing 3.0 sketch animating some waves!
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;