Skip to content

Instantly share code, notes, and snippets.

@algal
algal / nginx-cors.conf
Created April 29, 2013 10:52
nginx configuration for CORS (Cross-Origin Resource Sharing), with an origin whitelist, and HTTP Basic Access authentication allowed
#
# A CORS (Cross-Origin Resouce Sharing) config for nginx
#
# == Purpose
#
# This nginx configuration enables CORS requests in the following way:
# - enables CORS just for origins on a whitelist specified by a regular expression
# - CORS preflight request (OPTIONS) are responded immediately
# - Access-Control-Allow-Credentials=true for GET and POST requests
@algal
algal / UIImageView+ALGDataTask.h
Created May 22, 2014 17:08
Simple asynchronous UIImageView image-loading using only built-in framework components
//
// UIImageView+ALGDataTask.h
// VideoClient
//
// Created by Alexis Gallagher on 2014-01-18.
// Copyright (c) 2014 Bloom Filter. All rights reserved.
//
#import <UIKit/UIKit.h>
@algal
algal / SwiftArrayProblem.swift
Last active April 12, 2016 22:08
Illustrates what is problematic about Swift's let-defined "constant" arrays actually being mutable
/*
This gist illustrates how Swift's "immutable" arrays are actually
mutable, and why that is sad. Load it into a playground to experiment.
"let"-defining an array should produce a truly immutable value. Instead, it
now produces a constant reference to a mutable, fixed-size array. This is like
C. So it is no better than C. And it's worse than Objective-C, where you
could produce a reliably constant array with code like:
//
// Set
//
struct MySet<KeyType : Hashable> : Sequence
{
var dictionaryOfItems = Dictionary<KeyType,Bool>()
init() {}
init(array:Array<KeyType>) {
class Bag<T: Hashable>: Sequence, Printable {
var _storage = Dictionary<T, Int>()
typealias GeneratorType = Dictionary<T,Int>.GeneratorType
func addItem(item: T) {
if let count = _storage[item] {
_storage[item] = count + 1
} else {
_storage[item] = 1
}
@algal
algal / tsv_to_json.swift
Last active April 22, 2023 10:01
Quick CSV to JSON in Swift
import Foundation
// Swift 2.0
// poor man's parsers for (TSV) tab-separated value files
// for something more full-featured, the best avenue is CHCSVParser
/**
Reads a multiline, tab-separated String and returns an Array<NSictionary>, taking column names from the first line or an explicit parameter
*/
@algal
algal / groupBy.swift
Last active February 3, 2016 13:47
groupBy
func groupBy<T>(equivalent:(a:T,b:T)->Bool, items:[T]) -> [[T]] {
var lastItem:T? = nil
var groups:[[T]] = []
var currentGroup:[T] = []
for item in items {
if lastItem == nil {
// first item
currentGroup.append(item)
}
else {
@algal
algal / :(.swift
Last active September 4, 2015 12:18 — forked from chriseidhof/:(
import Foundation
import ImageIO
//
// before
//
func datesFromImagesInDir(dir: String) -> [NSDate] {
let fm = NSFileManager.defaultManager()
@algal
algal / List.swift
Last active August 29, 2015 14:10
Ye old linked list
class Box<T> {
let unbox:T
init(unbox:T) { self.unbox = unbox }
}
/*
Boxing is necessary because Swift does not allow recursive enum types.
However, AFAICT, is it also desirable here because it enables structural
@algal
algal / StructEnumVsEnumAssocValues.swift
Last active August 29, 2015 14:15
Enum in a Struct vs Enum with Associated Values
/*
The examples below show two ways to model a state machine with two states,
On and Off, with an additional constant Int property.
On -> Off and Off -> On transitions must be inititated by calling flip()
*/