ronantakizawa/codereview-bench huggingface dataset:
Repository: ReactiveX/RxSwift
File: Tests/RxSwiftTests/PrimitiveSequenceTest.swift (Swift)

Code before:
        func testAsSingleReleasesResourcesOnComplete() {
            _ = Observable<Int>.just(1).asSingle().subscribe({ _ in })
        }

        func testAsSingleReleasesResourcesOnError1() {
        _ = Observable<Int>.error(testError).asSingle().subscribe({ _ in })
        }

        func testAsSingleReleasesResourcesOnError2() {
        _ = Observable<Int>.of(1, 2).asSingle().subscribe({ _ in })
        }
    #endif
}

extension PrimitiveSequenceTest {
    func testFirst_Empty() {
        let scheduler = TestScheduler(initialClock: 0)

        let xs = scheduler.createHotObservable([
            next(150, 1),
            completed(250),
            error(260, testError)
            ])

        let res: TestableObserver<Int> = scheduler.start { () -> Observable<Int> in
            let single: Single<Int> = xs.first().map { $0 ?? -1 }
            return single.asObservable()
        }

        XCTAssertEqual(res.events, [
            next(250, -1),
            completed(250)
            ])

        XCTAssertEqual(xs.subscriptions, [
            Subscription(200, 250)
            ])
    }

    func testFirst_One() {
        let scheduler = TestScheduler(initialClock: 0)

        let xs = scheduler.createHotObservable([
            next(150, 1),
            next(210, 2),
            completed(250),
            error(260, testError)
            ])

        let res = scheduler.start { () -> Observable<Int> in
            let single: Single<Int> = xs.first().map { $0 ?? -1 }

Review comment (suggestion):
I think we would should make this `let single: Single<Int?> = xs.first()`.

Code after:
        func testAsSingleReleasesResourcesOnComplete() {
            _ = Observable<Int>.just(1).asSingle().subscribe({ _ in })
        }

        func testAsSingleReleasesResourcesOnError1() {
        _ = Observable<Int>.error(testError).asSingle().subscribe({ _ in })
        }

        func testAsSingleReleasesResourcesOnError2() {
        _ = Observable<Int>.of(1, 2).asSingle().subscribe({ _ in })
        }
    #endif
}

extension PrimitiveSequenceTest {
    func testFirst_Empty() {
        let scheduler = TestScheduler(initialClock: 0)

        let xs = scheduler.createHotObservable([
            next(150, 1),
            completed(250),
            error(260, testError)
            ])

        let res: TestableObserver<Int?> = scheduler.start { () -> Observable<Int?> in
            let single: Single<Int?> = xs.first()
            return single.asObservable()
        }

        XCTAssertEqual(res.events, [
            next(250, nil),
            completed(250)
            ])

        XCTAssertEqual(xs.subscriptions, [
            Subscription(200, 250)
            ])
    }

    func testFirst_One() {
        let scheduler = TestScheduler(initialClock: 0)

        let xs = scheduler.createHotObservable([
            next(150, 1),
            next(210, 2),
            completed(250),
            error(260, testError)
            ])

        let res = scheduler.start { () -> Observable<Int?> in
            let single: Single<Int?> = xs.first()
Repository: ReactiveX/RxSwift
File: Tests/RxSwiftTests/PrimitiveSequenceTest.swift (Swift)

Code before:
            ])

        let res = scheduler.start { () -> Observable<Int> in
            let single: Single<Int> = xs.first().map { $0 ?? -1 }
            return single.asObservable()
        }

        XCTAssertEqual(res.events, [
            error(210, testError)
            ])

        XCTAssertEqual(xs.subscriptions, [
            Subscription(200, 210)
            ])
    }
}

extension PrimitiveSequenceTest {
    func testAsMaybe_Empty() {
        let scheduler = TestScheduler(initialClock: 0)

        let xs = scheduler.createHotObservable([
            next(150, 1),
            completed(250),
            error(260, testError)
            ])

        let res = scheduler.start { () -> Observable<Int> in
            let maybe: Maybe<Int> = xs.asMaybe()
            return maybe.asObservable()
        }

        XCTAssertEqual(res.events, [
            completed(250)
            ])

        XCTAssertEqual(xs.subscriptions, [
            Subscription(200, 250)
            ])
    }

