Skip to content

Instantly share code, notes, and snippets.

View Kdan's full-sized avatar

Kewin Remeczki Kdan

  • Copenhagen, Denmark
View GitHub Profile
@Kdan
Kdan / 9.swift
Last active April 11, 2018 16:44
enum PersonCodingKeys: String, CodingKey {
case name
case pets
}
// CHALLENGE 1: Nested Heterogeneous Array Decoded.
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: PersonCodingKeys.self)
name = try container.decode(String.self, forKey: .name)
extension KeyedDecodingContainer {
/// Decode a heterogeneous list of objects for a given family.
/// - Parameters:
/// - family: The ClassFamily enum for the type family.
/// - key: The CodingKey to look up the list in the current container.
/// - Returns: The resulting list of heterogeneousType elements.
func decode<T : Decodable, U : ClassFamily>(family: U.Type, forKey key: K) throws -> [T] {
var container = try self.nestedUnkeyedContainer(forKey: key)
var list = [T]()
@Kdan
Kdan / 10.swift
Last active February 18, 2019 22:05
// CHALLENGE 1: Nested Heterogeneous Array Decoded.
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: PersonCodingKeys.self)
name = try container.decode(String.self, forKey: .name)
pets = try container.decode(family: PetFamily.self, forKey: .pets)
}
@Kdan
Kdan / BoilerplateDistance.swift
Created February 9, 2019 12:36
An example of a boilerplate total distance function for a list of CLLocation objects.
func totalDistance(of locations: [CLLocation]) -> CLLocationDistance {
var distance: CLLocationDistance = 0.0
var previousLocation: CLLocation?
locations.forEach { location in
if let previousLocation = previousLocation {
distance += location.distance(from: previousLocation)
}
previousLocation = location
}
@Kdan
Kdan / RecursiveDistance.swift
Created February 9, 2019 13:18
An example of how a recursive total distance function can be implemented on a list of CLLocation objects.
func totalDistance(initialDistance: CLLocationDistance, initialLocation: CLLocation?, locations: inout [CLLocation]) -> CLLocationDistance {
if locations.isEmpty {
return initialDistance
} else if let initialLocation = initialLocation {
let firstLocation = locations.removeFirst()
let distance = firstLocation.distance(from: initialLocation)
return totalDistance(initialDistance: initialDistance + distance, initialLocation: firstLocation, locations: &locations)
}
return totalDistance(initialDistance: initialDistance, initialLocation: locations.removeFirst(), locations: &locations)
}
@Kdan
Kdan / ReduceClosure.swift
Created February 9, 2019 14:26
An example of a closure used to perform a reduce on a list of CLLocation elements to calculate the total distance.
let closure: ((CLLocationDistance, CLLocation?), CLLocation) -> (CLLocationDistance, CLLocation?) = { tuple, location in
guard let previousLocation = tuple.1 else {
return (tuple.0, location)
}
return (tuple.0 + location.distance(from: previousLocation), location)
}
@Kdan
Kdan / ReduceClosureOneLine.swift
Created February 9, 2019 14:40
An example of a closure.
let closure: ((CLLocationDistance, CLLocation?), CLLocation) -> (CLLocationDistance, CLLocation) = { tuple, location in
return (tuple.0 + location.distance(from: tuple.1 ?? location), location)
}
@Kdan
Kdan / ReduceSolution.swift
Created February 9, 2019 14:48
An example of how the reduce function can be used to calculate the total distance between the elements in a sequence of CLLocation objects.
func totalDistance(of locations: [CLLocation]) -> CLLocationDistance {
return locations.reduce((0.0, nil), { ($0.0 + $1.distance(from: $0.1 ?? $1), $1) }).0
}
@Kdan
Kdan / Array+CLLocation.swift
Created February 9, 2019 15:00
An example of how Arrays of CLLocation objects can be extended with a nice computed property calculating the total distance.
extension Array where Element: CLLocation {
/// Returns the total distance of the list of CLLocation objects.
var totalDistance: CLLocationDistance {
return reduce((0.0, nil), { ($0.0 + $1.distance(from: $0.1 ?? $1), $1) }).0
}
}
@Kdan
Kdan / TotalDistance.swift
Last active December 9, 2021 03:25
A Swift Playground for total distance functions on lists of CLLocation objects. https://medium.com/@kewindannerfjordremeczki/swift-4-2-corelocation-total-distance-traveled-ff881ba2a1ce
import CoreLocation
/// The locations used for the examples.
let locations = [CLLocation(latitude: 10.0, longitude: 12.0),
CLLocation(latitude: 10.1, longitude: 12.0),
CLLocation(latitude: 10.2, longitude: 12.0),
CLLocation(latitude: 10.1, longitude: 12.0)]
/* A BOILERPLATE SOLUTION */