Skip to content

Instantly share code, notes, and snippets.

View zyuanming's full-sized avatar
🎯
Focusing

YuanMing zyuanming

🎯
Focusing
View GitHub Profile
@zyuanming
zyuanming / Lock
Created June 5, 2017 13:53
Locks
internal final class SpinLock {
private let lock = NSLock()
func sync<T>(action: () -> T) -> T {
lock.lock()
defer { lock.unlock() }
return action()
}
}
@zyuanming
zyuanming / String escape white spaces
Created June 4, 2017 01:16
String escape white spaces
extension String {
private static let whitespaceRegularExpression = try! NSRegularExpression(pattern: "\\s")
var escapingWhitespaces: String {
return String.whitespaceRegularExpression.stringByReplacingMatches(
in: self,
range: NSRange(location: 0, length: self.utf16.count),
withTemplate: "\\\\$0"
)
}
@zyuanming
zyuanming / UINavigationbar hidden
Created May 25, 2017 12:18
UINavigationbar hidden
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
navigationController?.isNavigationBarHidden = false
navigationController?.navigationBar.alpha = 0
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let presentedVC = UIApplication.presentedViewController(), presentedVC != self {
navigationController?.isNavigationBarHidden = true
@zyuanming
zyuanming / Commutative Monoid
Created May 18, 2017 08:12
Commutative Monoid
protocol Semigroup {
// Binary, associative semigroup operation (op)
func op (_ g: Self) -> Self
}
extension Int : Semigroup {
func op (_ n: Int) -> Int {
return self + n
}
}
@zyuanming
zyuanming / Algebraic Structure
Created May 18, 2017 07:47
Algebraic Structure
protocol Semigroup {
// Binary semigroup operation
// **AXIOM** Should be associative:
// a.op(b.op(c)) == (a.op(b)).op(c)
func op (_ g: Self) -> Self
}
extension Int : Semigroup {
func op (_ n: Int) -> Int {
return self + n
@zyuanming
zyuanming / functional-data-structures
Created May 18, 2017 07:36
functional-data-structures
indirect enum List <A> {
case Nil
case Cons(
A,
List<A>
)
}
func map <A, B> (_ f: @escaping (A) -> B) -> (List<A>) -> List<B> {
return { xs in
@zyuanming
zyuanming / Future
Last active June 15, 2017 02:25
Applicatives and Swift – Stephen Celis
import Foundation
import Dispatch
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
precedencegroup Additive {
associativity: left
}
@zyuanming
zyuanming / Json Parser
Last active May 16, 2017 03:24
Applicative Parsing with Documentation – Chris Eidhof
import Foundation
typealias JSONDict = [String: Any]
struct Parameter<A> {
let apiDescription: String
let parse: (Any) -> A?
}
extension Parameter {
@zyuanming
zyuanming / Timer
Last active June 15, 2017 02:26
Timer not retain view controller
// http://www.futantan.com/2016/04/14/NSTimer-tips/#more
extension NSTimer {
private class FTTimerClosureWraper {
private (set) var timerClosure: () -> ()
init(timerClosure: () -> () ) {
self.timerClosure = timerClosure
}
}
@zyuanming
zyuanming / reduce
Created April 19, 2017 10:12
Swift
struct Stock {
var name = ""
var id = ""
}
let stock1 = Stock(name: "name", id: "1")
let stock2 = Stock(name: "name", id: "2")
let stock3 = Stock(name: "name", id: "3")
let stock4 = Stock(name: "name", id: "4")
let stock5 = Stock(name: "name", id: "5")