Skip to content

Instantly share code, notes, and snippets.

@algal
algal / NSURLSession+DataTaskResult.swift
Created April 6, 2016 21:46
Type-wrapper for dataTaskWithRequest
// known-good: Xcode 7.3, Swift 2.2
import Foundation
public enum DataTaskResult {
case Success(data:NSData,response:NSURLResponse?)
case Error(error:NSError,response:NSURLResponse?)
}
extension NSURLSession {
@algal
algal / PKCS12.swift
Last active February 24, 2025 20:47
Reading PKCS12 with Swift in Foundation
// xcode 7.3
import Foundation
/**
Struct representing values returned by `SecPKCS12Import` from the Security framework.
This is what Cocoa and CocoaTouch can tell you about a PKCS12 file.
*/
@algal
algal / wallsleep.sh
Last active September 13, 2016 21:56
wallsleep -- like sleep, but uses wall time not process time
#!/bin/sh
# sleeps for $1 seconds based on wall time, not CPU time
set -e
# known-good with:
# - GNU date (Ubuntu 14.04.3 LTS)
# - Darwin date (OS X 10.11.4)
@algal
algal / edn-to-json.clj
Created March 29, 2016 18:25
Quick edn-to-json, with basic handling of date and uuid types
#!/usr/bin/env boot
;; needs boot-clj
;; converts edn to json,
;; - taking #inst to strings of ISO8601 timestamps
;; - taking #uuid to strings
(set-env! :dependencies '[[org.clojure/data.json "0.2.6"]])
(require '[clojure.data.json :as json])
@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
@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)

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 / 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 / FileOutputStream.swift
Created November 30, 2015 07:25
Buffered writing to a file with Swift OutputStreamType and GCD i/o channel
/// An `OutputStreamType` which uses GCD to write to a file
class FileOutputStream : OutputStreamType
{
private let filepath:NSURL
private let channel:dispatch_io_t!
/**
Initializes output stream to write to `filename` in the user's Documents directory.
*/
@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 {