Skip to content

Instantly share code, notes, and snippets.

/*:
Paste this into Swift Playgrounds on an iPad to see how
modifying a label's frame affects its intrinsicContentSize.
As near as I can tell, frame currently does not affect
intrinsicContentSize.
At least I am sure it did not used to affect it.

Setting up a Jupyter server

These notes describe how to install and configure a secured Jupyter server directly onto Ubuntu 16.04.

Warning: this might be stupid. In 2017, it might be wiser just to grab a docker image, since some of these look extremely well-packaged. In my view, the advantage of docker is that someone else does the packaging, while docker itself provides cross-platform portability, rudimentary process supervision, and perhaps a simpler model than your OS's package manager. It is easy to know what is installed, how to uninstall it, and where its boundaries are. The disadvantage of docker is that you need to understand docker possibly in addition to understanding some Unix fundamentals.

Setup LetsEncrypt certs if needed

Install LetsEncrypt

@algal
algal / emacs-open-new-window.sh
Created October 24, 2017 04:41
emacs-open-new-window.sh
#!/bin/sh
emacsclient -n -e "(find-file-other-frame \"$1\")"
# For this script to work, you must already have started
# an emacs instance and started its server. You can do
# that by adding some elisp like the following into
# your .emacs file:
#
# (require 'server)
# (server-mode 1)
@algal
algal / CGColorGenerator.swift
Created August 8, 2017 16:05
Generate CGColors to match UIColors
//: Known-Good: Version 9.0 beta 5 (9M202q)
//: Playground - noun: a place where people can play
import UIKit
//: This playground is for generating code to define CGColors to match UIColors
@algal
algal / CharSet+ShellSafe.swift
Last active August 8, 2017 00:15
Shell-safe characters
// known-good: Xcode 8.3.3
/*
This shell script outputs which characters are special and need escaping in bash or sh:
#!/bin/bash
special=$'`!@#$%^&*()-_+={}|[]\\;\':",.<>?/ '
for ((i=0; i < ${#special}; i++)); do
char="${special:i:1}"
printf -v q_char '%q' "$char"
@algal
algal / CGRect+Codable.swift
Created July 10, 2017 21:58
CGRect Codable implementation
// known-good: Swift 4, in Xcode Version 9.0 beta 2 (9M137d)
// will be unnecessary once the language can auto-synthesize for this case, or
// when Apple's ships a Codable implementation for CoreGraphics struct types.
extension CGSize : Codable {
enum CodingKeys: String, CodingKey {
case width
case height
}
@algal
algal / sendSynchronousRequest.swift
Last active April 25, 2017 03:20
Synchronous HTTP request in
// known-good: 8.3.2, Swift 3
/**
Sometimes you just want to launch a synchronous HTTP request for some JSON, damnit.
Other approaches here: https://github.com/algal/SynchronousDownloadPlayground
*/
extension URLSession {
func sendSynchronousRequest(_ request:URLRequest,
@algal
algal / HTTPBasicAuthenticationSessionTaskDelegate.swift
Last active October 6, 2021 07:33
HTTP Basic Authentication in iOS
/*
Everyone on Stack Overflow does HTTP Basic Authentication on iOS by manually
building the HTTP headers.
This amounts to re-implementing HTTP.
Why? The Cocoa Touch URL Loading System aleady knows HTTP, and you can
configure your URLSession to supply HTTP Basic Authentication credentials
like so.
import Foundation
/// Represents side effects needed to sync dst with src
enum Effect {
/// in dst, remove specified item in dst
case remove(dstIndex:Int)
/// in dst, add the specified item from src
case add(srcIndex:Int)
}
@algal
algal / SelfAssigningReferenceTypes.swift
Last active October 26, 2017 09:01
Brief exploration of Swift classes that assign to self via protocol extensions
/*
## background
When you write a function that mutates the var properties of a
mutable struct, this _creates a new instance_.[1] As a result,
a variable pointing to a value of this type before the mutation
will still be pointing to the old value after the mutation, as long
as you did not use that variable to do the mutation.