Skip to content

Instantly share code, notes, and snippets.

View phucnm's full-sized avatar
😬
eager to learn

Tony Nguyen phucnm

😬
eager to learn
  • Vancouver, BC, Canada
View GitHub Profile
@phucnm
phucnm / broken_gzip.py
Last active June 30, 2020 06:10
Broken gzip, whereas bzip2 is still stable.
'''
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
@phucnm
phucnm / RxCFNotification.swift
Created November 19, 2019 03:22
Rx extension for CFNotification
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?,
@phucnm
phucnm / tracking-usage.swift
Created March 9, 2019 20:38
TrackingUsage
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 {
@phucnm
phucnm / MirrorableEnum.swift
Created March 9, 2019 20:28
MirrorableEnum
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
@phucnm
phucnm / verboseenum.swift
Created March 9, 2019 20:17
EnumReflection
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 = [
@phucnm
phucnm / rxswift-min-operator.swift
Created June 28, 2018 12:43
rxswift-min-value
struct MyStruct: Comparable {
var a: Int
public static func <(lhs: MyStruct, rhs: MyStruct) -> Bool {
return lhs.a < rhs.a
}
}
extension ObservableType {
/**
@phucnm
phucnm / rxleak.swift
Last active June 1, 2018 14:47
Rx Leak
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
}
@phucnm
phucnm / PassthroughViewToWindow.swift
Created October 9, 2017 09:16
PassthroughViewToWindow
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
}
@phucnm
phucnm / PassthroughWindow.swift
Created October 9, 2017 08:52
PassthorughWindow
class PassthroughWindow: UIWindow {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let view = super.hitTest(point, with: event)
return view == self ? nil : view
}
}
@phucnm
phucnm / PassthroughView.swift
Created October 9, 2017 08:08
PassthroughView
class PassthroughView: UIView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let view = super.hitTest(point, with: event)
return view == self ? nil : view
}
}