<commit_before>//
//  Channels.swift
//  ChannelZ
//
//  Created by <NAME> <<EMAIL>>
//  License: MIT (or whatever)
//

import Foundation


/// A defaultable type is a type that can be initialized with no arguments and a default value
public protocol Defaultable {
    init(defaulting: ())
}

public extension _WrapperType {
    /// Returns the wrapped value or the defaulting value if the wrapped value if nil
    public subscript(defaulting constructor: @autoclosure () -> Wrapped) -> Wrapped {
        get { return self.flatMap({ $0 }) ?? constructor() }
        set { self = Self(newValue) }
    }
}

public extension _WrapperType where Wrapped : Defaultable {
    /// Returns the current value of the wrapped instance or, if nul, instantiates a default value
    public var defaulted: Wrapped {
        get { return self[defaulting: .init(defaulting: ())] }
        set { self = Self(newValue) }
    }
}

public extension RangeReplaceableCollection where Element : Defaultable {
    public var defaultedFirst: Element {
        get { return self.first[defaulting: .init(defaulting: ())] }
        set { self = Self([newValue] + dropFirst()) }
    }

    public subscript(defaulted index: Index) -> Element {
        get { return indices.contains(index) ? self[index] : .init(defaulting: ()) }

        set {
            // fill in the intervening indeices with the default value
            while !indices.contains(index) {
                append(.init(defaulting: ()))
            }
            // set the target index item
            replaceSubrange(index...index, with: [newValue])
        }
    }
}

public extension RangeReplaceableCollection where Self : BidirectionalCollection, Element : Defaultable {
    public var defaultedLast: Element {
        get { return self.last[defaulting: .init(defaulting: ())] }
        set { self = Self(dropLast() + [newValue]) }
    }
}

public extension Set where Element : Defaultable {
    public var defaultedAny: Element {
        get { return self.first ?? .init(defaulting: ()) }
        set { self = Set(dropFirst() + [newValue]) }
    }
}

extension ExpressibleByNilLiteral {
    /// An `ExpressibleByNilLiteral` conforms to `Defaultable` by nil initialization
    public init(defaulting: ()) { self.init(nilLiteral: ()) }
}

extension ExpressibleByArrayLiteral {
    /// An `ExpressibleByArrayLiteral` conforms to `Defaultable` by empty array initialization
    public init(defaulting: ()) { self.init() }
}

extension ExpressibleByDictionaryLiteral {
    /// An `ExpressibleByDictionaryLiteral` conforms to `Defaultable` by empty dictionary initialization
    public init(defaulting: ()) { self.init() }
}

//extension Optional : Defaultable { } // inherits initializer from ExpressibleByNilLiteral
extension Set : Defaultable { } // inherits initializer from ExpressibleByArrayLiteral
extension Array : Defaultable { } // inherits initializer from ExpressibleByArrayLiteral
extension ContiguousArray : Defaultable { } // inherits initializer from ExpressibleByArrayLiteral
extension Dictionary : Defaultable { } // inherit initializer from ExpressibleByDictionaryLiteral

extension EmptyCollection : Defaultable { public init(defaulting: ()) { self.init() } }

/// An optional is defaultable when its wrapped instance is defaultable
extension Optional : Defaultable where Wrapped : Defaultable {
    public init(defaulting: ()) {
        self = .some(Wrapped.init(defaulting: ()))
    }
}

public extension Defaultable where Self : Equatable {
    /// Returns true if this instance is the same as the defaulted value
    public var isDefaultedValue: Bool {
        return self == Self.init(defaulting: ())
    }

    /// Assign this to the specified value unless it is already set, in which
    /// case it is defaulted to the default value. For example:
    /// ob.optionalString.toggleDefault("foo") will set the optionalString?
    /// variable to "foo", or, if it was already "foo", will clear it.
    public mutating func toggleDefault(_ value: Self) {
        self = self == value ? .init(defaulting: ()) : value
    }

}

/// Zips the two sequence together using `zip`, but first pads the shorter of the
/// sequences with the defaultable init so that they are both the same length
@inlinable public func zipWithPadding<Sequence1, Sequence2>(_ sequence1: Sequence1, _ sequence2: Sequence2) -> Zip2Sequence<[Sequence1.Element], [Sequence2.Element]> where Sequence1 : Sequence, Sequence1.Element : Defaultable, Sequence2 : Sequence, Sequence2.Element : Defaultable {
    var s1 = Array(sequence1)
    var s2 = Array(sequence2)
    while s1.count < s2.count { s1.append(.init(defaulting: ())) }
    while s2.count < s1.count { s2.append(.init(defaulting: ())) }
    return zip(s1, s2)
}


