<commit_before>//
//  Observable.swift
//  Swifty
//
//  Created by <NAME> on 31.01.18.
//  Copyright © 2018 <NAME>. All rights reserved.
//

import Foundation

final public class Observable<Value> {

    public typealias Observer    = (Value) -> Void
    public typealias ObserverOld = (Value, Value) -> Void

    private var observers:    Atomic<[UUID: Observer]>
    private var observersOld: Atomic<[UUID: ObserverOld]>

    public init(_ value: Value) {
        self.value = value
        self.observers    = Atomic([:])
        self.observersOld = Atomic([:])
    }

    public var value: Value {
        didSet {
            observers.value.forEach { (_, observer) in
                observer(self.value)
            }
            observersOld.value.forEach { (_, observer) in
                observer(self.value, oldValue)
            }
        }
    }

    public func observe(onlyNew: Bool = false, observer: @escaping Observer) -> Disposable {
        if !onlyNew {
            observer(self.value)
        }

        let id = UUID()
        self.observers.mutate { observers in
            observers[id] = observer
        }

        return Disposable {
            self.observers.mutate { observers in
                observers[id] = nil
            }
        }
    }

    public func observeOld(observer: @escaping ObserverOld) -> Disposable {

        let id = UUID()
        self.observersOld.mutate { observers in
            observers[id] = observer
        }

        return Disposable {
            self.observersOld.mutate { observers in
                observers[id] = nil
            }
        }
    }
}

extension Observable where Value: Equatable {

    public func distinct(observer: @escaping Observer) -> Disposable {
        return self.observeOld { newValue, oldValue in
            if newValue != oldValue {
                observer(newValue)
            }
        }
    }
}


final public class Disposable {
    public typealias Dispose = () -> Void
    private let dispose: Dispose

    init(_ dispose: @escaping Dispose) {
        self.dispose = dispose
    }

    deinit {
        dispose()
    }
}<commit_msg>distinct+new<commit_after>//
//  Observable.swift
//  Swifty
//
//  Created by <NAME> on 31.01.18.
//  Copyright © 2018 <NAME>. All rights reserved.
//

import Foundation

final public class Observable<Value> {

    public typealias Observer    = (Value) -> Void
    public typealias ObserverOld = (Value, Value) -> Void

    private var observers:    Atomic<[UUID: Observer]>
    private var observersOld: Atomic<[UUID: ObserverOld]>

    public init(_ value: Value) {
        self.value = value
        self.observers    = Atomic([:])
        self.observersOld = Atomic([:])
    }

    public var value: Value {
        didSet {
            observers.value.forEach { (_, observer) in
                observer(self.value)
            }
            observersOld.value.forEach { (_, observer) in
                observer(self.value, oldValue)
            }
        }
    }

    public func observe(onlyNew: Bool = false, observer: @escaping Observer) -> Disposable {
        if !onlyNew { observer(self.value) }

        let id = UUID()
        self.observers.mutate { observers in
            observers[id] = observer
        }

        return Disposable {
            self.observers.mutate { observers in
                observers[id] = nil
            }
        }
    }

    public func observeOld(observer: @escaping ObserverOld) -> Disposable {

        let id = UUID()
        self.observersOld.mutate { observers in
            observers[id] = observer
        }

        return Disposable {
            self.observersOld.mutate { observers in
                observers[id] = nil
            }
        }
    }
}

extension Observable where Value: Equatable {

    public func distinct(onlyNew: Bool = false, observer: @escaping Observer) -> Disposable {
        if !onlyNew { observer(self.value) }

        return self.observeOld { newValue, oldValue in
            if newValue != oldValue {
                observer(newValue)
            }
        }
    }
}


final public class Disposable {
    public typealias Dispose = () -> Void
    private let dispose: Dispose

    init(_ dispose: @escaping Dispose) {
        self.dispose = dispose
    }

    deinit {
        dispose()
    }
}
{'fraction_non_alphanumeric': 0.09727985246657446, 'fraction_numerical': 0.004610419548178884, 'mean_word_length': 1.7531725888324874, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2624476', 'n_tokens_mistral': 1308, 'n_tokens_neox': 1205, 'n_words': 314}