Skip to content

Instantly share code, notes, and snippets.

View thexande's full-sized avatar

Alexander Murphy thexande

  • Denver, CO, USA, Planet Earth
View GitHub Profile
@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 / 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 }
}
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 / 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
}

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.

struct Stack<T> {
private var storage = [T]()
mutating func push(_ item: T) {
storage.append(item)
}
mutating func pop() -> T? {
return storage.removeLast()
}
@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]
}
}
let test = "abba"
class Solution {
func expandPalindrome(input: String, startIndex: Int, endIndex: Int) -> (Int, Int) {
var start = startIndex
var end = endIndex
let inputArray = Array(input)
var result = (start, end)
@thexande
thexande / Valid Binary Search Tree.swift
Last active January 22, 2020 05:15
Determine if a given binary search tree is valid.
final class Node {
let left: Node?
let right: Node?
let value: Int
init(left: Node? = nil, right: Node? = nil, value: Int) {
self.left = left
self.right = right
self.value = value
}
final class RansomNoteBuilder {
func isValidRansomNote(note: String, in magazine: String) -> Bool {
var lookup = magazine.reduce(into: [Character:Int]()) { dictionary, letter in
dictionary[letter] = (dictionary[letter] ?? 0) + 1
}
for character in note {
guard let count = lookup[character], count > 0 else { return false }
lookup[character] = count - 1