/// Dynamically convert between the given numeric types, getting past Swift's inability to statically cast between numbers
@inlinable public func convertNumericType<From : ConduitNumericCoercible, To : ConduitNumericCoercible>(_ from: From) -> To {
    // try both sides of the convertables so this can be extended by other types (such as NSNumber)
    return To.fromConduitNumericCoercible(from) ?? from.toConduitNumericCoercible() ?? from as! To
}

/// Implemented by numeric types that can be coerced into other numeric types
public protocol ConduitNumericCoercible {
    static func fromConduitNumericCoercible(_ value: ConduitNumericCoercible) -> Self?
    func toConduitNumericCoercible<T : ConduitNumericCoercible>() -> T?
}


extension Bool : ConduitNumericCoercible {
    @inlinable public static func fromConduitNumericCoercible(_ value: ConduitNumericCoercible) -> Bool? {
        if let value = value as? Bool { return self.init(value) }
        else if let value = value as? Int8 { return self.init(value != 0) }
        else if let value = value as? UInt8 { return self.init(value != 0) }
        else if let value = value as? Int16 { return self.init(value != 0) }
        else if let value = value as? UInt16 { return self.init(value != 0) }
        else if let value = value as? Int32 { return self.init(value != 0) }
        else if let value = value as? UInt32 { return self.init(value != 0) }
        else if let value = value as? Int { return self.init(value != 0) }
        else if let value = value as? UInt { return self.init(value != 0) }
        else if let value = value as? Int64 { return self.init(value != 0) }
        else if let value = value as? UInt64 { return self.init(value != 0) }<commit_msg>Checkpoint<commit_after>//
//  Channels.swift
//  ChannelZ
//
//  Created by <NAME> <<EMAIL>>
//  License: MIT (or whatever)
//

import Foundation


/// A defaultable type is a type that can be initialized with no arguments and a default value
public protocol Defaultable {
    /// Instantiate a new instance of this default value.
    init(defaulting: ())

    /// A single instance of the default value, which may be shared for pure value types.
    static var defaultValue: Self { get }
}

public extension Defaultable {
    public static var defaultValue: Self { return .init(defaulting: ()) }
}

public extension _WrapperType {
    /// Returns the wrapped value or the defaulting value if the wrapped value if nil
    public subscript(defaulting constructor: @autoclosure () -> Wrapped) -> Wrapped {
        get { return self.flatMap({ $0 }) ?? constructor() }
        set { self = Self(newValue) }
    }
}

public extension _WrapperType where Wrapped : Defaultable {
    /// Returns the current value of the wrapped instance or, if nil, instantiates a default value
    public var defaulted: Wrapped {
        get { return self[defaulting: .defaultValue] }
        set { self = Self(newValue) }
    }
}

public extension RangeReplaceableCollection where Element : Defaultable {
    public var defaultedFirst: Element {
        get { return self.first[defaulting: .defaultValue] }
        set { self = Self([newValue] + dropFirst()) }
    }

    public subscript(defaulted index: Index) -> Element {
        get { return indices.contains(index) ? self[index] : .defaultValue }

        set {
            // fill in the intervening indeices with the default value
            while !indices.contains(index) {
                append(.defaultValue)
            }
            // set the target index item
            replaceSubrange(index...index, with: [newValue])
        }
    }
}

public extension RangeReplaceableCollection where Self : BidirectionalCollection, Element : Defaultable {
    public var defaultedLast: Element {
        get { return self.last[defaulting: .defaultValue] }
        set { self = Self(dropLast() + [newValue]) }
    }
}

public extension Set where Element : Defaultable {
    public var defaultedAny: Element {
        get { return self.first ?? .defaultValue }
        set { self = Set(dropFirst() + [newValue]) }
    }
}

extension ExpressibleByNilLiteral {
    /// An `ExpressibleByNilLiteral` conforms to `Defaultable` by nil initialization
    public init(defaulting: ()) { self.init(nilLiteral: ()) }
}

extension ExpressibleByArrayLiteral {
    /// An `ExpressibleByArrayLiteral` conforms to `Defaultable` by empty array initialization
    public init(defaulting: ()) { self.init() }
}

extension ExpressibleByDictionaryLiteral {
    /// An `ExpressibleByDictionaryLiteral` conforms to `Defaultable` by empty dictionary initialization
    public init(defaulting: ()) { self.init() }
}

//extension Optional : Defaultable { } // inherits initializer from ExpressibleByNilLiteral
extension Set : Defaultable { } // inherits initializer from ExpressibleByArrayLiteral
extension Array : Defaultable { } // inherits initializer from ExpressibleByArrayLiteral
extension ContiguousArray : Defaultable { } // inherits initializer from ExpressibleByArrayLiteral
extension Dictionary : Defaultable { } // inherit initializer from ExpressibleByDictionaryLiteral

extension EmptyCollection : Defaultable { public init(defaulting: ()) { self.init() } }

/// An optional is defaultable when its wrapped instance is defaultable
extension Optional : Defaultable where Wrapped : Defaultable {
    public init(defaulting: ()) {
        self = .some(.defaultValue)
    }
}

public extension Defaultable where Self : Equatable {
    /// Returns true if this instance is the same as the defaulted value
    public var isDefaultedValue: Bool {
        return self == .defaultValue
    }

    /// Assign this to the specified value unless it is already set, in which
    /// case it is defaulted to the default value. For example:
    /// ob.optionalString.toggleDefault("foo") will set the optionalString?
    /// variable to "foo", or, if it was already "foo", will clear it.
    public mutating func toggleDefault(_ value: Self) {
        self = self == value ? .defaultValue : value
    }

}

/// Zips the two sequence together using `zip`, but first pads the shorter of the
/// sequences with the defaultable init so that they are both the same length
@inlinable public func zipWithPadding<Sequence1, Sequence2>(_ sequence1: Sequence1, _ sequence2: Sequence2) -> Zip2Sequence<[Sequence1.Element], [Sequence2.Element]> where Sequence1 : Sequence, Sequence1.Element : Defaultable, Sequence2 : Sequence, Sequence2.Element : Defaultable {
    var s1 = Array(sequence1)
    var s2 = Array(sequence2)
    while s1.count < s2.count { s1.append(.defaultValue) }
    while s2.count < s1.count { s2.append(.defaultValue) }
    return zip(s1, s2)
}


/// Dynamically convert between the given numeric types, getting past Swift's inability to statically cast between numbers
@inlinable public func convertNumericType<From : ConduitNumericCoercible, To : ConduitNumericCoercible>(_ from: From) -> To {
    // try both sides of the convertables so this can be extended by other types (such as NSNumber)
    return To.fromConduitNumericCoercible(from) ?? from.toConduitNumericCoercible() ?? from as! To
}

/// Implemented by numeric types that can be coerced into other numeric types
public protocol ConduitNumericCoercible {
    static func fromConduitNumericCoercible(_ value: ConduitNumericCoercible) -> Self?
    func toConduitNumericCoercible<T : ConduitNumericCoercible>() -> T?
}


extension Bool : ConduitNumericCoercible {
    @inlinable public static func fromConduitNumericCoercible(_ value: ConduitNumericCoercible) -> Bool? {
        if let value = value as? Bool { return self.init(value) }
        else if let value = value as? Int8 { return self.init(value != 0) }
        else if let value = value as? UInt8 { return self.init(value != 0) }
        else if let value = value as? Int16 { return self.init(value != 0) }
        else if let value = value as? UInt16 { return self.init(value != 0) }
        else if let value = value as? Int32 { return self.init(value != 0) }
        else if let value = value as? UInt32 { return self.init(value != 0) }
        else if let value = value as? Int { return self.init(value != 0) }
        else if let value = value as? UInt { return self.init(value != 0) }
        else if let value = value as? Int64 { return self.init(value != 0) }
        else if let value = value as? UInt64 { return self.init(value != 0) }
{'fraction_non_alphanumeric': 0.09739421104611411, 'fraction_numerical': 0.007597052343690648, 'mean_word_length': 3.313237221494102, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 33, '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': '3929350', 'n_tokens_mistral': 3702, 'n_tokens_neox': 3588, 'n_words': 1276}