Skip to content

Instantly share code, notes, and snippets.

@marty-suzuki
Last active February 19, 2020 13:16
Show Gist options
  • Save marty-suzuki/0936166e8d1105db50abda4c58334703 to your computer and use it in GitHub Desktop.
Save marty-suzuki/0936166e8d1105db50abda4c58334703 to your computer and use it in GitHub Desktop.
/// - seealso: https://www.hackingwithswift.com/articles/212/whats-new-in-swift-5-2
import Foundation
protocol OptionalType {
associatedtype Wrapped
var value: Wrapped? { get }
}
extension Optional: OptionalType {
var value: Wrapped? { self }
}
@dynamicMemberLookup
struct Map<Collection: Swift.Collection> {
let collection: Collection
subscript<Value>(dynamicMember keyPath: KeyPath<Collection.Element, Value>) -> [Value] {
collection.map { $0[keyPath: keyPath] }
}
}
@dynamicMemberLookup
struct Filter<Collection: Swift.Collection> {
let collection: Collection
subscript(dynamicMember keyPath: KeyPath<Collection.Element, Bool>) -> [Collection.Element] {
collection.filter { $0[keyPath: keyPath] }
}
}
@dynamicMemberLookup
struct CompactMap<Collection: Swift.Collection> {
let collection: Collection
subscript<Value>(dynamicMember keyPath: KeyPath<Collection.Element, Value>) -> [Value] {
collection.compactMap { $0[keyPath: keyPath] }
}
subscript<Value: OptionalType>(dynamicMember keyPath: KeyPath<Collection.Element, Value>) -> [Value.Wrapped] {
collection.compactMap { $0[keyPath: keyPath].value }
}
}
protocol DynamicKePathLiteralFunctionExpressions: Collection {
var map: Map<Self> { get }
var filter: Filter<Self> { get }
var compactMap: CompactMap<Self> { get }
}
extension DynamicKePathLiteralFunctionExpressions {
var map: Map<Self> { .init(collection: self) }
var filter: Filter<Self> { .init(collection: self) }
var compactMap: CompactMap<Self> { .init(collection: self) }
}
extension Array: DynamicKePathLiteralFunctionExpressions {}
struct User {
let name: String
let age: Int
let bestFriend: String?
var canVote: Bool {
age >= 18
}
}
let eric = User(name: "Eric Effiong", age: 18, bestFriend: "Otis Milburn")
let maeve = User(name: "Maeve Wiley", age: 19, bestFriend: nil)
let otis = User(name: "Otis Milburn", age: 17, bestFriend: "Eric Effiong")
let users = [eric, maeve, otis]
let userNames: [String] = users.map.name
print(userNames)
let voters: [User] = users.filter.canVote
print(voters)
let bestFriends: [String] = users.compactMap.bestFriend
print(bestFriends)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment