Skip to content

Instantly share code, notes, and snippets.

View thexande's full-sized avatar

Alexander Murphy thexande

  • Ibotta
  • Denver, CO, USA, Planet Earth
View GitHub Profile
@thexande
thexande / Contiguous Colors In Grid: Iterative.swift
Last active November 18, 2019 17:53
An iterative approach to solving a variation of the flood fill problem in swift
extension Array {
public subscript(safe index: Int) -> Element? {
guard index >= 0, index < endIndex else {
return nil
}
return self[index]
}
}
struct Stack<T> {
private var storage = [T]()
mutating func push(_ item: T) {
storage.append(item)
}
mutating func pop() -> T? {
return storage.removeLast()
}

Privacy Policy

Alexander Murphy built the UI Gradients Viewer app as a Free app. This SERVICE is provided by Alexander Murphy at no cost and is intended for use as is.

This page is used to inform visitors regarding my policies with the collection, use, and disclosure of Personal Information if anyone decided to use my Service.

If you choose to use my Service, then you agree to the collection and use of information in relation to this policy. The Personal Information that I collect is used for providing and improving the Service. I will not use or share your information with anyone except as described in this Privacy Policy.

The terms used in this Privacy Policy have the same meanings as in our Terms and Conditions, which is accessible at UI Gradients Viewer unless otherwise defined in this Privacy Policy.

@thexande
thexande / SwitchRootViewController.swift
Created October 30, 2019 02:59
A window extension to switch root view controllers in an iOS app.
extension UIWindow {
func switchRootViewController(_ viewController: UIViewController,
animated: Bool = true,
duration: TimeInterval = 0.5,
options: UIView.AnimationOptions = .transitionFlipFromBottom,
completion: (() -> Void)? = nil) {
guard animated else {
rootViewController = viewController
return
}
import Foundation
var stack = [Character]()
func isPalendrome(subject: String) -> Bool {
var reversed = ""
var sanitized = ""
for character in subject where character.isLetter {
sanitized.append(character)
@thexande
thexande / EmojiCode.swift
Created August 19, 2019 16:48
Using emojis as class names.
import Foundation
protocol JuiceContaining {
var volume: Double { get }
var juiceRatio: Double { get }
}
protocol DrinkProducing {
var drinkVolume: Double { get }
}
@thexande
thexande / SwiftTipKeypathMapping.swift
Created August 19, 2019 02:32
Extend map to support mapping to object key paths.
extension Sequence {
func map<T>(_ keyPath: KeyPath<Element, T>) -> [T] {
return self.map {
$0[keyPath: keyPath]
}
}
}
struct Transaction {
let amount: Double
@thexande
thexande / Thread.swift
Created April 24, 2019 21:13
Print current thread information
extension Thread {
class func printCurrent() {
print("\r⚡️: \(Thread.current)\r" + "🏭: \(current.threadName)\r")
}
var threadName: String {
if let currentOperationQueue = OperationQueue.current?.name {
return "OperationQueue: \(currentOperationQueue)"
}
else if let underlyingDispatchQueue = OperationQueue.current?.underlyingQueue?.label {
@thexande
thexande / MergeSort.cpp
Created April 16, 2019 14:41
C++ Merge Sort implementation
/* C program for Merge Sort */
#include<stdlib.h>
#include<stdio.h>
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
@thexande
thexande / Throttler.swift
Created February 19, 2019 18:06
throttle implementation class in swift
final class Throttler {
private var throttleWorkItems: [AnyHashable: DispatchWorkItem] = [:]
private var lastDebounceCallTimes: [AnyHashable: DispatchTime] = [:]
private let nilContext: AnyHashable = arc4random()
/// Delays a closure execution and ensures no other executions are made during deadline
///
/// - Parameters:
/// - deadline: The timespan to delay a closure execution