This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension FixedWidthInteger where Self: SignedInteger { | |
func roundedUpDivision(by divisor: Self) throws -> Self { | |
guard divisor != .zero else { | |
throw Division.Error.divideByZero | |
} | |
guard !(self == .min && divisor == -1) else { | |
throw Division.Error.overflow | |
} | |
let roundedTowardsZeroQuotient = self / divisor | |
if isMultiple(of: divisor) { return roundedTowardsZeroQuotient } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Folow up on Vacawama post on StackOverflow post https://stackoverflow.com/questions/64094640/every-value-combination-of-dictionary/64094971?noredirect=1#comment113341385_64094971 | |
extension Dictionary where Value: Collection { | |
func permutations() -> [[Key: Value.Element]] { | |
guard !isEmpty else { return [] } | |
var permutations: [[Key: Value.Element]] = [] | |
permutate(&permutations) | |
return permutations | |
} | |
private func permutate(_ permutations: inout [[Key: Value.Element]], _ dictionaries: [[Key: Value.Element]] = []) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// String+IntRange.swift | |
// | |
// Created by Zack Sheppard on 8/30/20. | |
// Copyright © 2020 Zack Sheppard. All rights reserved. | |
// Freely usable under the Apache 2.0 License. | |
// | |
import Foundation | |
/// This extension is freely available at |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// Swift 4.2.1 syntax | |
public struct SubstringMatchSource<S: StringProtocol> where S.Index == String.Index { | |
private let wrapped: S | |
public init(wrapping wrapped: S) { | |
self.wrapped = wrapped | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import MessageUI | |
class MailComposeViewController: MFMailComposeViewController, MFMailComposeViewControllerDelegate { | |
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { | |
controller.dismiss(animated: true) { | |
switch result { | |
case .cancelled: | |
print ("Canceled") | |
case .saved: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Contact: CustomStringConvertible { | |
let user: User | |
let address: Address | |
let deliveryInstruction: String | |
let deliveryMethod: String | |
// customize the description to your needs | |
var description: String { return "\(user.name) \(deliveryInstruction) \(deliveryMethod)" } | |
init(dictionary: [String: Any]) { | |
self.deliveryInstruction = dictionary["delivery_instruction"] as? String ?? "" | |
self.deliveryMethod = dictionary["delivery_method"] as? String ?? "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension BidirectionalCollection where Iterator.Element: FloatingPoint { | |
func multiplied(by multiplier: Iterator.Element) -> [Iterator.Element] { | |
return map{$0 * multiplier} | |
} | |
var squared: [Iterator.Element] { | |
return map{$0 * $0} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Point: Hashable { | |
let cgPoint: CGPoint | |
var nsValue: NSValue { return cgPoint as NSValue } | |
var x: CGFloat { return cgPoint.x } | |
var y: CGFloat { return cgPoint.y } | |
init(x: CGFloat, y: CGFloat) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension CGPoint: Hashable { | |
public var hashValue: Int { | |
return (self as NSValue).hashValue | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let myArray = [CGPoint(x: 0.1 + 0.2, y: 0), CGPoint(x: 0.3, y: 0), CGPoint(x: 2, y: 2), CGPoint(x: 2, y: 2)] | |
var counts = [String: Int]() | |
myArray.forEach { counts[$0.debugDescription] = (counts[$0.debugDescription] ?? 0) + 1 } | |
if let (value, count) = counts.max(by: {$0.value < $1.value}) { | |
print("\(value) occurs \(count) times") // "(0.3, 0.0) occurs 2 times\n" | |
} |
NewerOlder