<commit_before>//
//  SwiftySwift.swift
//  SwiftySwift
//
//  Created by <NAME> on 6/1/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

// MARK: - cast helpers

/**
The following methods can be used to perform a cast without being explicit 
about the class the object is being cast to, relying instead on the type-inferrence
features of the compiler.

This allows creating class methods that return an instance of the subclass they are called on.

e.g.

    class A {
     class func factoryMethod() -> Self? {
      let instance = createNewInstance()
      return cast(instance)
     }
    }
    
    class B : A {
    }
    
    A.factoryMethod()  // type is inferred as A?
    B.factoryMethod()  // type is inferred as B?
**/

/// Optional cast of `x` as type `V`
public func cast<U, V>(_ x: U) -> V? {
    return x as? V
}

/// Return all values of `source` that were successfully casted to type `V`
public func castFilter<S: Sequence, V>(_ source: S) -> [V] {
    return source.flatMap {
        $0 as? V
    }
}

/// Return the first value of `source` that was successfully casted to type `V`
public func castFirst<S: Sequence, V>(_ source: S) -> V? {
    return source.flatMap {
        $0 as? V
    }.first
}

/// Forced cast of `x` as type `V`
public func forcedCast<U, V>(_ x: U) -> V {
    return x as! V
}

public func stringFromClass(_ aClass: AnyClass) -> String {
    return NSStringFromClass(aClass).components(separatedBy: ".").last!
}

// MARK: - math

/// Linear interpolation between two values
public func lerp<T: FloatingPoint>(_ from: T, _ to: T, _ progress: T) -> T {
    return from * (T(1) - progress) + to * progress
}

public func sign<T: SignedNumber>(_ x: T) -> T {
    return x == 0 ? 0 : x == abs(x) ? 1 : -1
}

public func sign<T: UnsignedInteger>(_ x: T) -> T {
    return x == 0 ? 0 : 1
}

public func mod(_ lhs: Int, _ rhs: Int) -> Int {
    return (lhs % rhs + rhs) % rhs
}

public func min<T : Comparable>(_ t: (T, T)) -> T {
    return min(t.0, t.1)
}

public func max<T : Comparable>(_ t: (T, T)) -> T {
    return max(t.0, t.1)
}
<commit_msg>fixing compile error in Xcode 8.3.1. Using min and max from Swift module.<commit_after>//
//  SwiftySwift.swift
//  SwiftySwift
//
//  Created by <NAME> on 6/1/15.
//  Copyright (c) 2015 <NAME>. All rights reserved.
//

// MARK: - cast helpers

/**
The following methods can be used to perform a cast without being explicit 
about the class the object is being cast to, relying instead on the type-inferrence
features of the compiler.

This allows creating class methods that return an instance of the subclass they are called on.

e.g.

    class A {
     class func factoryMethod() -> Self? {
      let instance = createNewInstance()
      return cast(instance)
     }
    }
    
    class B : A {
    }
    
    A.factoryMethod()  // type is inferred as A?
    B.factoryMethod()  // type is inferred as B?
**/

/// Optional cast of `x` as type `V`
public func cast<U, V>(_ x: U) -> V? {
    return x as? V
}

/// Return all values of `source` that were successfully casted to type `V`
public func castFilter<S: Sequence, V>(_ source: S) -> [V] {
    return source.flatMap {
        $0 as? V
    }
}

/// Return the first value of `source` that was successfully casted to type `V`
public func castFirst<S: Sequence, V>(_ source: S) -> V? {
    return source.flatMap {
        $0 as? V
    }.first
}

/// Forced cast of `x` as type `V`
public func forcedCast<U, V>(_ x: U) -> V {
    return x as! V
}

public func stringFromClass(_ aClass: AnyClass) -> String {
    return NSStringFromClass(aClass).components(separatedBy: ".").last!
}

// MARK: - math

/// Linear interpolation between two values
public func lerp<T: FloatingPoint>(_ from: T, _ to: T, _ progress: T) -> T {
    return from * (T(1) - progress) + to * progress
}

public func sign<T: SignedNumber>(_ x: T) -> T {
    return x == 0 ? 0 : x == abs(x) ? 1 : -1
}

public func sign<T: UnsignedInteger>(_ x: T) -> T {
    return x == 0 ? 0 : 1
}

public func mod(_ lhs: Int, _ rhs: Int) -> Int {
    return (lhs % rhs + rhs) % rhs
}

public func min<T : Comparable>(_ t: (T, T)) -> T {
    return Swift.min(t.0, t.1)
}

public func max<T : Comparable>(_ t: (T, T)) -> T {
    return Swift.max(t.0, t.1)
}

{'fraction_non_alphanumeric': 0.13890192397935242, 'fraction_numerical': 0.011027686532144533, 'mean_word_length': 2.579345088161209, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 49, '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': '3082614', 'n_tokens_mistral': 1518, 'n_tokens_neox': 1451, 'n_words': 562}