Last active
June 26, 2019 04:06
-
-
Save yanil3500/65641b656b51a01fff122554800a04ef to your computer and use it in GitHub Desktop.
[Swift Convenience Extensions] A list of handy extensions on the String, Array, and Character types.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Array where Element: Numeric { | |
/// Returns a number that is sum of the elements in the sequence. | |
func sum() -> Element { | |
return self.reduce(0) { (res, next) -> Element in | |
res + next | |
} | |
} | |
} | |
extension Character { | |
private static let EMPTY_STRING_ASCII_VALUE = 32 | |
/// A Boolean value indicating whether this is an empty "" character. | |
var isEmpty: Bool { | |
return self.isASCII && self.asciiValue! == Character.EMPTY_STRING_ASCII_VALUE | |
} | |
} | |
/// `CharacterCount` is a type alias for a tuple containing both the string to be used in the characterCount operation and the output of the characterCount operation, which is a dictionary where each key is a character and its associated value is the count of its key. | |
typealias CharacterCount = (string: String, count: [Character: Int]) | |
extension String { | |
/// Returns a tuple that contains the target string and the dictionary in which the keys are the characters in the string and the values are the count of those characters. | |
func characterCount() -> CharacterCount { | |
var tuple: CharacterCount = (string: self, count: [:]) | |
self.forEach { c in | |
if !c.isEmpty { | |
let val = tuple.count[c, default: 0] | |
tuple.count[c] = val + 1 | |
} | |
} | |
return tuple | |
} | |
} | |
extension Array where Element == String { | |
/// Returns an Int that is the sum of all the character counts in each element. | |
func totalCharacterCount() -> Int { | |
return self.reduce(0) { (res, next) -> Int in | |
res + next.count | |
} | |
} | |
/// Returns an Int that is equal to the number of strings in the array. | |
func wordCount() -> Int { | |
return self.reduce(0) { (res, next) -> Int in | |
res + next.components(separatedBy: " ").count | |
} | |
} | |
/// Splits each string on ' ', flattens the intermediate array and returns it | |
func flattenStringArray() -> [String] { | |
return self.map { $0.components(separatedBy: " ") }.flatMap { $0 } | |
} | |
/// Returns an array of dictionaries, where each dictionary represents the counts of each character in every element. | |
func characterCountForEachElement() -> [CharacterCount] { | |
return self.map { $0.characterCount() } | |
} | |
} | |
var names = ["Jon Snow", "Davos Seaworth", "Tyrion Lannister", "Margaery Tyrell", "Walder Frey", "Balon Greyjoy", "Arya Stark", "Petyr Baelish"] | |
names = names.flattenStringArray() | |
names.totalCharacterCount() | |
for (string, count) in names.characterCountForEachElement() { | |
print("\(string): \(count)") | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment