Skip to content

Instantly share code, notes, and snippets.

View TerryCK's full-sized avatar

Guan-Jhen (Terry) TerryCK

  • Taipei, Taiwan
View GitHub Profile
@ViewBuilder
private func build<#name#>(_ viewStore: ViewStoreType) -> some View {
}
@pofat
pofat / DelegatePublisher.swift
Last active August 9, 2022 04:42
How to handle delegate in a reactive way? Turn a delegate into Publisher.
import Combine
import CombineExt // Check this implementation: https://github.com/CombineCommunity/CombineExt/blob/main/Sources/Operators/Create.swift
import CoreBluetooth
// Client to generate target delegate publisher
// Learned from https://github.com/pointfreeco/composable-core-location/blob/main/Sources/ComposableCoreLocation/Live.swift
struct PeripheralClient {
enum Action: Equatable {
case didUptateName
public typealias CompletionHandler = Function<Void, Void>
public typealias SideEffect<Input> = Function<Input, Void>
public struct Function<Input, Output>: Identifiable {
public let id: String
internal let f: (Input) -> Output
public init(
id: String,
@pofat
pofat / Either.swift
Created November 14, 2019 15:09
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)
// The SwiftUI Lab
// Website: https://swiftui-lab.com
// Article: https://swiftui-lab.com/alignment-guides
import SwiftUI
struct ContentView: View {
@State var position: Int = 0
var body: some View {
@AliSoftware
AliSoftware / Bindings.swift
Last active July 9, 2024 22:02
Re-implementation of @binding and @State (from SwiftUI) myself to better understand it
/*:
This is a concept re-implementation of the @Binding and @State property wrappers from SwiftUI
The only purpose of this code is to implement those wrappers myself
just to understand how they work internally and why they are needed,
⚠️ This is not supposed to be a reference implementation nor cover all
subtleties of the real Binding and State types.
The only purpose of this playground is to show how re-implementing
them myself has helped me understand the whole thing better
@tclementdev
tclementdev / libdispatch-efficiency-tips.md
Last active July 12, 2024 03:33
Making efficient use of the libdispatch (GCD)

libdispatch efficiency tips

The libdispatch is one of the most misused API due to the way it was presented to us when it was introduced and for many years after that, and due to the confusing documentation and API. This page is a compilation of important things to know if you're going to use this library. Many references are available at the end of this document pointing to comments from Apple's very own libdispatch maintainer (Pierre Habouzit).

My take-aways are:

  • You should create very few, long-lived, well-defined queues. These queues should be seen as execution contexts in your program (gui, background work, ...) that benefit from executing in parallel. An important thing to note is that if these queues are all active at once, you will get as many threads running. In most apps, you probably do not need to create more than 3 or 4 queues.

  • Go serial first, and as you find performance bottle necks, measure why, and if concurrency helps, apply with care, always validating under system pressure. Reuse

@willard1218
willard1218 / Intro.md
Last active March 1, 2018 12:16
Protocol-Oriented Programming — Protocol Extension
We couldn’t find that file to show.
@tkdmaf
tkdmaf / new_string_state.swift
Last active December 27, 2017 05:55
Playground get state string
import UIKit
enum MyContain {
case before
case after
case between
}
@willard1218
willard1218 / pop.swift
Last active October 9, 2017 15:58
Rewrite code in functional programming
// source : https://github.com/TerryCK/Protocol-Oriented-Programming
extension Collection where Iterator.Element == Int {
func countOddEven() -> (odd: Int, even: Int) {
return self.reduce((0, 0)) { (tuple, number) -> (odd: Int, even: Int) in
return (number % 2 == 0) ?
(tuple.0, tuple.1 + 1) :
(tuple.0 + 1,tuple.1)
}