Last active
August 26, 2015 21:56
-
-
Save erica/9cd98d94acd35d7a50f5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
let string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi enim lacus, ullamcorper in gravida a, semper id dolor. Mauris quis metus id" | |
extension String { | |
public func split( | |
maxSplit: Int = .max, | |
allowEmptySlices: Bool = false, | |
@noescape isSeparator: ((Character) throws -> Bool)) rethrows -> [String] { | |
return try self.characters.split( | |
maxSplit, | |
allowEmptySlices: allowEmptySlices, | |
isSeparator: isSeparator) | |
.map({String($0)}) | |
} | |
public enum StringSplitError : ErrorType {case MissingCharacter} | |
public func split(characterString: String) throws -> [String] { | |
guard let c = characterString.characters.first else { | |
throw StringSplitError.MissingCharacter | |
} | |
return self.split(isSeparator: {$0 == c}) | |
} | |
} | |
func dotry(block: () throws -> Void) { | |
do {try block()} catch {print(error)} | |
} | |
dotry { | |
let words = try string.split(" ") | |
//let counts = lazy(words).map(count) | |
let counts = words.lazy.map({$0.characters.count}) | |
print(Array(counts)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment