Skip to content

Instantly share code, notes, and snippets.

View nikriek's full-sized avatar
🚀
Flying to the moon

Niklas Riekenbrauck nikriek

🚀
Flying to the moon
View GitHub Profile

Keybase proof

I hereby claim:

  • I am nikriek on github.
  • I am nikriek (https://keybase.io/nikriek) on keybase.
  • I have a public key ASB1XtC9LqK7VEbMHJum0gFDe6D0qEIPGNDNwldvN3jcXAo

To claim this, I am signing this object:

@nikriek
nikriek / Array+ReduceByKey.swift
Last active July 24, 2017 12:25
Functional Reduce By Key
extension Array {
/// GroupBy that puts each element into a group of similiar elements denoted by an Equatable attribute, complexity: O(n)
func reduce<G: Equatable>(by key: (Element) -> (G)) -> [G: [Element]] {
return reduce([:], { (result, element) in
var groups = result
let group = key(element)
if let existingGroupElements = groups[group] {
groups[group] = existingGroupElements + [element]
} else {