    func testAsMaybe_One() {
        let scheduler = TestScheduler(initialClock: 0)

        let xs = scheduler.createHotObservable([
            next(150, 1),
            next(210, 2),
            completed(250),
            error(260, testError)
            ])


Review comment (suggestion):
We are missing 
```
#if TRACE_RESOURCES
        func testFirstReleasesResourcesOnComplete()
        func testFirstReleasesResourcesOnError() 
#endif
```

Code after:
            ])

        let res = scheduler.start { () -> Observable<Int?> in
            let single: Single<Int?> = xs.first()
            return single.asObservable()
        }

        XCTAssertEqual(res.events, [
            error(210, testError)
            ])

        XCTAssertEqual(xs.subscriptions, [
            Subscription(200, 210)
            ])
    }

    #if TRACE_RESOURCES
        func testFirstReleasesResourcesOnComplete() {
            _ = Observable<Int>.just(1).first().subscribe({ _ in })
        }

        func testFirstReleasesResourcesOnError1() {
            _ = Observable<Int>.error(testError).first().subscribe({ _ in })
        }
    #endif
}

extension PrimitiveSequenceTest {
    func testAsMaybe_Empty() {
        let scheduler = TestScheduler(initialClock: 0)

        let xs = scheduler.createHotObservable([
            next(150, 1),
            completed(250),
            error(260, testError)
            ])

        let res = scheduler.start { () -> Observable<Int> in
            let maybe: Maybe<Int> = xs.asMaybe()
            return maybe.asObservable()
        }

        XCTAssertEqual(res.events, [
            completed(250)
            ])

        XCTAssertEqual(xs.subscriptions, [
            Subscription(200, 250)
            ])
    }
Repository: ReactiveX/RxSwift
File: RxBlocking/BlockingObservable+Operators.swift (Swift)

Code before:
//
//  BlockingObservable+Operators.swift
//  RxBlocking
//
//  Created by Krunoslav Zaher on 10/19/15.
//  Copyright © 2015 Krunoslav Zaher. All rights reserved.
//

#if !RX_NO_MODULE
    import RxSwift
#endif

public enum MaterializedSequenceResult<T> {
    case completed(elements: [T])
    case failed(elements: [T], error: Error)
}

extension BlockingObservable {
    /// Blocks current thread until sequence terminates.
    ///
    /// If sequence terminates with error, terminating error will be thrown.
    ///
    /// - returns: All elements of sequence.
    public func toArray() throws -> [E] {
        let results = materializeResult()
        return try elementsOrThrow(results)
    }
}

extension BlockingObservable {
    /// Blocks current thread until sequence produces first element.
    ///
    /// If sequence terminates with error before producing first element, terminating error will be thrown.
    ///
    /// - returns: First element of sequence. If sequence is empty `nil` is returned.
    public func first() throws -> E? {
        let results = materializeResult(max: 1)

Review comment (bug):
I think we should document this, what `completed` means, what `failed` means and what is this `enum` for.

Code after:
//
//  BlockingObservable+Operators.swift
//  RxBlocking
//
//  Created by Krunoslav Zaher on 10/19/15.
//  Copyright © 2015 Krunoslav Zaher. All rights reserved.
//

#if !RX_NO_MODULE
    import RxSwift
#endif

/// The `MaterializedSequenceResult` enum represents the materialized
/// output of a BlockingObservable.
///
/// If the sequence terminates successfully, the result is represented
/// by `.completed` with the array of elements.
///
/// If the sequene terminates with error, the result is represented
/// by `.failed` with both the array of elements and the terminating error.
public enum MaterializedSequenceResult<T> {
    case completed(elements: [T])
    case failed(elements: [T], error: Error)
}

extension BlockingObservable {
    /// Blocks current thread until sequence terminates.
    ///
    /// If sequence terminates with error, terminating error will be thrown.
    ///
    /// - returns: All elements of sequence.
    public func toArray() throws -> [E] {
        let results = materializeResult()
        return try elementsOrThrow(results)
    }
}
Repository: ReactiveX/RxSwift
File: RxSwift/Observables/Sample.swift (Swift)

Code before:
final private class SamplerSink<Observer: ObserverType, SampleType>
    : ObserverType
    , LockOwnerType
    , SynchronizedOnType {
    typealias Element = SampleType
    
    typealias Parent = SampleSequenceSink<Observer, SampleType>
    
    private let parent: Parent

    var lock: RecursiveLock {
        self.parent.lock
    }
    
    init(parent: Parent) {
        self.parent = parent
    }
    
    func on(_ event: Event<Element>) {
        self.synchronizedOn(event)
    }

    func synchronized_on(_ event: Event<Element>) {
        switch event {
        case .next, .completed:
            if let element = parent.element {
                self.parent.element = nil
                self.parent.forwardOn(.next(element))
            } else if let element = self.parent.parent.defaultValue {
               self.parent.forwardOn(.next(element))
            }

            if self.parent.atEnd {
                self.parent.forwardOn(.completed)
                self.parent.dispose()
            }
        case .error(let e):
            self.parent.forwardOn(.error(e))
            self.parent.dispose()
        }
    }
}

final private class SampleSequenceSink<Observer: ObserverType, SampleType>
    : Sink<Observer>
    , ObserverType
    , LockOwnerType
    , SynchronizedOnType {
    typealias Element = Observer.Element 
    typealias Parent = Sample<Element, SampleType>
    

Review comment (suggestion):
```suggestion
            if let element = self.parent.element ?? self.parent.defaultValue {
```

Code after:
final private class SamplerSink<Observer: ObserverType, SampleType>
    : ObserverType
    , LockOwnerType
    , SynchronizedOnType {
    typealias Element = SampleType
    
    typealias Parent = SampleSequenceSink<Observer, SampleType>
    
    private let parent: Parent

    var lock: RecursiveLock {
        self.parent.lock
    }
    
    init(parent: Parent) {
        self.parent = parent
    }
    
    func on(_ event: Event<Element>) {
        self.synchronizedOn(event)
    }

    func synchronized_on(_ event: Event<Element>) {
        switch event {
        case .next, .completed:
            if let element = parent.element ?? self.parent.defaultValue {
                self.parent.element = nil
                self.parent.forwardOn(.next(element))
            }

            if self.parent.atEnd {
                self.parent.forwardOn(.completed)
                self.parent.dispose()
            }
        case .error(let e):
            self.parent.forwardOn(.error(e))
            self.parent.dispose()
        }
    }
}

final private class SampleSequenceSink<Observer: ObserverType, SampleType>
    : Sink<Observer>
    , ObserverType
    , LockOwnerType
    , SynchronizedOnType {
    typealias Element = Observer.Element 
    typealias Parent = Sample<Element, SampleType>
    
    fileprivate let parent: Parent
    fileprivate let defaultValue: Element?
Repository: ReactiveX/RxSwift
File: RxSwift/Observables/Sample.swift (Swift)

Code before:
        return Disposables.create(sourceSubscription, samplerSubscription)
    }
    
    func on(_ event: Event<Element>) {
        self.synchronizedOn(event)
    }

    func synchronized_on(_ event: Event<Element>) {
        switch event {
        case .next(let element):
            self.element = element
        case .error:
            self.forwardOn(event)
            self.dispose()
        case .completed:
            self.atEnd = true
            self.sourceSubscription.dispose()
        }
    }
    
}

final private class Sample<Element, SampleType>: Producer<Element> {
    fileprivate let source: Observable<Element>
    fileprivate let sampler: Observable<SampleType>
    fileprivate let defaultValue: Element?
    
    init(source: Observable<Element>, sampler: Observable<SampleType>, defaultValue: Element? = nil) {
        self.source = source
        self.sampler = sampler
        self.defaultValue = defaultValue
    }
    
    override func run<Observer: ObserverType>(_ observer: Observer, cancel: Cancelable) -> (sink: Disposable, subscription: Disposable) where Observer.Element == Element {
        let sink = SampleSequenceSink(parent: self, observer: observer, cancel: cancel)
        let subscription = sink.run()
        return (sink: sink, subscription: subscription)
    }
}


Review comment (suggestion):
add a `defaultValue` argument to `SampleSequenceSink` and pass it inside. Doing `parent.parent` doesn't make much sense in this case. 

Code after:
        return Disposables.create(sourceSubscription, samplerSubscription)
    }
    
