Skip to content

Instantly share code, notes, and snippets.

@lizixroy
lizixroy / gist:e5dd4f9702274449e10c
Created May 14, 2015 04:29
Macros for checking the iPhone model based on screen sizes.
// macros that can be used to quickly check the model of current iPhone in your Objective-C code.
#define IS_IPHONE_6_PLUS ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )736 ) < DBL_EPSILON )
#define IS_IPHONE_6 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )667 ) < DBL_EPSILON )
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
#define IS_IPHONE_4 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )480 ) < DBL_EPSILON )
@lizixroy
lizixroy / gist:f238cc4cc4ece3dac80e
Created May 30, 2015 00:06
Interactively explore the strong reference cycle.
/* Let's explore a little bit how to break the strong reference cycles.
* In this Playground file I re-created the examples mentioned in the ARC chapter in the Swift Porgramming Lang book: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html
* In each section, the code is implemented to avoid strong reference cycle. If you want to see how a strong reference cycle can actually occur follow the comments in each section play for yourself.
* Avoiding memory leak is important for making your app memory-efficient and, in some cases, removing the risks of crashing out.
*/
import UIKit
/* Section 1 */
@lizixroy
lizixroy / ParseTreeNode.swift
Created July 28, 2016 03:00
ParseTreeNode.swift
class ParseTreeNode {
let nonTerminal: String
let leftNode: ParseTreeNode?
let rightNode: ParseTreeNode?
init(nonTerminal: String, leftNode: ParseTreeNode?, rightNode: ParseTreeNode?) {
self.nonTerminal = nonTerminal
self.leftNode = leftNode
self.rightNode = rightNode
}
}
class CKYParser {
func parse(words words: [String], language: Language) -> [[[ParseTreeNode]]] {
let wordCount = words.count
var table = [[[ParseTreeNode]]]()
for _ in 0...words.count - 1 {
let array = [[ParseTreeNode]](count: wordCount, repeatedValue: [ParseTreeNode]())
table.append(array)
}
// populate lexicon. assuming all the words are in our lexicon
for index in 0...words.count - 1 {
@lizixroy
lizixroy / language.swift
Last active July 28, 2016 03:03
Language
class Language {
private let separator = ", "
private var productions = [String: [String]]()
init(setUpProductionRules: Bool) {
if setUpProductionRules {
self.setUpProductionRules()
}
}
let wordCount = words.count
var table = [[[ParseTreeNode]]]()
for _ in 0...words.count - 1 {
let array = [[ParseTreeNode]](count: wordCount, repeatedValue: [ParseTreeNode]())
table.append(array)
}
for index in 0...words.count - 1 {
let word = words[index]
let nonTerminals = language.recognize([word])!
for nonTerminal in nonTerminals {
table[index][index].append(ParseTreeNode(nonTerminal: nonTerminal, leftNode: nil, rightNode: nil))
}
}
for col in 0...wordCount - 1 {
for row in (0...wordCount - 1).reverse() {
var t = row
while t + 1 <= col {
let arr1 = table[row][t]
let arr2 = table[t + 1][col]
if arr1.count == 0 || arr2.count == 0 {
t += 1
continue
}
@lizixroy
lizixroy / RxSwift-article-1.swift
Created September 4, 2016 19:34
RxSwift-article-1
class MessageViewModel {
let messageSubject = BehaviorSubject<String>(value: "Greetings")
func receiveMessage(message: String) {
messageSubject.onNext(message)
}
}
class MessageViewController: UIViewController {
@lizixroy
lizixroy / RxSwift-article-2.swift
Created September 4, 2016 19:37
RxSwift-article-2.swift
messageViewModel.messageSubject.subscribeNext { [unowned self] message in
self.messageLabel.text = message
}.addDisposableTo(disposeBag)