 }
 
 protocol EpicType: MiddlewareType {
-
   associatedtype EmitActionType
 
   init(emitter: ActionEmitter)
 }
 
 protocol UiComponent {
-
   associatedtype StateType
 
   init(source: Observable<StateType>, emitter: ActionEmitter, state: StateType)
 }
 
 class ActionEmitter {
-
   let observable: Observable<ReduxTypes.ActionType>
 
   init() {
-    self.observable = self.subject.asObservable().observeOn(scheduler)
-  }
-
-  func typedEmit<T>() -> ((T) -> Void) {
-    return { (action: T) in
-      self.subject.onNext(action)
-    }
-  }
-
-  func terminate() {
-    self.subject.onCompleted()
-  }
-
-  deinit {
-    self.subject.onCompleted()
-  }
+    self.observable = self.subject.asObservable().observeOn(self.scheduler)
+  }
+
+  func typedEmit<T>() -> (T) -> Void {
+    { (action: T) in self.subject.onNext(action) }
+  }
+
+  func terminate() { self.subject.onCompleted() }
+
+  deinit { self.subject.onCompleted() }
 
   private let scheduler = SerialDispatchQueueScheduler(qos: .userInteractive)
   private let subject = PublishSubject<ReduxTypes.ActionType>()
 }
 
 class ReduxContext {
-
   let actionEmitter = ActionEmitter()
   let stateSource: Observable<ReduxTypes.StateType>
 
-  convenience init(initialState: ReduxTypes.StateType,
-                   reducers: [ReduxTypes.ReduceFunction],
-                   middlewares: [(@escaping ReduxTypes.ReduceFunction) -> ReduxTypes.ReduceFunction] = []) {
-
+  convenience init(
+    initialState: ReduxTypes.StateType,
+    reducers: [ReduxTypes.ReduceFunction],
+    middlewares: [(@escaping ReduxTypes.ReduceFunction) -> ReduxTypes
+      .ReduceFunction] = []
+  ) {
     self.init(initialState: initialState)
 
     self.actionEmitter.observable

}

protocol MiddlewareType {

  associatedtype StateType
  associatedtype ActionType

  typealias ReduceTuple = (state: StateType, action: ActionType, modified: Bool)
  typealias ActionTypeErasedReduceTuple = (state: StateType, action: ReduxTypes.ActionType, modified: Bool)

  typealias TypedActionReduceFunction = (ReduceTuple) -> ActionTypeErasedReduceTuple
  typealias ActionTypeErasedReduceFunction = (ActionTypeErasedReduceTuple) -> ActionTypeErasedReduceTuple

  func typedApply(_ reduce: @escaping TypedActionReduceFunction) -> TypedActionReduceFunction
}

extension MiddlewareType {

  func apply(_ reduce: @escaping ActionTypeErasedReduceFunction) -> ActionTypeErasedReduceFunction {
    return { tuple in
      guard let typedTuple = tuple as? ReduceTuple else {
        return reduce(tuple)
      }

      let typedReduce: (ReduceTuple) -> ActionTypeErasedReduceTuple = { typedTuple in
        // We know that we can cast the typed action to ReduxTypes.ActionType
        return reduce((state: typedTuple.state, action: typedTuple.action, modified: typedTuple.modified))
      }

      return self.typedApply(typedReduce)(typedTuple)

}

extension Observable {
  func reduce(
    by reducers: [(Element) -> Element],
    middlewares: [(@escaping (Element) -> Element) -> (Element) -> Element]
  ) -> Observable<Element> {
    let dispatch = { pair in
      reducers.reduce(pair) { result, reduceBody in reduceBody(result) }
    }

    let next = middlewares.reversed().reduce(dispatch) { result, middleware in middleware(result) }