    func on(_ event: Event<Element>) {
        self.synchronizedOn(event)
    }

    func synchronized_on(_ event: Event<Element>) {
        switch event {
        case .next(let element):
            self.element = element
        case .error:
            self.forwardOn(event)
            self.dispose()
        case .completed:
            self.atEnd = true
            self.sourceSubscription.dispose()
        }
    }
    
}

final private class Sample<Element, SampleType>: Producer<Element> {
    fileprivate let source: Observable<Element>
    fileprivate let sampler: Observable<SampleType>
    fileprivate let defaultValue: Element?
    
    init(source: Observable<Element>, sampler: Observable<SampleType>, defaultValue: Element? = nil) {
        self.source = source
        self.sampler = sampler
        self.defaultValue = defaultValue
    }
    
    override func run<Observer: ObserverType>(_ observer: Observer, cancel: Cancelable) -> (sink: Disposable, subscription: Disposable) where Observer.Element == Element {
        let sink = SampleSequenceSink(parent: self, observer: observer, cancel: cancel, defaultValue: self.defaultValue)
        let subscription = sink.run()
        return (sink: sink, subscription: subscription)
    }
}
Repository: ReactiveX/RxSwift
File: RxSwift/Observables/Sample.swift (Swift)

Code before:
final private class SamplerSink<Observer: ObserverType, SampleType>
    : ObserverType
    , LockOwnerType
    , SynchronizedOnType {
    typealias Element = SampleType
    
    typealias Parent = SampleSequenceSink<Observer, SampleType>
    
    private let parent: Parent

    var lock: RecursiveLock {
        self.parent.lock
    }
    
    init(parent: Parent) {
        self.parent = parent
    }
    
    func on(_ event: Event<Element>) {
        self.synchronizedOn(event)
    }

    func synchronized_on(_ event: Event<Element>) {
        switch event {
        case .next, .completed:
            if let element = parent.element ?? self.parent.defaultValue{
                self.parent.element = nil
                self.parent.forwardOn(.next(element))
            }

            if self.parent.atEnd {
                self.parent.forwardOn(.completed)
                self.parent.dispose()
            }
        case .error(let e):
            self.parent.forwardOn(.error(e))
            self.parent.dispose()
        }
    }
}

final private class SampleSequenceSink<Observer: ObserverType, SampleType>
    : Sink<Observer>
    , ObserverType
    , LockOwnerType
    , SynchronizedOnType {
    typealias Element = Observer.Element 
    typealias Parent = Sample<Element, SampleType>
    
    fileprivate let parent: Parent
    fileprivate let defaultValue: Element?

Review comment (suggestion):
```suggestion
            if let element = parent.element ?? self.parent.defaultValue {
```

Code after:
final private class SamplerSink<Observer: ObserverType, SampleType>
    : ObserverType
    , LockOwnerType
    , SynchronizedOnType {
    typealias Element = SampleType
    
    typealias Parent = SampleSequenceSink<Observer, SampleType>
    
    private let parent: Parent

    var lock: RecursiveLock {
        self.parent.lock
    }
    
    init(parent: Parent) {
        self.parent = parent
    }
    
    func on(_ event: Event<Element>) {
        self.synchronizedOn(event)
    }

    func synchronized_on(_ event: Event<Element>) {
        switch event {
        case .next, .completed:
            if let element = parent.element ?? self.parent.defaultValue {
                self.parent.element = nil
                self.parent.forwardOn(.next(element))
            }

            if self.parent.atEnd {
                self.parent.forwardOn(.completed)
                self.parent.dispose()
            }
        case .error(let e):
            self.parent.forwardOn(.error(e))
            self.parent.dispose()
        }
    }
}

final private class SampleSequenceSink<Observer: ObserverType, SampleType>
    : Sink<Observer>
    , ObserverType
    , LockOwnerType
    , SynchronizedOnType {
    typealias Element = Observer.Element 
    typealias Parent = Sample<Element, SampleType>
    
    fileprivate let parent: Parent
    fileprivate let defaultValue: Element?
Repository: ReactiveX/RxSwift
File: Tests/RxSwiftTests/MaybeTest.swift (Swift)

Code before:
        }

        XCTAssertEqual(res.events, [
            next(200, 2),
            completed(200)
            ])
    }

