Created
March 14, 2018 10:00
-
-
Save tarunon/06e153b4e7c11630826eadcbd8f6462d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// とりあえず各ケースのOptional変換を作っておく。ここはPrivateでも良い。 | |
extension Enum2Convertible { | |
func ifCase0() -> Case1? { | |
switch self.asEnum() { | |
case .case0(let x): return x | |
default: return nil | |
} | |
} | |
func ifCase1() -> Case1? { | |
switch self.asEnum() { | |
case .case0(let x): return x | |
default: return nil | |
} | |
} | |
} | |
/// 川変換をOptional変換を利用して作る | |
extension Observable where E: Enum2Convertible { | |
func case0() -> Observable<E.Case0> { | |
return flatMap { .from($0.ifCase0()) } | |
} | |
func case1() -> Observable<E.Case1> { | |
return flatMap { .from($0.ifCase1()) } | |
} | |
} | |
/// 全ケース処理を強制するbindを川変換を利用して作る | |
extension Observable where E: Enum2Convertible { | |
func bind(to binder0: (Observable<E.Case0>) -> Disposable, _ binder1: (Observable<E.Case1>) -> Disposable) -> Disposable { | |
return bind { `self` in | |
let d0 = binder0(self.case0()) | |
let d1 = binder1(self.case1()) | |
return Disposables.create(d0, d1) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment