Created
December 12, 2014 20:49
-
-
Save jarsen/b741462a051fcd9b8a86 to your computer and use it in GitHub Desktop.
Functional Swift — Counting Frequencies
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
func update<K,V>(var dictionary: [K:V], key: K, value: V) -> [K:V] { | |
dictionary[key] = value | |
return dictionary | |
} | |
func increment<T>(dictionary: [T:Int], key: T) -> [T:Int] { | |
return update(dictionary, key, map(dictionary[key]) {$0 + 1} ?? 1) | |
} | |
func histogram<T>(var s: [T]) -> [T:Int] { | |
return reduce(s, [T:Int](), increment) | |
} | |
let foo = histogram([1,2,3,1,4,1,1,2,3]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment