<commit_before>//
//  Compare.swift
//  Basis
//
//  Created by <NAME> on 9/13/14.
//  Copyright (c) 2014 TypeLift. All rights reserved.
//  Released under the MIT license.
//

/// Returns whether the two arrays are in ascending order or are equal.
///
/// Arrays are equal when they contain the same elements in the same order.  An array is always
/// larger than the empty array.  If the arrays cannot be determined to have different sizes of
/// elements, the heads of subsequent sub-arrays are compared wtih this ordering predicate until a
/// match is found.
public func <=<T : Comparable>(lhs: [T], rhs: [T]) -> Bool {
	switch (destruct(lhs), destruct(rhs)) {
		case (.Empty, .Empty):
			return true
		case (.Empty, .Destructure(_, _)):
			return true
		case (.Destructure(_, _), .Empty):
			return false
		case (.Destructure(let x, let xs), .Destructure(let y, let ys)):
			return (x == y) ? (xs <= ys) : x <= y
	}
}

/// Returns whether the two arrays are in descending order or are equal.
///
/// Arrays are equal when they contain the same elements in the same order.  An array is always
/// larger than the empty array.  If the arrays cannot be determined to have different sizes of
/// elements, the heads of subsequent sub-arrays are compared wtih this ordering predicate until a
/// match is found.
public func >=<T : Comparable>(lhs: [T], rhs: [T]) -> Bool {
	switch (destruct(lhs), destruct(rhs)) {
		case (.Empty, .Empty):
			return true
		case (.Empty, .Destructure(_, _)):
			return false
		case (.Destructure(_, _), .Empty):
			return true
		case (.Destructure(let x, let xs), .Destructure(let y, let ys)):
			return (x == y) ? (xs >= ys) : x >= y
	}
}

/// Returns whether the two arrays are in ascending order.
public func <<T : Comparable>(lhs: [T], rhs: [T]) -> Bool {
	switch (destruct(lhs), destruct(rhs)) {
		case (.Empty, .Empty):
			return false
		case (.Empty, .Destructure(_, _)):
			return true
		case (.Destructure(_, _), .Empty):
			return false
		case (.Destructure(let x, let xs), .Destructure(let y, let ys)):
			return (x == y) ? (xs < ys) : x < y
	}
}

/// Returns whether the two arrays are in descending order.
public func ><T : Comparable>(lhs: [T], rhs: [T]) -> Bool {
	switch (destruct(lhs), destruct(rhs)) {
		case (.Empty, .Empty):
			return false
		case (.Empty, .Destructure(_, _)):
			return false
		case (.Destructure(_, _), .Empty):
			return true
		case (.Destructure(let x, let xs), .Destructure(let y, let ys)):
			return (x == y) ? (xs > ys) : x > y
	}
}

/// Takes a list and groups its arguments into sublists of duplicate elements found next to each
/// other according to an equality predicate.
public func groupBy<A>(cmp : A -> A -> Bool) -> [A] -> [[A]] {
	return { l in
		switch destruct(l) {
			case .Empty:
				return []
			case .Destructure(let x, let xs):
				let (ys, zs) = span(cmp(x))(xs)
				return (x <| ys) <| groupBy(cmp)(zs)
		}
	}
}

/// Takes a list and groups its arguments into sublists of duplicate elements found next to each
/// other according to an equality operator.<commit_msg>Comparing<commit_after>//
//  Compare.swift
//  Basis
//
//  Created by <NAME> on 9/13/14.
//  Copyright (c) 2014 TypeLift. All rights reserved.
//  Released under the MIT license.
//

/// Returns whether the two arrays are in ascending order or are equal.
///
/// Arrays are equal when they contain the same elements in the same order.  An array is always
/// larger than the empty array.  If the arrays cannot be determined to have different sizes of
/// elements, the heads of subsequent sub-arrays are compared wtih this ordering predicate until a
/// match is found.
public func <=<T : Comparable>(lhs: [T], rhs: [T]) -> Bool {
	switch (destruct(lhs), destruct(rhs)) {
		case (.Empty, .Empty):
			return true
		case (.Empty, .Destructure(_, _)):
			return true
		case (.Destructure(_, _), .Empty):
			return false
		case (.Destructure(let x, let xs), .Destructure(let y, let ys)):
			return (x == y) ? (xs <= ys) : x <= y
	}
}

/// Returns whether the two arrays are in descending order or are equal.
///
/// Arrays are equal when they contain the same elements in the same order.  An array is always
/// larger than the empty array.  If the arrays cannot be determined to have different sizes of
/// elements, the heads of subsequent sub-arrays are compared wtih this ordering predicate until a
/// match is found.
public func >=<T : Comparable>(lhs: [T], rhs: [T]) -> Bool {
	switch (destruct(lhs), destruct(rhs)) {
		case (.Empty, .Empty):
			return true
		case (.Empty, .Destructure(_, _)):
			return false
		case (.Destructure(_, _), .Empty):
			return true
		case (.Destructure(let x, let xs), .Destructure(let y, let ys)):
			return (x == y) ? (xs >= ys) : x >= y
	}
}

/// Returns whether the two arrays are in ascending order.
public func <<T : Comparable>(lhs: [T], rhs: [T]) -> Bool {
	switch (destruct(lhs), destruct(rhs)) {
		case (.Empty, .Empty):
			return false
		case (.Empty, .Destructure(_, _)):
			return true
		case (.Destructure(_, _), .Empty):
			return false
		case (.Destructure(let x, let xs), .Destructure(let y, let ys)):
			return (x == y) ? (xs < ys) : x < y
	}
}

/// Returns whether the two arrays are in descending order.
public func ><T : Comparable>(lhs: [T], rhs: [T]) -> Bool {
	switch (destruct(lhs), destruct(rhs)) {
		case (.Empty, .Empty):
			return false
		case (.Empty, .Destructure(_, _)):
			return false
		case (.Destructure(_, _), .Empty):
			return true
		case (.Destructure(let x, let xs), .Destructure(let y, let ys)):
			return (x == y) ? (xs > ys) : x > y
	}
}

/// Maps an argument into a comparable form for use with the xxxBy functions.
///
///
///     let x = [(2, 3), (1, 4), (4, 2), (5, 1), (3, 5)]
///     
///     sortBy(comparing(fst))(x) = [(1, 4), (2, 3), (3, 5), (4, 2), (5, 1)]
///     sortBy(comparing(snd))(x) = [(5, 1), (4, 2), (2, 3), (1, 4), (3, 5)]
public func comparing<A : Comparable, B>(p : B -> A) -> B -> B -> Bool {
	return { x in { y in p(x) < p(y) } }
}

/// Takes a list and groups its arguments into sublists of duplicate elements found next to each
/// other according to an equality predicate.
public func groupBy<A>(cmp : A -> A -> Bool) -> [A] -> [[A]] {
	return { l in
		switch destruct(l) {
			case .Empty:
				return []
			case .Destructure(let x, let xs):
				let (ys, zs) = span(cmp(x))(xs)
				return (x <| ys) <| groupBy(cmp)(zs)
		}
	}
}

/// Takes a list and groups its arguments into sublists of duplicate elements found next to each
/// other according to an equality operator.
{'fraction_non_alphanumeric': 0.15688073394495414, 'fraction_numerical': 0.007339449541284404, 'mean_word_length': 2.9859841560024374, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4249246', 'n_tokens_mistral': 2390, 'n_tokens_neox': 2118, 'n_words': 873}