Skip to content

Instantly share code, notes, and snippets.

@dolphinsue319
dolphinsue319 / pgoRobot.py
Created August 14, 2016 15:22
pgoapi demo
from pgoapi import pgoapi
from s2sphere import Cell, CellId, LatLng
import time
def get_cell_ids(lat, long, radius = 10):
origin = CellId.from_lat_lng(LatLng.from_degrees(lat, long)).parent(15)
walk = [origin.id()]
right = origin.next()
@dolphinsue319
dolphinsue319 / AudioUnitDemo
Last active December 9, 2016 09:31
Learn AudioUnit from live demo in WWDC 2016 session 507.
//
// main.swift
// SquareWaveGenerator
//
// Created by dolphin on 2016/8/7.
// Copyright © 2016年 dolphin. All rights reserved.
//
import Foundation
import AudioToolbox
@dolphinsue319
dolphinsue319 / Pattern matching final result
Last active October 10, 2015 10:53
Pattern matching 最後的結果
/*
第一個參數是一個會輸出 Bool 的函數,也就是 case 後面接的東西。
第二個參數會被放入第一個函數型態的參數中,藉以輸出一個 Bool 值
*/
func ~=<T>(pattern: T->Bool, value: T) -> Bool {
return pattern(value)
}
func greaterThan<T: Comparable>(a:T)(b: T) -> Bool {
@dolphinsue319
dolphinsue319 / Generic parameter with protocol example
Created October 10, 2015 09:56
指定泛型參數必須是實作了某種 protocol 的例子
func ~=<T: Comparable, U: Equatable>(pattern: T, value: U) -> Bool {
return true
}
@dolphinsue319
dolphinsue319 / Simple example for ~=
Created October 10, 2015 09:03
~= 的簡單範例
//pattern 是 case 後接的參數,value 是 switch 後面接的參數
func ~=(pattern: String, value: Int) -> Bool {
return pattern == "\(value)"
}
//這個會被代入 ~= 函數裡的 value
let x = 1
switch x {
//這個會被代入 ~= 函數裡的 pattern
case "1":
@dolphinsue319
dolphinsue319 / Pattern matching result in method
Created October 10, 2015 08:48
我們最後希望可以得到這樣的結果
let x: Int = 1
switch x {
case greaterThan(0):
print("positive")
case lessThan(0):
print("negative")
default:
print("0")
}
@dolphinsue319
dolphinsue319 / Using switch syntax to solve <>== 0 question
Last active October 10, 2015 08:44
用 switch 語法解決 <>== 0 的問題
let x = 1
switch x {
case _ where x > 0:
print("> 0")
case _ where x < 0:
print("< 0")
default:
print("== 0")
}
@dolphinsue319
dolphinsue319 / Common way to solve <, >, == 0
Last active October 10, 2015 08:39
常見的大於、小於及等於零的寫法
let x = 1
if x > 0 {
print("> 0")
} else if x < 0 {
print("< 0")
} else {
print("== 0")
}
@dolphinsue319
dolphinsue319 / Clean your derived data
Created September 16, 2015 11:47
用 script 清 derived data
//在 ~/.bash_profile 放這個:
alias nuke="rm -rf ~/Library/Developer/Xcode/DerivedData/"
@dolphinsue319
dolphinsue319 / What Siri says
Last active September 12, 2015 03:17
解密Siri 講的那串1、0
import Foundation
// siri 說出來的2進位字串
let whatSirirSays = ["11100110", "10101001", "10011111", "11100101", "10101111", "10000110"]
extension Int8 {
init(string str: String){
var rtn: Int8 = 0
var i: Int = 0
for anUnicodeScalar in str.unicodeScalars{
//將字串型態的單一 bit 轉成 00000000, 或 00000001