    func test_flatMap() {
        let scheduler = TestScheduler(initialClock: 0)

        let res = scheduler.start {
            (Maybe<Int>.just(1).flatMap { .just($0 * 2) } as Maybe<Int>).asObservable()
        }

        XCTAssertEqual(res.events, [
            next(200, 2),
            completed(200)
            ])
    }

    func test_ifEmptyDefault() {
        let scheduler = TestScheduler(initialClock: 0)

        let res = scheduler.start {
            (Maybe<Int>.empty().ifEmpty(default: 5).asObservable())
        }

        XCTAssertEqual(res.events, [
            next(200, 5),
            completed(200)
            ])
    }

    func test_ifEmptySwitchToMaybe() {
        let scheduler = TestScheduler(initialClock: 0)
        let source = Maybe<Int>.empty()
        let switchSource = Maybe.just(10)

        let res = scheduler.start {
            (source.ifEmpty(switchTo: switchSource).asObservable())
        }

        XCTAssertEqual(res.events, [
            next(200, 10),
            completed(200)
            ])
    }

    func test_ifEmptySwitchToSingle() {
        let scheduler = TestScheduler(initialClock: 0)

Review comment (suggestion):
This should be of form:
```
    (Maybe<Int>.empty().ifEmpty(default: 5) as Single<Int>).asObservable()
```

Code after:
        }

