Skip to content

Instantly share code, notes, and snippets.

View zhxnlai's full-sized avatar

Zhixuan Lai zhxnlai

  • Square Inc
  • San Francisco, CA
View GitHub Profile
@zhxnlai
zhxnlai / AsyncTask3.swift
Last active June 15, 2016 09:05
Async programming in swift 5
// async
encrypt(message).async { ciphertext in /* do somthing */ }
get(URL).async {(data, response, error) in /* do somthing */ }
// await
let ciphertext = encrypt(message).await()
let (data, response, error) = get(URL).await()
@zhxnlai
zhxnlai / AsyncTask2.swift
Created June 15, 2016 09:02
Async programming in swift 4
// synchronous API
func encrypt(message: String) -> Task<String> {
return Task {
encrypt(message)
}
}
// asynchronous API
func get(URL: NSURL) -> Task<(NSData?, NSURLResponse?, NSError?)> {
return Task {completionHandler in
NSURLSession().dataTaskWithURL(URL, completionHandler: completionHandler).resume()
@zhxnlai
zhxnlai / AsyncTask.swift
Created June 15, 2016 07:48
Async programming in swift 3
Task {
let enhancedImage = self.applyImageFilter(image)
Task {self.imageView.image = enhancedImage}.async(.Main)
let completed = UIView.animateWithDurationAsync(0.3) { self.label.alpha = 1 }.await(.Main)
// add code to happen next here
}.async()
@zhxnlai
zhxnlai / PromiseKit.swift
Created June 15, 2016 07:47
Async programming in swift 2
dispatch_promise {
return self.applyImageFilter(image)
}.then { enhancedImage in
self.imageView.image = enhancedImage
return UIView.animateWithDurationPromise(0.3) {
self.label.alpha = 1
}
}.then {
// add code to happen next here
}
@zhxnlai
zhxnlai / dispatch_async.swift
Last active June 15, 2016 08:19
Async programming in swift
// get a global concurrent queue
let queue = dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0)
// submit a task to the queue for background execution
dispatch_async(queue) {
let enhancedImage = self.applyImageFilter(image) // expensive operation taking a few seconds
// update UI on the main queue
dispatch_async(dispatch_get_main_queue()) {
self.imageView.image = enhancedImage
UIView.animateWithDuration(0.3, animations: {
self.imageView.alpha = 1
@zhxnlai
zhxnlai / Problem 1.js
Last active January 25, 2016 17:12
Vegenere cipher
function getCharCodes(s) {
return s.toUpperCase().split("").map(c => c.charCodeAt(0))
}
function zip(a, b) {
return a.map((char,i) => [char, b[i % b.length]]);
}
function decrypt(ciphertext, key) {
const plaintextCharCode = zip(getCharCodes(ciphertext), getCharCodes(key)).map(pair => (pair[0] - pair[1] + 26) % 26 + "A".charCodeAt(0))
@zhxnlai
zhxnlai / index.js
Last active December 4, 2015 01:12
A script that stimulates mouse events to remove all of your photos on Google Photos. (copy and paste to the console at https://photos.google.com/)
function click(el) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
el.dispatchEvent(evt);
}
function click2(el) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("mousedown", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
@zhxnlai
zhxnlai / final.md
Last active August 29, 2015 14:22
115A notes

Math 115A

Chapter 1

1.2-1.6

Definitions

  • Definition of a vector space

@zhxnlai
zhxnlai / project5
Created May 17, 2015 00:05
Project 5
// stores an array of images (PImage[]) for each scene
ArrayList<PImage[]> scenes;
// keeps track of the index of the current scene
int sceneIndex = 0;
// used to animate the images of the current scene
int frameIndex = 0;
// keep track of the last mouse position
float clickedX;
float clickedY;
@zhxnlai
zhxnlai / vc.sh
Created April 21, 2015 21:25
Video Conversion
# To pngs 1 frame per second
# https://trac.ffmpeg.org/wiki/Create%20a%20thumbnail%20image%20every%20X%20seconds%20of%20the%20video
mkdir frames && ffmpeg -i video.mov -vf fps=1 frames/ffout%03d.png
# screen recording to gif
mkdir bufferFrames && ffmpeg -i video.mov -vf scale=320:-1 -r 30 bufferFrames/ffout%03d.png && convert -delay 4.167 -loop 0 bufferFrames/ffout*.png ZLHistogramAudioPlotBuffer.gif