Skip to content

Instantly share code, notes, and snippets.

View TerryCK's full-sized avatar

Guan-Jhen (Terry) TerryCK

  • Taipei, Taiwan
View GitHub Profile
@TerryCK
TerryCK / ViewStack.swift
Last active January 7, 2022 12:40
a view stack in the enviroment by value semantic way
@main
struct MyApp: App {
// @State var viewStack: [ViewStack] = []
var body: some Scene {
WindowGroup {
HomeView()
// .environment(\.viewStack, $viewStack)
}
}
}
@TerryCK
TerryCK / funWithAnagrams.swift
Created December 19, 2021 06:40
Algorithms for Anagrams output
// brute force
func funWithAnagrams1(text: [String]) -> [String] {
var result = [String]()
var lookup = [Int: Set<Character>]()
for string in text {
if let set = lookup[string.count],
set.isSuperset(of: Array(string)) {
@TerryCK
TerryCK / await_async.swift
Created April 28, 2020 10:14
practice of await/async
struct Promise<T, Error: Swift.Error> {
let handler: (Result<T, Error>) -> Void
}
extension Promise where T == Payload {
func fire() -> Result<T, Error> {
var result: Result<T, Error>!
let semaphore = DispatchSemaphore(value: 0)
@TerryCK
TerryCK / SPI test.swift
Last active April 2, 2020 07:40
test for Graph QL for different of data model with same point of api
import UIKit
protocol Request {
associatedtype Payload: Codable
func fire(completion: (Result<Payload, Error>) -> Void)
var parameter: [String] { get }
}
protocol DataPayloadModel: Codable {
@TerryCK
TerryCK / Either.swift
Created December 12, 2019 09:06 — forked from pofat/Either.swift
Codable Either
enum Either<A, B> {
case left(A)
case right(B)
}
extension Either: Decodable where A: Decodable, B: Decodable {
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let a = try? container.decode(A.self) {
self = .left(a)
@TerryCK
TerryCK / tree.swift
Created November 13, 2019 11:03
tree by indirect enum implements
import Foundation
indirect enum Node<Element>: CustomStringConvertible {
case leaf(Element)
case child(left: Self?, element: Element, right: Self?)
var description: String {
switch self {
case let .child(left: _, element: element, right: _): return String(describing: element)
case let .leaf(element) : return String(describing: element)
}
@TerryCK
TerryCK / LinkedListbyIndirectEnum.swift
Last active November 13, 2019 10:07
LinkedListbyIndirectEnum.swift
import Foundation
indirect enum Node<Element>: CustomStringConvertible {
case end(Element)
case node(Element, Self)
var description: String {
switch self {
case .end(let element) : return String(describing: element)
case .node(let element, _): return String(describing: element)
/**
* Retrieves all the rows in the active spreadsheet that contain data and logs the
* values for each row.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
@TerryCK
TerryCK / .swift
Created November 3, 2018 04:45
cover flow for horizontal
private func updateCellAttributes(by attributes: UICollectionViewLayoutAttributes) {
var minX = collectionView!.bounds.minX + collectionView!.contentInset.left
let maxX = attributes.frame.origin.x
if minX > attributes.frame.origin.x + attributes.bounds.width + minimumLineSpacing + collectionView!.contentInset.left {
minX = 0
}
let finalX = max(minX, maxX)
var origin = attributes.frame.origin
let deltaX = (finalX - origin.x) / attributes.frame.width
@TerryCK
TerryCK / captureGroups.swift
Last active October 22, 2018 06:20
Regular expression captureGroups
import Foundation
struct Regex {
let pattern: String
}
protocol RegexMatchable : StringProtocol {
var regex: Regex { get }
func matches(regex: Regex, options: NSRegularExpression.Options) -> [String]
func capturedGroups(regex: Regex, options: NSRegularExpression.Options) -> [String]