Skip to content

Instantly share code, notes, and snippets.

View tarunon's full-sized avatar
🐌

tarunon tarunon

🐌
View GitHub Profile
import Foundation
// MARK: without cancel implementation
print("check without cancel implementation")
do {
print("#1 function")
func function() async -> Int {
return await withTaskCancellationHandler {
@tarunon
tarunon / notes.md
Created January 11, 2022 09:59
Carthage経由でXCFrameworkを生成し、SPM未対応のライブラリを利用する
carthage bootstrap --no-use-binaries --use-xcframeworks --use-submodules --platform iOS
mkdir Release
cd Carthage/Build
zip -r -X ../../Release/XXX.xcframework.zip XXX.xcframework
shasum -a 256 "XXX.xcframework.zip" >> checksum

cat checksum
@tarunon
tarunon / TaskContext.swift
Last active September 25, 2021 09:30
Swift Task Extension for slim AsyncSequence access.
import UIKit
class Base: UIViewController {
func mySomeAsyncSequence() -> AsyncStream<String> {
fatalError()
}
func use(_ value: String) {
print(value)
}
@tarunon
tarunon / Expressive.swift
Last active April 6, 2021 15:21
Introducing `if statement` `for statement` into swift. Required swift 5.4.
public enum Either<L, R> {
case left(L)
case right(R)
}
@resultBuilder
public struct Expressive<T> {
public static func buildBlock<C>() -> C? { nil }
public static func buildBlock<C>(_ component: C) -> C { component }
public static func buildEither(first component: T) -> T { component }
import Foundation
private class Weakbox {
weak var object: NSObjectProtocol?
init(_ object: NSObjectProtocol) {
self.object = object
}
}
class DelegateForwarder: NSObject {
@tarunon
tarunon / >>=.swift
Last active August 26, 2020 10:04
>>=.swift
// tryがResult.flatMapのdo記法であることの確認。
// 逆になるが、Result.flatMapから考えていったほうが理解しやすいのでその順番で。
func foo() -> Result<Int, Error> {
.success(1)
}
func bar(_ arg: Int) -> Result<Int, Error> {
.success(arg + 1)
}
@tarunon
tarunon / fbsample.swift
Created July 22, 2020 05:57
Traits flatMap with FB
struct Single<T> {}
struct Maybe<T> {}
protocol MaybeCov {
associatedtype T
func asMaybe() -> Maybe<T>
}
extension Single: MaybeCov {
func asMaybe() -> Maybe<T> { fatalError() }
public typealias PartialApplySelfToIsEqual = (Any?) -> Bool
public protocol MaybeEquatable {
func partialApplySelftoIsEqual() -> PartialApplySelfToIsEqual
}
extension MaybeEquatable {
public func partialApplySelftoIsEqual() -> PartialApplySelfToIsEqual {
{ _ in false }
}
}
private struct SwiftFuncWrapper {
var trampolinePtr: UnsafeMutablePointer<UInt64>
var functionObject: UnsafeMutablePointer<SwiftFuncObject>
var functionPtr: UnsafeMutableRawPointer {
let pointer = UnsafeMutablePointer<UInt64>(bitPattern: UInt(functionObject.pointee.address))!
// Getting actual function ptr from instruction.
// 0: 55 push rbp
// 1: 48 89 e5 mov rbp,rsp
// 4: 5d pop rbp
@tarunon
tarunon / Image.swift
Created October 4, 2018 06:48
How to solve Bundle things in microframeworks
public struct BundleReference<T: NSObjectProtocol> {
static var _bundle: Bundle {
return Bundle(for: T.self)
}
}
public protocol BundleReferencable {
associatedtype Referenced: NSObjectProtocol
static var bundle: BundleReference<Referenced>.Type { get }