Skip to content

Instantly share code, notes, and snippets.

View mickmaccallum's full-sized avatar
🤘

Mick MacCallum mickmaccallum

🤘
View GitHub Profile
@mickmaccallum
mickmaccallum / scheduler.ex
Created November 9, 2016 23:41 — forked from danielberkompas/scheduler.ex
A simple mix task scheduler for Elixir apps
defmodule MyApp.Scheduler do
@moduledoc """
Schedules a Mix task to be run at a given interval in milliseconds.
## Options
- `:task`: The name of the Mix task to run.
- `:args`: A list of arguments to pass to the Mix task's `run/1` function.
- `:interval`: The time interval in millisconds to rerun the task.
@mickmaccallum
mickmaccallum / scheduler.ex
Created November 9, 2016 23:41 — forked from danielberkompas/scheduler.ex
A simple mix task scheduler for Elixir apps
defmodule MyApp.Scheduler do
@moduledoc """
Schedules a Mix task to be run at a given interval in milliseconds.
## Options
- `:task`: The name of the Mix task to run.
- `:args`: A list of arguments to pass to the Mix task's `run/1` function.
- `:interval`: The time interval in millisconds to rerun the task.
@mickmaccallum
mickmaccallum / nginx.conf
Created July 9, 2016 05:52 — forked from plentz/nginx.conf
Best nginx configuration for improved security(and performance). Complete blog post here http://tautt.com/best-nginx-configuration-for-security/
# to generate your dhparam.pem file, run in the terminal
openssl dhparam -out /etc/nginx/ssl/dhparam.pem 2048
#user nobody;
#Defines which Linux system user will own and run the Nginx server
worker_processes 1;
#Referes to single threaded process. Generally set to be equal to the number of CPUs or cores.
#error_log logs/error.log; #error_log logs/error.log notice;
#Specifies the file where server logs.
@mickmaccallum
mickmaccallum / NSObject+RawRepresentableKVO.swift
Created January 13, 2016 15:31
An NSObject extension which allows you to use enums as key paths for common KVO tasks.
extension NSObject {
func addObserver <RawType: RawRepresentable where RawType.RawValue == String>(observer: NSObject, forKeyPath keyPath: RawType, options: NSKeyValueObservingOptions, context: UnsafeMutablePointer<Void>) {
addObserver(observer, forKeyPath: keyPath.rawValue, options: options, context: context)
}
func removeObserver <RawType: RawRepresentable where RawType.RawValue == String>(observer: NSObject, forKeyPath keyPath: RawType) {
removeObserver(self, forKeyPath: keyPath.rawValue)
}
func removeObserver <RawType: RawRepresentable where RawType.RawValue == String>(observer: NSObject, forKeyPath keyPath: RawType, context: UnsafeMutablePointer<Void>) {
@mickmaccallum
mickmaccallum / RomanToArabicAndBack.swift
Last active October 20, 2015 14:38
Swift extensions of Int and String for converting to and from Roman and Arabic digits and checking to see if a given Roman numeral String is valid.
// Extensions of Int and String for converting to and from Roman and Arabic digits and
// checking to see if a given Roman numeral String is valid. Only designed to work in
// the range [0, 3999]. Output given from outside of this range will not be correct.
//
// Created by Michael MacCallum
// CC0 License
public extension Int {
public func toRoman() throws -> String {
guard 0..<4000 ~= self else {
@mickmaccallum
mickmaccallum / fizzBuzzGenerator.swift
Last active October 20, 2015 13:44
Swift 2 - FizzBuzz Generator
struct FizzBuzzGenerator: SequenceType {
private let bounds: Range<Int>
func generate() -> AnyGenerator<String> {
var current = bounds.startIndex
return anyGenerator {
guard current < self.bounds.endIndex else {
return nil
}
@mickmaccallum
mickmaccallum / gist:c2b322e059c9b3379245
Last active February 25, 2017 11:28
Swift recursive flatmap
func recursiveFlatmap<T, U: AnyObject>(list: [U]) -> [T] {
var results = [T]()
results.reserveCapacity(list.count)
for element in list {
if let subList = element as? [U] {
results += recursiveFlatmap(subList)
} else if let element = element as? T {
results.append(element)
}
@mickmaccallum
mickmaccallum / classes.swift
Created May 5, 2015 17:50
Missing NSObject introspection methods. isStrictSubclassOfClass(), isStrictSuperclassOfClass() and isSuperclassOfClass()
extension NSObject {
class func isStrictSubclassOfClass(aClass: AnyClass) -> Bool {
return isSubclassOfClass(aClass) && self !== aClass.self
}
class func isStrictSuperclassOfClass(aClass: AnyClass) -> Bool {
return isSuperclassOfClass(aClass) && self !== aClass.self
}
class func isSuperclassOfClass(aClass: AnyClass) -> Bool {
@mickmaccallum
mickmaccallum / gist:994447e9cf6d396c68d8
Last active August 29, 2015 14:18
Swift factorial
// Simple factorial generator. Doesn't handle numbers that overflow Int or the 0! = 1 case.
// Swift 2
let num = (1...17).reduce(1, combine: *) // 355687428096000
// Swift 1.x
let num = reduce(1...17, 1, *) // 355687428096000