Skip to content

Instantly share code, notes, and snippets.

View austinzheng's full-sized avatar

Austin Zheng austinzheng

View GitHub Profile
PROMOTION DEMOTION
(init?) (init)
[~~~~~~~~~~~AnyRandomAccessCollection~~~~~~~~~~~]
^ ^ | |
| | | |
| | v |
| [~~AnyBidirectionalCollection~~] |
| ^ | |
| | | |
| | v v
@austinzheng
austinzheng / swiftDelegateExample.swift
Created August 1, 2015 00:12
A simple example of setting up a delegate in Swift.
//
// ExampleCode.swift
//
import UIKit
// MARK: - Protocol
protocol SearchQueryProviderProtocol : class { // 'class' means only class types can implement it
func searchQueryData() -> String
@austinzheng
austinzheng / funcLabels.swift
Last active August 29, 2015 14:23
Weird stuff with free function labels in Xcode 7.0 beta 2...
import UIKit
// Running on Xcode 7.0 beta (7A121l)
// All running in a playground
// Open console (View -> Debug Area -> Show Debug Area)
// Define a non-generic free fn taking 1 arg
func notGeneric(x: String) {
print("notGeneric called")
}
@austinzheng
austinzheng / CustomTextInputView.swift
Last active January 20, 2022 15:14
Simple UIKeyInput example
import UIKit
/// A very simple example view that can accept keyboard input and add or delete text from an enclosed label.
class CustomTextInputView : UIControl, UIKeyInput {
var label : UILabel?
override func canBecomeFirstResponder() -> Bool {
return true
}
@austinzheng
austinzheng / prepostseq.swift
Created February 7, 2015 09:27
prefixed postfixed sequence
// Released under the terms of the MIT license.
struct PrefixedPostfixedSequence<T : SequenceType, U where U == T.Generator.Element> : SequenceType {
private let sequence : T
private let initial : U?
private let final : U?
init(_ sequence: T, initial: U? = nil, final: U? = nil) {
self.sequence = sequence
self.initial = initial
@austinzheng
austinzheng / genericsDeadlock.swift
Last active August 29, 2015 14:14
generics deadlock example
import Foundation
// Build this as an OS X command line project, then run it. It should never terminate.
enum Value<T> {
case Nil
case Cons(T, LinkedList<T>)
}
final class LinkedList<T> {
@austinzheng
austinzheng / hkw_mentions_setup.m
Last active August 29, 2015 14:07
Mentions plug-in setup example
// Set up mentions plug-in
- (void)viewDidLoad {
[super viewDidLoad];
HKWMentionsChooserPositionMode mode = HKWMentionsChooserPositionModeEnclosedTop;
NSCharacterSet *controlCharacters = [NSCharacterSet characterSetWithCharactersInString:@"@+"];
HKWMentionsPlugin *mentionsPlugin = [HKWMentionsPlugin mentionsPluginWithChooserMode:mode
controlCharacters:controlCharacters
searchLength:3];
self.plugin = mentionsPlugin;
mentionsPlugin.delegate = [MentionsManager sharedInstance];
@austinzheng
austinzheng / hkw_transformer.m
Last active August 29, 2015 14:07
Hakawai transformer example
// Text transformer (palindrome)
[self.textView transformSelectedTextWithTransformer:^NSAttributedString *(NSAttributedString *input) {
NSString *rawInput = [input string];
NSMutableString *buffer = [NSMutableString string];
unichar stackC;
for (NSInteger i=[rawInput length] - 1; i>=0; i--) {
stackC = [rawInput characterAtIndex:i];
[buffer appendString:[NSString stringWithCharacters:&stackC length:1]];
}
NSDictionary *attrs = [input attributesAtIndex:0 effectiveRange:NULL];
@austinzheng
austinzheng / reify.swift
Last active August 29, 2015 14:05
Reification example
protocol InitProtocol { init() }
struct SomeStruct<T: InitProtocol> {
var x: T? = nil
mutating func instantiate() {
if x == nil {
x = T()
}
}
}
@austinzheng
austinzheng / array_structure.swift
Last active August 1, 2018 07:04
A horrendous example of custom pattern matching in Swift
// Playground - noun: a place where people can play
import Cocoa
enum Wildcard : NilLiteralConvertible {
case Single
case FromBeginning
case ToEnd
case Range(Int)
case Literal(Int)