Avatar
@swift-main -Xfrontend -emit-sil -O protocol Observable { associatedtype Element func subscribe(_ f: @escaping (Element) -> Void) } struct Just<T>: Observable { typealias Element = T let value: T func subscribe(_ f: @escaping (T) -> Void) { f(value) } } struct Map<Source: Observable, U>: Observable { typealias Element = U let source: Source let transformer: (Source.Element) -> U func subscribe(_ f: @escaping (U) -> Void) { source.subscribe { [transformer] element in f(transformer(element)) } } } extension Observable { func map<U>(_ f: @escaping (Element) -> U) -> Map<Self, U> { return Map(source: self, transformer: f) } } Just(value: 122) .map { $0.description } .map { $0.count } .subscribe { element in print(element.description) }