Skip to content

Instantly share code, notes, and snippets.

View weihanglo's full-sized avatar

Weihang Lo weihanglo

View GitHub Profile
@weihanglo
weihanglo / xitou_gis.R
Last active August 28, 2016 04:12
Generate SpatialPointDataFrame based on dataset in Xitou
#!/usr/bin/env Rscript
#
# ----- For NTU Forestry 3049 Course Only -----
#
# Generate SpatialPointDataFrame based on dataset in Xitou
#
# This Gist: https://git.io/v6ozP
#
# Mail: b01605002@ntu.edu.tw
# Author: Weihang Lo
@weihanglo
weihanglo / .swiftlint.yml
Last active December 24, 2016 15:46
Configurations of iOS Project
opt_in_rules:
- closure_end_indentation
- closure_spacing
- empty_count
- force_unwrapping
- missing_docs
- operator_usage_whitespace
- private_outlet
- redundant_nil_coalescing
- overridden_super_call
@weihanglo
weihanglo / inline-worker.js
Created June 17, 2017 00:38
Inline web worker
const blobURL = URL.createObjectURL(new Blob([ '(',
function () {
function fibonacci () {}
onmessage = function (ev) {
const result = processData(ev.data)
postMessage(result)
}
}.toString(),
@weihanglo
weihanglo / cancellableGet.js
Created July 2, 2017 08:18
Simple cancellable GET request promise
/**
* GET request with a cancelable token
* @param {string} url - url to request
* @param {objecg} token - an object with a `cancel` function property.
* @return {Promise}
*/
export function cancellableGet (url, token) {
const xhr = new XMLHttpRequest()
return new Promise(function (resolve, reject) {
token.cancel = function () {
@weihanglo
weihanglo / getClientOffset.js
Created July 2, 2017 08:19
Get real element offset with an offsetParent referencing element
/**
* Get real element offset with an offsetParent referencing element
*
* `getBoundingClientRect` return values is not correct under CSS multi-column
* layout, so we recursively get `offsetLeft`/`offsetTop` instead.
* @param {HTMLElement} el - element of interest
* @param {HTMLElement} stopEl - one of the offsetParent as a reference
* @return {object} object wth top and left offset
*/
export function getClientOffset (el, stopEl) {
@weihanglo
weihanglo / getRenderedRect.js
Created July 13, 2017 07:08
Calculate the rendered rect of img content (useful with object-fit: contain)
/**
* Calculate the rendered rect of img content (useful with object-fit: contain)
* @param {HTMLImageElement} img
* @return {object} rect of the object (the coordinate origin is top-left).
*/
export function getRenderedRect (img) {
const style = window.getComputedStyle(img)
const pos = style.getPropertyValue('object-position').split(' ')
const contains = style.getPropertyValue('object-fit') === 'contain'
@weihanglo
weihanglo / swift-error-handling.md
Last active August 1, 2017 03:45
Understanding Error Handling in Swift

Understanding Error Handling in Swift

如何利用 Swift 的語言特性來處理例外?使用 Optional 是常見的做法。如果成功就返回 value,失敗則返回 nil,這種模式常用於簡單的狀況。然而,面對複雜的情況,例如網路請求,若只簡單返回 nil,調用者並無法得知是 404,抑或 500。為了解決這個問題,我們必須緊緊抱住[錯誤/例外處理][wiki-exception-handling]的大腿。

(撰於 2017-04-10,基於 Swift 3.1)

@weihanglo
weihanglo / intro-rx-1-iterator-pattern.md
Last active August 20, 2017 05:06
Intro Rx - 1. Iterator Pattern

Intro Rx - 1. Iterator Pattern

本篇介紹 Rx 的重要基礎概念 Iterator pattern

(撰於 2017-08-15,基於 Swift 3.1)

Definition

@weihanglo
weihanglo / intro-rx-2-observer-pattern.md
Last active August 20, 2017 05:27
Intro Rx - 2. Observer Pattern

Intro Rx - 2. Observer Pattern

本篇介紹 Rx 另一個重要的基礎概念 Observer pattern

(撰於 2017-08-15,基於 Swift 3.1)

Definition

@weihanglo
weihanglo / intro-rx-0-reactivex.md
Last active August 22, 2017 12:52
Intro of ReactiveX

Intro of ReactiveX

聽過 Reactive Programming 嗎?ReactiveX(Rx)是近來火紅的技術,帶動函數響應式程式設計的熱潮。本系列將從 Rx 最原始的概念解釋起,一步步認識 Rx 巧妙的設計理念。期盼讀完後,人人心中都能有 Reactive 的思維!

(撰於 2017-08-15)