Is there a purely functional way of writing frequency(of:) in this code?
struct GenericDataFrequency<T: Hashable & Comparable> {
var data: [T]
var frequencies: [T : Int] {
return Dictionary((data.map {($0, 1)}), uniquingKeysWith: {$0 + $1})
}
func frequency(of elements: [T]) -> [T : Int] {
var collector = [T : Int]()
for e in elements {
collector[e] = frequencies[e] ?? 0
}
return collector
}
}
I've been staring at it for ages and don't see it. This could be a valuable learning experience for me.