Skip to content

Commit

Permalink
Add CC_dictionary, a convenient way of creating Dictionaries from Seq…
Browse files Browse the repository at this point in the history
…uences
  • Loading branch information
mickeyl committed Nov 22, 2024
1 parent 94d98a0 commit f3edbdf
Showing 1 changed file with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Cornucopia – (C) Dr. Lauer Information Technology
//
public extension Sequence {

/// Create dictionary from sequence via keypath and value transformer.
func CC_dictionary<Key, Value>(keyPath: KeyPath<Element, Key>, valueTransformer: (Element) -> Value) -> [Key: Value] {
self.reduce(into: [Key: Value]()) { result, element in
result[element[keyPath: keyPath]] = valueTransformer(element)
}
}
}

0 comments on commit f3edbdf

Please sign in to comment.