Skip to content

Instantly share code, notes, and snippets.

@lizixroy
lizixroy / RxSwift-article-3.swift
Created September 4, 2016 19:46
RxSwift-article-3
@warn_unused_result(message="http://git.io/rxs.ud")
public func subscribeNext(onNext: (E) -> Void) -> Disposable {
let observer = AnonymousObserver<E> { e in
if case .Next(let value) = e {
onNext(value)
}
}
return self.subscribeSafe(observer)
}
@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)
@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 {
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
}
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))
}
}
let wordCount = words.count
var table = [[[ParseTreeNode]]]()
for _ in 0...words.count - 1 {
let array = [[ParseTreeNode]](count: wordCount, repeatedValue: [ParseTreeNode]())
table.append(array)
}
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 / 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
}
}
@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()
}
}
@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 */