<commit_before>//
//  ArrayExt.swift
//  swiftz
//
//  Created by <NAME> on 3/06/2014.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

/// MARK: Array extensions

/// Safely indexes into an array by converting out of bounds errors to nils.
public func safeIndex<T>(array : Array<T>)(i : Int) -> T? {
	return indexArray(array, i)
}

/// Returns the result of concatenating the values in the left and right arrays together.
public func concat<T>(#lhs: [T])(#rhs : [T]) -> [T] {
	return lhs + rhs
}

/// Maps a function over an array that takes pairs of (index, element) to a different element.
public func mapWithIndex<T, U>(array : Array<T>)(f : (Int, T) -> U) -> [U] {
	var res = [U]()
	res.reserveCapacity(array.count)
	for i in 0 ..< array.count {
		res.append(f(i, array[i]))
	}
	return res
}

/// Folds a reducing function over an array from right to left.
public func foldRight<T, U>(array : Array<T>)(z : U, f : (T, U) -> U) -> U {
	var res = z
	for x in array {
		res = f(x, res)
	}
	return res
}

/// Takes a binary function, an initial value, and a list and scans the function across each element
/// of a list accumulating the results of successive function calls applied to reduced values from
/// the left to the right.
///
///     scanl(z, [x1, x2, ...], f) == [z, f(z, x1), f(f(z, x1), x2), ...]
public func scanl<B, T>(start : B, list : [T], r : (B, T) -> B) -> [B] {
	if list.isEmpty {
		return []
	}
	var arr = [B]()
	arr.append(start)
	var reduced = start
	for x in list {
		reduced = r(reduced, x)
		arr.append(reduced)
	}
	return Array(arr)
}

/// Returns the first element in a list matching a given predicate.  If no such element exists, this
/// function returns nil.
public func find<T>(list : [T], f : (T -> Bool)) -> T? {
	for x in list {
		if f(x) {
			return .Some(x)
		}
	}
	return .None
}

/// Returns a tuple containing the first n elements of a list first and the remaining elements
/// second.
///
///     splitAt(3, [1,2,3,4,5]) == ([1,2,3],[4,5])
///     splitAt(1, [1,2,3])     == ([1],[2,3])
///     splitAt(3, [1,2,3])     == ([1,2,3],[])
///     splitAt(4, [1,2,3])     == ([1,2,3],[])
///     splitAt(0, [1,2,3])     == ([],[1,2,3])
public func splitAt<T>(index : Int, list : [T]) -> ([T], [T]) {
    switch index {
    case 0..<list.count:
        return (Array(list[0..<index]), Array(list[index..<list.count]))
    case list.count...Int.max:
        return (list, [T]())
    default:
        return ([T](), [T]())
    }
}

/// Takes a separator and a list and intersperses that element throughout the list.
///
///     intersperse(",", ["a","b","c","d","e"] == ["a",",","b",",","c",",","d",",","e"]
public func intersperse<T>(item : T, list : [T]) -> [T] {
	func prependAll(item:T, array:[T]) -> [T] {
		var arr = Array([item])
		for i in 0..<(array.count - 1) {
			arr.append(array[i])
			arr.append(item)
		}<commit_msg>Do the tabs vs spaces dance<commit_after>//
//  ArrayExt.swift
//  swiftz
//
//  Created by <NAME> on 3/06/2014.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

/// MARK: Array extensions

/// Safely indexes into an array by converting out of bounds errors to nils.
public func safeIndex<T>(array : Array<T>)(i : Int) -> T? {
	return indexArray(array, i)
}

/// Returns the result of concatenating the values in the left and right arrays together.
public func concat<T>(#lhs: [T])(#rhs : [T]) -> [T] {
	return lhs + rhs
}

/// Maps a function over an array that takes pairs of (index, element) to a different element.
public func mapWithIndex<T, U>(array : Array<T>)(f : (Int, T) -> U) -> [U] {
	var res = [U]()
	res.reserveCapacity(array.count)
	for i in 0 ..< array.count {
		res.append(f(i, array[i]))
	}
	return res
}

/// Folds a reducing function over an array from right to left.
public func foldRight<T, U>(array : Array<T>)(z : U, f : (T, U) -> U) -> U {
	var res = z
	for x in array {
		res = f(x, res)
	}
	return res
}

/// Takes a binary function, an initial value, and a list and scans the function across each element
/// of a list accumulating the results of successive function calls applied to reduced values from
/// the left to the right.
///
///     scanl(z, [x1, x2, ...], f) == [z, f(z, x1), f(f(z, x1), x2), ...]
public func scanl<B, T>(start : B, list : [T], r : (B, T) -> B) -> [B] {
	if list.isEmpty {
		return []
	}
	var arr = [B]()
	arr.append(start)
	var reduced = start
	for x in list {
		reduced = r(reduced, x)
		arr.append(reduced)
	}
	return Array(arr)
}

/// Returns the first element in a list matching a given predicate.  If no such element exists, this
/// function returns nil.
public func find<T>(list : [T], f : (T -> Bool)) -> T? {
	for x in list {
		if f(x) {
			return .Some(x)
		}
	}
	return .None
}

/// Returns a tuple containing the first n elements of a list first and the remaining elements
/// second.
///
///     splitAt(3, [1,2,3,4,5]) == ([1,2,3],[4,5])
///     splitAt(1, [1,2,3])     == ([1],[2,3])
///     splitAt(3, [1,2,3])     == ([1,2,3],[])
///     splitAt(4, [1,2,3])     == ([1,2,3],[])
///     splitAt(0, [1,2,3])     == ([],[1,2,3])
public func splitAt<T>(index : Int, list : [T]) -> ([T], [T]) {
	switch index {
	case 0..<list.count:
		return (Array(list[0..<index]), Array(list[index..<list.count]))
	case list.count...Int.max:
		return (list, [T]())
	default:
		return ([T](), [T]())
	}
}

/// Takes a separator and a list and intersperses that element throughout the list.
///
///     intersperse(",", ["a","b","c","d","e"] == ["a",",","b",",","c",",","d",",","e"]
public func intersperse<T>(item : T, list : [T]) -> [T] {
	func prependAll(item:T, array:[T]) -> [T] {
		var arr = Array([item])
		for i in 0..<(array.count - 1) {
			arr.append(array[i])
			arr.append(item)
		}
{'fraction_non_alphanumeric': 0.20883534136546184, 'fraction_numerical': 0.020953378732320588, 'mean_word_length': 2.8572390572390574, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 55, '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': '1639496', 'n_tokens_mistral': 2254, 'n_tokens_neox': 2138, 'n_words': 695}