Skip to content

Instantly share code, notes, and snippets.

@wotjd
wotjd / compositional_waterfall_layout.swift
Created October 26, 2022 11:37
a simple implementation of waterfall layout using compositional layout
import UIKit
extension UICollectionViewCompositionalLayout {
static func waterfall(
columnCount: Int,
interItemSpacing: CGFloat = 0,
lineSpacing: CGFloat = 0,
itemCountProvider: @escaping (Int) -> Int,
itemSizeProvider: @escaping (IndexPath) -> CGSize,
sectionConfigurator: @escaping (inout NSCollectionLayoutSection) -> Void = { _ in },
import Foundation
import Combine
// shareWhileConnected using Combine Proof of Concept
extension Publisher {
func shareWhileConnected() -> Publishers.ShareWhileConnected<Self, PassthroughSubject<Output, Failure>> {
.init(upstream: self)
}
}
@wotjd
wotjd / ASAuthorizationController+Combine.swift
Created October 4, 2022 17:38
Sign in with Apple + Combine (CombineCocoa)
import AuthenticationServices
import Combine
import CombineCocoa
extension ASAuthorizationController {
private var delegateProxy: AuthorizationControllerDelegateProxy {
.createDelegateProxy(for: self)
}
var didCompleteWithAuthroizationPublisher: AnyPublisher<ASAuthorization, Never> {
@wotjd
wotjd / AsyncStream+Extension.swift
Last active September 2, 2022 11:42
some utility operators for AsyncStream inspired by RxSwift
import Foundation
extension AsyncStream {
init(
_ elementType: Element.Type = Element.self,
bufferingPolicy limit: AsyncStream<Element>.Continuation.BufferingPolicy = .unbounded,
_ build: @escaping (AsyncStream<Element>.Continuation) async -> Void
) {
self = AsyncStream(elementType, bufferingPolicy: limit) { continuation in
Task { await build(continuation) }
// poor arrow function expression implementation like javascript's using infix operator and closure
precedencegroup ArrowFunctionExpressionPrecedence {
associativity: left
higherThan: BitwiseShiftPrecedence
}
infix operator =>: ArrowFunctionExpressionPrecedence
infix operator ==>: ArrowFunctionExpressionPrecedence
@wotjd
wotjd / keypath_optional.swift
Created August 5, 2021 16:31
test keypath expression for optional `.?.`
var greeting: String? = "Hello, playground"
class Wrapper<T> {
var value: T
init(initialValue: T) {
self.value = initialValue
}
func transform<V>(_ transform: (T) -> V) -> V {
@wotjd
wotjd / layout.kbd.json
Last active May 3, 2021 15:05 — forked from yaoferrari/layout.kbd.json
Untitled Keyboard Layout
[
[
{
"a": 7
},
"",
"",
{
"x": 0.25,
"a": 0
struct NotePage {
var title: String
var contents: String
}
class NoteCover {
var page: NotePage
init(page: NotePage) {
self.page = page
@wotjd
wotjd / enum_protocol_switch.swift
Created April 1, 2021 14:56
limitation of Enum cases as protocol witnesses (SE-0280)
protocol Enumable {
static func integerHolder(_: Int) -> Self
}
// SE-0280
enum Value: Enumable {
case integerHolder(Int)
}
let someValue = Value.integerHolder(1)