        XCTAssertEqual(res.events, [
            .next(200, 2),
            .completed(200)
            ])
    }

    func test_flatMap() {
        let scheduler = TestScheduler(initialClock: 0)

        let res = scheduler.start {
            (Maybe<Int>.just(1).flatMap { .just($0 * 2) } as Maybe<Int>).asObservable()
        }

        XCTAssertEqual(res.events, [
            .next(200, 2),
            .completed(200)
            ])
    }

    func test_ifEmptyDefault() {
        let scheduler = TestScheduler(initialClock: 0)

        let res = scheduler.start {
            ((Maybe<Int>.empty().ifEmpty(default: 5) as Single<Int>).asObservable())
        }

        XCTAssertEqual(res.events, [
            next(200, 5),
            completed(200)
            ])
    }

    func test_ifEmptySwitchToMaybe() {
        let scheduler = TestScheduler(initialClock: 0)
        let source = Maybe<Int>.empty()
        let switchSource = Maybe.just(10)

        let res = scheduler.start {
            ((source.ifEmpty(switchTo: switchSource) as Maybe<Int>).asObservable())
        }

        XCTAssertEqual(res.events, [
            next(200, 10),
            completed(200)
            ])
    }

    func test_ifEmptySwitchToSingle() {
        let scheduler = TestScheduler(initialClock: 0)
Repository: ReactiveX/RxSwift
File: Tests/RxSwiftTests/MaybeTest.swift (Swift)

Code before:
        XCTAssertEqual(res.events, [
            next(200, 2),
            completed(200)
            ])
    }

    func test_ifEmptyDefault() {
        let scheduler = TestScheduler(initialClock: 0)

        let res = scheduler.start {
            (Maybe<Int>.empty().ifEmpty(default: 5).asObservable())
        }

        XCTAssertEqual(res.events, [
            next(200, 5),
            completed(200)
            ])
    }

    func test_ifEmptySwitchToMaybe() {
        let scheduler = TestScheduler(initialClock: 0)
        let source = Maybe<Int>.empty()
        let switchSource = Maybe.just(10)

        let res = scheduler.start {
            (source.ifEmpty(switchTo: switchSource).asObservable())
        }

        XCTAssertEqual(res.events, [
            next(200, 10),
            completed(200)
            ])
    }

    func test_ifEmptySwitchToSingle() {
        let scheduler = TestScheduler(initialClock: 0)
        let source = Maybe<Int>.empty()
        let switchSource = Single.just(10)

        let res = scheduler.start {
            (source.ifEmpty(switchTo: switchSource).asObservable())
        }

        XCTAssertEqual(res.events, [
            next(200, 10),
            completed(200)
            ])
    }
}

extension MaybeTest {

Review comment (suggestion):
I think this should be:
```
(Maybe<Int>.empty().ifEmpty(switchTo: Maybe.just(10)) as Maybe<Int>).asObservable()
```

Code after:
        XCTAssertEqual(res.events, [
            .next(200, 2),
            .completed(200)
            ])
    }

    func test_ifEmptyDefault() {
        let scheduler = TestScheduler(initialClock: 0)

        let res = scheduler.start {
            ((Maybe<Int>.empty().ifEmpty(default: 5) as Single<Int>).asObservable())
        }

        XCTAssertEqual(res.events, [
            next(200, 5),
            completed(200)
            ])
    }

    func test_ifEmptySwitchToMaybe() {
        let scheduler = TestScheduler(initialClock: 0)
        let source = Maybe<Int>.empty()
        let switchSource = Maybe.just(10)

        let res = scheduler.start {
            ((source.ifEmpty(switchTo: switchSource) as Maybe<Int>).asObservable())
        }

        XCTAssertEqual(res.events, [
            next(200, 10),
            completed(200)
            ])
    }

    func test_ifEmptySwitchToSingle() {
        let scheduler = TestScheduler(initialClock: 0)
        let source = Maybe<Int>.empty()
        let switchSource = Single.just(10)

        let res = scheduler.start {
            ((source.ifEmpty(switchTo: switchSource) as Single<Int>).asObservable())
        }

        XCTAssertEqual(res.events, [
            next(200, 10),
            completed(200)
            ])
    }
}

extension MaybeTest {