Skip to content

Instantly share code, notes, and snippets.

View austinzheng's full-sized avatar

Austin Zheng austinzheng

View GitHub Profile
var myClosure: (Int, Int) -> String
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
addSquareToView(size: CGSizeMake(30, 30), color: UIColor.greenColor());
}
func addSquareToView(size s: CGSize, color: UIColor) {
var square: UIView;
let frame = CGRectMake(10, 10, s.width, s.height);
@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 / 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 / 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 / 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 / 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 / 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 / az1.c
Last active December 14, 2015 09:48
AZ 3/1/2013 Question 1. I used Daniel's method and one list traversal (O(n)). A slight modification of my solution from Wednesday is using the do-while loop instead of the while loop, so that the last node in the original list is not omitted. This code can be compiled. In linux, compile with "gcc az1.c -o example1", then run with "./example1". I…
#include <stdio.h>
struct n {
int v;
struct n* next;
};
struct n* split_list(struct n* head, int pivot) {
struct n* cur = head;
struct n* lh = NULL; struct n* lt = NULL;
@austinzheng
austinzheng / min_stack.c
Created March 6, 2013 06:11
A very simple min-stack implementation using arrays to store signed integers. The stack is created with a user-specified size. Supports a pop, push, and current-minimum operation, all of which run in O(1) time. The tradeoff is that twice as much space is required as an equivalent implementation supporting a current-minimum operation running in O…
// min_stack.c
// Austin Zheng
#include <stdio.h>
#include <stdlib.h>
struct min_stack {
int* v; // This array represents the main stack, and contains actual values.
int* m; // This array represents the min stack, and contains an index into the v array.
unsigned int v_ptr; // This is a pointer to the next free main stack slot.