Last active
August 29, 2015 14:16
-
-
Save designatednerd/230a97679739303730ce to your computer and use it in GitHub Desktop.
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
/* | |
Some playing around with a playground in Swift to try and figure out how to make Funcitonal | |
stuff more readable. Initial example from: | |
http://www.raywenderlich.com/82599/swift-functional-programming-tutorial | |
*/ | |
import Foundation | |
let words = [ "Cat", "Chicken", "fish", "Dog", "Mouse", "Guinea Pig", "monkey"] | |
typealias Entry = (Character, [String]) | |
//Example 1: Imperative | |
//func buildIndex(words: [String]) -> [Entry] { | |
// var result = [Entry]() | |
// | |
// var letters = [Character]() | |
// for word in words { | |
// let firstLetter = Character(word.substringToIndex(advance(word.startIndex, 1)).uppercaseString) | |
// | |
// if !contains(letters, firstLetter) { | |
// letters.append(firstLetter) | |
// } | |
// } | |
// | |
// for letter in letters { | |
// var wordsForLetter = [String]() | |
// for word in words { | |
// let firstLetter = Character(word.substringToIndex(advance(word.startIndex, 1)).uppercaseString) | |
// | |
// if firstLetter == letter { | |
// wordsForLetter.append(word) | |
// } | |
// } | |
// | |
// result.append((letter, wordsForLetter)) | |
// } | |
// | |
// return result | |
//} | |
// | |
//println(buildIndex(words)) | |
//Helper functions | |
func distinct<T: Equatable>(source:[T]) -> [T] { | |
var unique = [T]() | |
for item in source { | |
if !contains(unique, item) { | |
unique.append(item) | |
} | |
} | |
return unique | |
} | |
func firstLetter(str: String) -> Character { | |
return Character(str.substringToIndex(advance(str.startIndex, 1)).uppercaseString) | |
} | |
//Example 2: Pure functional | |
func megaFunctionalIndex(words: [String]) -> [Entry] { | |
return sorted(distinct(words.map(firstLetter))).map { | |
(letter) -> Entry in | |
return (letter, words.filter { | |
(word) -> Bool in | |
firstLetter(word) == letter | |
}) | |
} | |
} | |
println("MEGA FUNCTIONAL!!!") | |
println(megaFunctionalIndex(words)) | |
//Example 3: Somewhat easier to read functional | |
func buildIndex(words: [String]) -> [Entry] { | |
func entryForLetter(letter: Character) -> Entry { | |
func stringsForLetter(letter: Character) -> [String] { | |
return words.filter { | |
(word) -> Bool in | |
firstLetter(word) == letter | |
} | |
} | |
return (letter, stringsForLetter(letter)) | |
} | |
let firstLetters = words.map(firstLetter) | |
let distinctFirstLetters = distinct(firstLetters) | |
let sortedFirstLetters = sorted(distinctFirstLetters, { $0 < $1 }) | |
return sortedFirstLetters.map(entryForLetter) | |
} | |
println("READABLE!!!") | |
println(buildIndex(words)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment