Skip to content

Instantly share code, notes, and snippets.

View zwang's full-sized avatar
😀
Plato App

Zhao Wang zwang

😀
Plato App
View GitHub Profile
@zwang
zwang / tracing.clj
Created September 27, 2014 06:19
clojure marco to enable code tracing
; From Clojure: the JFDI language by Michael O. Church
; https://docs.google.com/presentation/d/16ccLdZ6Epp6Nu-lMansEErwAjbN4sau4LztaARmdl_k/edit#slide=id.g1793dde48_1508
; Motivation: runtime tracing of functions is a powerful debug technique.
; It’d require a framework to do this in Java, but in Clojure, a macro suffices!
(defmacro tracing [code]
`(do
(println "The code was: " '~code)
@zwang
zwang / eval ex.clj
Created October 2, 2014 04:05
HackerRank evaluate e^x
(dotimes [n (read-string (read-line))]
(let [x (read-string (read-line)),
ex (fn [x]
(inc
(reduce +
(map #(/ (reduce * (repeat % x))
(reduce * (range 1 (inc %)))
)
(range 1 10)
)
@zwang
zwang / SystemLog.swift
Last active August 29, 2015 14:26 — forked from kristopherjohnson/SystemLog.swift
Demo of using the Apple System Log (ASL) API from Swift
// Note: This must be used in an Xcode project that contains a bridging header
// that includes <asl.h>
import Foundation
/// Provides high-level methods to access the raw data in
/// an ASL message.
struct SystemLogEntry {
/// Key-value pairs read from ASL message
let data: [String : String]
@zwang
zwang / simulator_populator
Last active August 29, 2015 14:26 — forked from cabeca/simulator_populator
This script removes and recreates all simulators in Xcode 6.
#!/usr/bin/env ruby
device_types_output = `xcrun simctl list devicetypes`
device_types = device_types_output.scan /(.*) \((.*)\)/
runtimes_output = `xcrun simctl list runtimes`
runtimes = runtimes_output.scan /(.*) \(.*\) \((com.apple[^)]+)\)$/
devices_output = `xcrun simctl list devices`
devices = devices_output.scan /\s\s\s\s(.*) \(([^)]+)\) (.*)/
@zwang
zwang / loadFont.swift
Last active March 23, 2022 12:33
Dynamically load font from a font file in swift in IOS
static func loadFont(fontName: String, baseFolderPath: String) -> Bool {
let basePath = baseFolderPath as NSString
let fontFilePath = basePath.stringByAppendingPathComponent(fontName)
let fontUrl = NSURL(fileURLWithPath: fontFilePath)
if let inData = NSData(contentsOfURL: fontUrl) {
var error: Unmanaged<CFError>?
let cfdata = CFDataCreate(nil, UnsafePointer<UInt8>(inData.bytes), inData.length)
if let provider = CGDataProviderCreateWithCFData(cfdata) {
if let font = CGFontCreateWithDataProvider(provider) {
if (!CTFontManagerRegisterGraphicsFont(font, &error)) {
@zwang
zwang / JavascriptCorePerfTest.swift
Created December 11, 2015 08:51
Test the performance of functions calls in JavaScriptCore in iOS 8+
class JSCContextTests: XCTestCase {
var jscContext: JSContext!
override func setUp() {
jscContext = JSContext()
let log_Binder: @convention(block) String -> Void = { input in
print(input)
@zwang
zwang / DoubleToStringWithMoreDecimals.swift
Last active March 18, 2016 20:04
Convert double to string in swift with more than default 15 decimals (a bug in swift should be fixed later)
// https://github.com/apple/swift/pull/348
private func getDoubleStrWith16DecimalDigits(value: Double) -> String {
return String(format: "%.16f", value)
}
@zwang
zwang / iosVersionRelatedBugFix.swift
Last active March 12, 2016 01:55
iOS 9.0.x bug automaticallyAdjustsScrollViewInsets doesn't change insets on iOS 9
#if os(iOS)
// temp fix for iOS 9.0.x http://www.openradar.me/22106545
let os = NSProcessInfo().operatingSystemVersion
switch (os.majorVersion, os.minorVersion, os.patchVersion) {
case (9, 0, _):
// "iOS >= 9.0.0, < 9.1.0"
// Only in iOS 9.0.x, the contentInset of the collection view does not automatically add the statusbar height and the navigation bar height
let statusBarheight = UIApplication.sharedApplication().statusBarFrame.height
let navigationBarHeight = self.navigationController?.navigationBar.frame.height ?? 0
self.constants.defaultContentInsets.top += statusBarheight + navigationBarHeight
@zwang
zwang / sqlite3.sh
Created March 15, 2016 20:11 — forked from fluidsonic/sqlite3.sh
Make sqlite3 module available in Swift
#!/bin/sh
modulesDirectory=$DERIVED_FILES_DIR/modules
modulesMap=$modulesDirectory/module.modulemap
modulesMapTemp=$modulesDirectory/module.modulemap.tmp
mkdir -p "$modulesDirectory"
cat > "$modulesMapTemp" << MAP
module sqlite3 [system] {
@zwang
zwang / two lines title for navigationbar.swift
Created October 20, 2016 01:00
two lines title for navigationbar
//http://stackoverflow.com/questions/2422383/uinavigationbar-multi-line-title
private func setupTitleView() {
let topText = NSLocalizedString("key", comment: "")
let bottomText = NSLocalizedString("key", comment: "")
let titleParameters = [NSForegroundColorAttributeName : UIColor.<Color>(),
NSFontAttributeName : UIFont.<Font>]
let subtitleParameters = [NSForegroundColorAttributeName : UIColor.<Color>(),
NSFontAttributeName : UIFont.<Font>]