Skip to content

Instantly share code, notes, and snippets.

View jnd-au's full-sized avatar
🇦🇺

James D jnd-au

🇦🇺
View GitHub Profile
@jnd-au
jnd-au / groupBy.swift
Last active June 14, 2018 07:51
Swift: Group array values into a dictionary by key mapping (Curryable version)
// Given an array collection (may be a Swift Array or an Objective-C NSArray)
// and a function that optionally maps each element to its corresponding group,
// return a Swift Dictionary of the mapped elements in their groups.
//
// Usage:
// func keyFunc(o: V) -> K {...}
// let grouped = groupBy(keyFunc)([V])
//
// Example:
// func length(s: String) -> Int { return countElements(s) }