Skip to content

Instantly share code, notes, and snippets.

View wotjd's full-sized avatar

wotjd wotjd

  • Seoul, South Korea
View GitHub Profile
@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)
@wotjd
wotjd / StateMachine.swift
Created March 20, 2021 17:31
combine based state machine - playground sample code
import UIKit
import Combine
class Store<Action: Equatable, State> {
var action = PassthroughSubject<Action, Never>()
private var cancelBag = Set<AnyCancellable>()
private var reducers: [(Action, State) -> State]
extension Array {
func mutateMap(_ transform: (inout Element) throws -> Void) rethrows -> Self {
var copy = self
for (index, _) in copy.enumerated() {
try transform(&copy[index])
}
return copy
}
func mutateMap<T>(_ keyPath: WritableKeyPath<Element, T>, _ value: T) -> Self {
var copy = self
import RxSwift
extension ObservableType {
public static func concatEager<S: Sequence>(_ sequence: S) -> Observable<Self.Element>
where S.Element == Observable<Self.Element> {
let multicastedObservables = sequence.map {
$0.multicast(ReplaySubject.createUnbounded())
}
var disposables: [Disposable] = []
@wotjd
wotjd / PoorConcatEager.swift
Created December 1, 2020 16:33
implement rxjava's concat eager in poor way
import RxSwift
let dispatchQueScheduler = ConcurrentDispatchQueueScheduler(qos: .background)
let observable1 = Observable<Int>
.just(1)
.do(
onSubscribe: { print("observable1 subscribed") },
onDispose: { print("observable1 disposed") }
)
import RxCocoa
@propertyWrapper
struct Behavior<T> {
private var relay: BehaviorRelay<T>
var wrappedValue: T {
get { self.relay.value }
set { self.relay.accept(newValue) }
}
import Foundation
@propertyWrapper
struct Converted<T: Decodable, U: RawRepresentable>: Decodable where T == U.RawValue {
private let baseValue: T
var wrappedValue: U? { U(rawValue: baseValue) }
var projectedValue: T { baseValue }
init(from decoder: Decoder) throws {