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 Array where Element: Comparable { | |
//insert item to sorted array | |
mutating func insertSorted(newItem item: Element) { | |
let index = insertionIndexOf(elem: item) { $0 < $1 } | |
insert(item, at: index) | |
} | |
} | |
extension Array { |
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
func squareroot(number: Double) -> Double { | |
if number <= 0 { | |
return 0 | |
} | |
var x = number / 3 | |
var y = number / x | |
let maxDiff: Double = 0.0001 | |
while abs(y - x) > maxDiff { |
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
// Taken from ACBRadialMenuView Project | |
// BSD 3-Clause License | |
// Copyright (c) 2017, akhilcb (https://github.com/akhilcb) | |
extension CGPoint { | |
static func pointOnCircle(center: CGPoint, radius: CGFloat, angle: CGFloat) -> CGPoint { | |
let x = center.x + radius * cos(angle) | |
let y = center.y + radius * sin(angle) | |
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
public extension Sequence { | |
public func mapUnique<T: Equatable>(_ transform: (Iterator.Element) -> T) -> [T] { | |
var result: [T] = [] | |
//takes O(n^2) time since T is not Hashable type | |
for element in self { | |
let transformedElement: T = transform(element) | |
if result.contains(transformedElement) { | |
continue | |
} |