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 / 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 / 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 / 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 / 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 / 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 / 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 / write-file.clj
Created September 7, 2014 15:01
convert a file with word on each line to a json array. (pick only the first 2000 words)
(defn convert [](with-open [rdr (reader "/Users/zhaowang/workspace/clojure/labrepl/data/words")]
(doseq [line (take 2000 (line-seq rdr))]
(writeToFile (str "\"" line "\"" ",")))))
(defn writeToFile [line]
(with-open [wrtr (writer "test.json" :append true)]
(.write wrtr line)))
@zwang
zwang / getLargestFactor.clj
Last active August 29, 2015 14:06
To calculate the largest prime factor of a number
(ns scratch.core)
(defn dividable? [number]
#(zero? (mod number %))
)
(defn dividableByAny? [number divList]
(> (count (filter (dividable? number) divList)) 0)
)
@zwang
zwang / removeTrailingSpace
Last active December 23, 2015 11:09
Remove trailing white space in Eclipse
You can easily remove all the trailing whitespace within a single file by using
a regular expression with the Find/Replace dialog. Enter [ \t]+$ in the "Find:" box
and leave the "Replace with:" box empty. Check the "Regular expressions" option
then click "Replace All"
@zwang
zwang / center div.css
Last active December 19, 2015 22:09
Make the inner Div center horizontally and vertically in the outer div
<style>
#outer
{
background:red;
display:table;
width: 100%;
height:600px;
}
#inner
{