Skip to content

Instantly share code, notes, and snippets.

@algal
algal / ContinuousGradient.swift
Last active November 2, 2015 22:23
Define gradient as usual with finite stops, then extract the color from any position
/// Exposes all colors in a gradient, which is defined as usual by a finite number of color stops
/// - version: known good on Xcode 7.1 playgrounds and iOS 9.1
class ColorGradient
{
// array of specified colors for specified locations along the gradient
let colors:[UIColor]
// array of specified locations (in 0...1) for the specified colors in the gradient
let locations:[CGFloat]
let gradientImage:UIImage
@algal
algal / SelfCapturePuzzle.swift
Created November 10, 2015 00:09
Gist trying to understand if Swift prevents capturing self in an initializer of a value type
// known confusing on Swift 2.1, Xcode 7.1
import Foundation
public class InnerMutator {
var f:(() -> Void)?
}
public struct MutableValue
{
@algal
algal / CenteredTagCloudView.swift
Last active November 26, 2015 14:39
CenteredTagCloudView playground code
//
// MultilineLabelCloudView.swift
//
//
import UIKit
/**
Presents a wrapping grid of tokens presented as pills.
@algal
algal / NSDate+SystemRestart.swift
Created November 28, 2015 04:09
NSDate extensions for time measured from system restart
/// seconds from the reference date to the system's last restart
private let secondsFromReferenceDateToRestart:NSTimeInterval = {
/* we pretend these next two statements take place atomically, although in fact they do not, and the fact that clock progresses between the two statements will introduce miniscule error into our calculation */
let secondsFromSystemRestartToInit = NSProcessInfo().systemUptime
let secondsFromReferenceDateToInit = NSDate.timeIntervalSinceReferenceDate()
let retVal = secondsFromReferenceDateToInit - secondsFromSystemRestartToInit
return retVal
}()
extension NSDate {
@algal
algal / IntervalTypeErasingWrapperPlayground.swift
Created December 18, 2015 22:23
Example of type-erasing to work with the PAT (Protocol with Associated Type) IntervalType
// Paste me into a playground and then do Editor / Show Rendedered Markup
import UIKit
/*:
Swift defines `IntervalType`, a protocol with associated type (PAT) with one associated type `Bound`
This PAT is adopted by two interval types, `ClosedInterval` and `HalfOpenInterval`
@algal
algal / ViewWrappingTableViewCell.swift
Last active February 2, 2016 12:37
Abstract UITableViewCell class for quickly defining UITableViewCell subclasses that merely wrap a custom UIView subclass
// ViewWrappingTableViewCell.swift
// Created by Alexis Gallagher on 6/4/15.
import UIKit
/**
Abstract superclass for quickly defining a `UITableViewCell` subclass that wraps a
specific `UIView` subclass. The resulting `UITableViewCell` will hug the wrapped view
with layout constraints, so that both the cell and the wrapped view can influence
@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 {

Keybase proof

I hereby claim:

  • I am algal on github.
  • I am alexisgallagher (https://keybase.io/alexisgallagher) on keybase.
  • I have a public key whose fingerprint is 3E64 F42E 2405 40EA 98E1 8AFB 60F4 2CDB DF42 F063

To claim this, I am signing this object:

@algal
algal / json-to-csv.clj
Created February 17, 2016 18:17
Quick JSON to CSV converter in Clojure, for use with boot-clj
#!/usr/bin/env boot
;; needs boot-clj
(set-env! :dependencies '[[org.clojure/data.csv "0.1.3"]
[org.clojure/data.json "0.2.6"]])
(require '[clojure.data.csv :as csv])
(require '[clojure.data.json :as json])
(defn run [in out]
(let [data (json/read in)
@algal
algal / pull.txt
Created March 24, 2016 05:39
the rise of the entity-attribute data model
http://docs.datomic.com/pull.html
https://github.com/tonsky/datascript
https://github.com/brandonbloom/jsgraphthinggie
https://github.com/juxt/pull/
https://github.com/alandipert/intension