Skip to content

Instantly share code, notes, and snippets.

View arashkashi's full-sized avatar
💭
Working with machines against the machine

A.K. arashkashi

💭
Working with machines against the machine
View GitHub Profile
@arashkashi
arashkashi / mergeSort.swift
Last active August 31, 2017 18:35
Merge Sort
public func mergeSort<T: Comparable>(_ A : inout [T]) -> [T] {
if A.count == 2 {
if A[0] > A[1] { return [A[1], A[0]] } else {
return [A[0], A[1]]
}
@arashkashi
arashkashi / max_slice.swift
Created September 12, 2017 09:27
Max double slice
public func solution(_ A : inout [Int]) -> Int {
if A.count <= 3 { return 0 }
// write your code in Swift 3.0 (Linux)
// handle special cases
var toRight: [Int] = Array(repeating: 0, count: A.count - 2)
var toLeft: [Int] = Array(repeating: 0, count: A.count - 2)
for (index, _) in A.enumerated() {
@arashkashi
arashkashi / prime_factors.swift
Created September 17, 2017 15:13
get the prime factors
func primeFactor(n: Int) -> [Int] {
var i: Int = 2
var A: [Int] = Array(repeating: 0, count: n - 1)
var result: [Int] = []
while i < n/2 {
var counter: Int = 0
var index: Int = i * i + i * counter - 2
@arashkashi
arashkashi / NetworkManager.swift
Last active December 11, 2017 09:35
Example of communicating with Github service.
//
// NetworkManager.swift
// DAC-Tracker
//
// Created by Arash K. on 2017-10-19.
// Copyright © 2017 Arash K. All rights reserved.
//
import Foundation
@arashkashi
arashkashi / auth2.swift
Created September 24, 2017 17:33
Github based auth2 class
class Authentication: NSObject, KnowsRootViewController, SFSafariViewControllerDelegate {
static let shared = Authentication()
private static let clientId: String = "666"
private static let clientSecret: String = "777777"
private static let loginURL = URL(string: "https://github.com/login/oauth/authorize?scope=user:email&client_id=\(Authentication.clientId)")!
private static let logoutURL = URL(string: "https://github.com/logout")!
@arashkashi
arashkashi / pageInfo.swift
Created October 1, 2017 09:46
based on visible cells of a collection view, extract which page and how many per page should be fetched
typealias PageInfo = (page: Int16, perPage: Int16)
class SwiftyCollectionCell: UICollectionViewCell {
var row: Int16 = 0
}
extension Collection where Iterator.Element == SwiftyCollectionCell {
func pageInfo(total: Int16 = Int16.max) -> PageInfo? {
@arashkashi
arashkashi / BinarySearch.swift
Created November 6, 2017 19:41
binary search
func binarySearch(item: Int, in A: [Int]) -> Int? {
var beg = 0
var end = A.count - 1
var result: Int?
while end - beg >= 1 {
var mid = (beg + end) / 2
@arashkashi
arashkashi / ConAsyncFunc.swift
Created November 18, 2017 17:38
ConcatinateAsyncFuncs
infix operator --> :AdditionPrecedence
func --><InputA, CompA, CompB>(first: @escaping ( InputA, @escaping (CompA?) -> () ) -> (),
second: @escaping ( CompA, @escaping (CompB?) -> () ) -> ()) -> (InputA, @escaping (CompB?) -> ()) -> () {
return { (input: InputA, compB: @escaping (CompB?) -> () ) in
first(input, { compA in
guard let validCompaA = compA else { compB(nil); return }
@arashkashi
arashkashi / AsynchronousOperation.swift
Last active September 26, 2019 09:01 — forked from Sorix/AsynchronousOperation.swift
Subclass of NSOperation to make it asynchronous in Swift 3
//
// AsynchronousOperation.swift
//
// Created by Vasily Ulianov on 09.02.17.
// Copyright © 2017 Vasily Ulianov. All rights reserved.
//
import Foundation
/// Subclass of `Operation` that add support of asynchronous operations.
@arashkashi
arashkashi / func_button.swift
Last active December 21, 2017 11:51
Functional Button
typealias UIButtonTargetClosure = (UIButton) -> ()
class ClosureWrapper: NSObject {
let closure: UIButtonTargetClosure
init(_ closure: @escaping UIButtonTargetClosure) {
self.closure = closure
}
}
extension UIButton {