This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
When reading about gzip and bzip2, I found an interesting data pattern | |
that terribly breaks gzip where the compressed file is larged than the | |
uncompressed file. The reason is that the input data is incompressible | |
to gzip, hence gzip has to store the data as uncompressed, adding file | |
header and block headers, resulting in extra bytes. On the other hand, | |
bzip is still quite stable and able to achieve a good compression ratio. | |
The format of data from the below script is as follows: | |
step 1, start 0: 0 1 2 3 ... 255 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Reactive where Base: NSObject { | |
func cfNotification(notificationName: CFString) -> Observable<Notification> { | |
let cfNotificationName = CFNotificationName(notificationName) | |
let notificationCenter = CFNotificationCenterGetDarwinNotifyCenter() | |
CFNotificationCenterAddObserver( | |
notificationCenter, | |
Unmanaged.passRetained(self.base).toOpaque(), | |
{ ( | |
center: CFNotificationCenter?, | |
observer: UnsafeMutableRawPointer?, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Analytics { | |
static func track(event: AppEvent) { | |
let mirror = event.mirror | |
let eventName = mirror.label | |
let params = mirror.params | |
// Track the event effortlessly | |
} | |
} | |
enum AppEvent: MirrorableEnum { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol MirrorableEnum {} | |
extension MirrorableEnum { | |
var mirror: (label: String, params: [String: Any]) { | |
get { | |
let reflection = Mirror(reflecting: self) | |
guard reflection.displayStyle == .enum, | |
let associated = reflection.children.first else { | |
return ("\(self)", [:]) | |
} | |
let values = Mirror(reflecting: associated.value).children |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Analytics { | |
static func track(event: AppEvent) { | |
switch event { | |
case .livestreamStarted: break | |
// Just tracking | |
case .liveStreamEnded(let duration): | |
let param = ["duration": duration] | |
// Then track the event with associated param | |
case .gameRoomCreated(let gameName, let invitedFriends): | |
let param = [ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct MyStruct: Comparable { | |
var a: Int | |
public static func <(lhs: MyStruct, rhs: MyStruct) -> Bool { | |
return lhs.a < rhs.a | |
} | |
} | |
extension ObservableType { | |
/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Observable.combineLatest(observable1.asObservable(), observable2.asObservable()) | |
.subscribe(onNext: { [weak self] (population, state) in | |
guard let _self = self else { return } | |
switch state { | |
case .connectedHost, .connectedParticipant: | |
if population == 0 { | |
_self.roomPopulationState = .waiting | |
} else { | |
_self.roomPopulationState = .inARoom | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PassthroughToWindowView: UIView { | |
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { | |
var view = super.hitTest(point, with: event) | |
if view != self { | |
return view | |
} | |
while !(view is PassthroughWindow) { | |
view = view?.superview | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PassthroughWindow: UIWindow { | |
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { | |
let view = super.hitTest(point, with: event) | |
return view == self ? nil : view | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PassthroughView: UIView { | |
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { | |
let view = super.hitTest(point, with: event) | |
return view == self ? nil : view | |
} | |
} |
NewerOlder