Skip to content

Instantly share code, notes, and snippets.

View akhilcb's full-sized avatar
💭
I may be slow to respond.

Akhil Choran Balan akhilcb

💭
I may be slow to respond.
View GitHub Profile
@akhilcb
akhilcb / Array+Extension.swift
Created November 21, 2017 00:57
Swift Array extension to insert value in a sorted array at correct index. Index is found by using binary search.
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 {
@akhilcb
akhilcb / Squareroot.swift
Created July 9, 2017 02:59
Finding Squareroot of a number without using any maths function(Efficient algorithm)
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 {
@akhilcb
akhilcb / CGPoint+Extension.swift
Created May 7, 2017 23:22
CGPoint extension with some useful functions for angle and point calculation on arc and circles
// 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)
@akhilcb
akhilcb / Array+Extension.Swift
Created February 25, 2017 05:53
Array/Sequence Extension Swift for mapUnique function to get unique and distinct properties mapped from any data structure which inherits Sequence
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
}