Skip to content

Instantly share code, notes, and snippets.

Mathew Sanders mathewsanders

Block or report user

Report or block mathewsanders

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
mathewsanders / Animation.playground.swift
Last active Mar 13, 2017
Example of exploring animation in a playground with use of UISlider to scrub animation.
View Animation.playground.swift
import UIKit
import PlaygroundSupport
let containerView = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
containerView.backgroundColor = .white
// Show the container view in the Assistant Editor
PlaygroundPage.current.liveView = containerView
// transformations for the dots to be pushed to either left or right of capsule
mathewsanders / FlattenTests.swift
Created Mar 9, 2017
XCTest to look at tail-call optimization
View FlattenTests.swift
import XCTest
class FlattenTests: XCTestCase {
var input: [Any] = []
override func setUp() {
let test: [Any] = [1, [2], [3, [4]], [5, [6]], 7, [8], [[[[9, [10]]], [11, [12], 13]], 14, [15, [16]], [17, [18]], 19], 20, [21, 22], [23, 24, 25]]
mathewsanders / ResizingTextView.swift
Created Jan 3, 2017
TextView subclass that animates resize as text is entered
View ResizingTextView.swift
import UIKit
/// TextView subclass that animates resize as text is entered
/// Should be used with constraints that pin width so that contentView
/// expands in height to accommodate the current text
class ResizingTextView: UITextView {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
isScrollEnabled = false
View SubstringTests.swift
// SubstringTests.swift
// SubstringTests
// Created by Mat on 1/3/17.
// Copyright © 2017 Mat. All rights reserved.
import XCTest
View FooBar.swift
enum FooBar: Hashable {
case foo(String)
case bar
case baz
// I want to use an enum as a dictionary key, so it needs to be Hashable,
// will hardcoding hashValues like this lead to a disaster? What's a better alternative?
public var hashValue: Int {
switch self {
mathewsanders / CGSize+Extension.swift
Created Oct 14, 2016
Create CGContext from a CGSize
View CGSize+Extension.swift
extension CGSize {
typealias ContextClosure = (_ context: CGContext, _ frame: CGRect) -> ()
func image(withContext context: ContextClosure) -> UIImage? {
let frame = CGRect(origin: .zero, size: self)
context(UIGraphicsGetCurrentContext()!, frame)
let image = UIGraphicsGetImageFromCurrentImageContext()
mathewsanders / pattern-matching.swift
Last active Apr 25, 2016
Array pattern matching
View pattern-matching.swift
// the types of items that a template can be built up from, in the future I'd be interested in expanding this
// to allow a wider range of options.
enum TemplateItem<T> {
case Exact(T) // matches only this exact element
case Either([T]) // matches any one of the elements given
case LazySequence // matches any sequence of elements
// create a template to describe the pattern you want to look for in the array
let moreThanTemplate: [TemplateItem<String>] = [
mathewsanders / regex.swift
Created Apr 25, 2016
Using a regular expression to check for a possible rule structure
View regex.swift
let pattern = "(greater|more|)(.+)(than|)(.+)"
let regex = try! NSRegularExpression(pattern: pattern, options: .CaseInsensitive)
let text = "more blue triangles than red circles"
let matches = regex.matchesInString(text, options: [], range: NSRange(location: 0, length: text.characters.count))
if let match = matches.first {
let seperator1 = (text as NSString).substringWithRange(match.rangeAtIndex(1))
let fragment1 = (text as NSString).substringWithRange(match.rangeAtIndex(2))
let seperator2 = (text as NSString).substringWithRange(match.rangeAtIndex(3))
mathewsanders / ContestList.vue
Last active Mar 3, 2016
ContestList.vue template snippet
View ContestList.vue
<!-- template code -->
<!-- loop over parties -->
<li class="party" v-for="party in APIresponse.parties">
<h3>{{ }} Party</h3>
<!-- loop over contests for a party -->
mathewsanders / StyleGuideView.vue
Last active Feb 20, 2016
Design in the browser, activity 12: update StyleGuideView.vue
View StyleGuideView.vue
<div class="details">
<h1>Cafe Card</h1>
<p>The cafe card shows information about a specific cafe including a description, hours, and the cafe's location.</p>
v-model directive connects our 'cafe' object to the contents of the text area with what is called
a two-way binding, meaning that changes made in the textarea update the 'cafe' object, and that changes
to the 'cafe' object also update the contents of the textarea.
<textarea v-model="cafe | json"></textarea>
You can’t perform that action at this time.