Skip to content

Instantly share code, notes, and snippets.

View acrookston's full-sized avatar
👨‍💻
Coding!

Andrew Crookston acrookston

👨‍💻
Coding!
View GitHub Profile
@acrookston
acrookston / UIColorExtensions.swift
Created April 26, 2017 22:26
UIColor extension for creating UIColor with rgba() values
import UIKit
extension UIColor {
static func rgba(_ r: CGFloat, _ g: CGFloat, _ b: CGFloat, _ a: CGFloat) -> UIColor {
return UIColor(red: r / 255.0, green: g / 255.0, blue: b / 255.0, alpha: a)
}
static var lightBackground: UIColor { return .rgba(246, 246, 246, 1) }
static var text: UIColor { return .rgba(51, 51, 51, 1) }
}
import Foundation
// Given some sorted dates find out if the set contains 5 dates within 30 days of eachother.
// I.e. Given a user's login dates, find out if they were logged in 5 or more times in 30 days.
extension Date {
static func parse(_ str: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
return dateFormatter.date(from: str)!
@acrookston
acrookston / sorting_tree.rb
Last active April 29, 2017 19:29
Tree sorting in ruby
# Question: https://www.dropbox.com/s/4dlhs6x69cmj8uv/Screenshot%202017-04-29%2012.21.05.png?dl=0
# Time limit: 30min
# Finished in 28min
# It should be possible to load the tree in one pass. I just didn't have time to rewrite that part.
require "rubygems"
require "json"
locations = JSON.parse(STDIN.read)
class Node
@acrookston
acrookston / roman_numeral.swift
Created May 9, 2017 00:24
Convert roman numerals to integers
import Foundation
let values: [Character: Int] = [
"I" : 1,
"V" : 5,
"X" : 10,
"L" : 50,
"C" : 100,
"D" : 500,
"M" : 1000
@acrookston
acrookston / git-purge.sh
Last active May 19, 2017 18:30
Deletes local and remote branch. Add as alias in .gitconfig `purge = "!git-purge $1"`
#!/bin/sh
if [ -z "$1" ]
then
echo "Usage: git-purge [branch]"
exit
fi
if [ "master" == "$1" ]
then
@acrookston
acrookston / config_puma.rb
Last active August 26, 2017 23:47
puma config for rvm, capistrano 3 and upstart. Tested on Ubuntu 12.04 LTS and 14.04 LTS
#!/usr/bin/env puma
app_path = '/var/www/YOUR_APP_NAME/current'
directory app_path
environment 'production'
daemonize true
pidfile "#{app_path}/tmp/pids/puma.pid"
state_path "#{app_path}/tmp/pids/puma.state"
stdout_redirect "#{app_path}/log/puma_error.log", "#{app_path}/log/puma_access.log", true
threads 0,16
@acrookston
acrookston / CallbackViewModel.swift
Last active September 6, 2017 20:18
Demonstrating MVVM View Model with multiple observers in Swift
class CallbackViewModel {
typealias DidUpdateCallback = ((Void) -> Void)
private var didUpdateCallbacks = [DidUpdateCallback?]()
// Have to remember to use `[weak self] in` in your callback!
func addObserver(_ callback: @escaping DidUpdateCallback) {
didUpdateCallbacks.append(callback)
}
// Results:
// 0.40788197517395 - IteratedEnum (it's slow due to EnumCollection)
// 0.282593011856079 - StaticIteratedEnum
// 0.152174055576324 - SwitchedEnum
// 0.159240961074829 - OtherSwitchedEnum
import Foundation
// MARK: - Benchmark - gotta know how fast it is!
/*******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2017 Jean-David Gadina - www.xs-labs.com
* Original: https://github.com/macmade/user-defaults/blob/master/swift/Preferences.swift
*
* Copyright (c) 2017 Andrew Crookston - andrewcrookston.com
* Contributor: https://gist.github.com/acrookston/f973137b9adbd9bff3589ca2cd1d1cb8
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
input = "3893445835429722678558456317563893861752455542588369533636585887178232467588827193173595918648538852463974393264428538856739259399322741844613957229674619566966921656443476317729968764183945899765294481327998956154956571467872487576314549468261122281384513266834769436913544431258253346374641589492728885222652146158261225296144835682556133922436438188211288458692217737145834468534829945993366314375465767468939773939978272968388546791547526366348163672162245585168892858977723516752284597322176349412485116173844733679871253985762643852151748396593275274582481295864991886985988427966155944392352248314629138972358467959614279553511247863869663526823326467571462371663396188951696286916979923587358992127741723727623235238531991996999181976664226274715591531566495345212849683589582225465555847312199122268773923175183128124556249916458878785361322713513153175157855597289482439449732469754748544437553251412476225415932478849961897299721228198262823515159848941742786272262236888514421279147329383465929358896761