Skip to content

Instantly share code, notes, and snippets.

View kristopherjohnson's full-sized avatar
💭
Huh?

Kristopher Johnson kristopherjohnson

💭
Huh?
View GitHub Profile
@kristopherjohnson
kristopherjohnson / String+substringFromNumericIndex.swift
Last active February 18, 2017 13:18
Swift 3: Get a substring starting from a given numeric index to the end of the string.
extension String {
/// Returns substring starting from given numeric index to the end of the string.
func substring(fromNumericIndex numericIndex: Int) -> String {
return self.substring(from: self.index(self.startIndex, offsetBy: numericIndex))
}
}
@kristopherjohnson
kristopherjohnson / FibonacciSequence.swift
Last active February 18, 2017 13:19
Fibonacci sequence in Swift 3
/// Returns a sequence of Fibonacci numbers.
///
/// The sequence is [1, 1, 2, 3, 5, 8, 13, ...].
///
/// The sequence is not infinite; it terminates when
/// the next value would be greater than
/// `Int.max`.
///
/// - returns: A sequence of Fibonacci numbers.
public func fibonacciSequence() -> AnySequence<Int> {
@kristopherjohnson
kristopherjohnson / FileHandleIterators.swift
Last active May 23, 2022 00:32
Iterators for reading bytes or lines from FileHandle objects as sequences
import Foundation
extension FileHandle {
/// Return an iterator over the bytes in the file.
///
/// - returns: An iterator for UInt8 elements.
public func bytes() -> FileHandleByteIterator {
return FileHandleByteIterator(fileHandle: self)
}
@kristopherjohnson
kristopherjohnson / CGPath_forEach.swift
Created December 28, 2016 14:29
A Swift-friendly wrapper for CGPath.apply()
import CoreGraphics
extension CGPath {
/// Call the given closure on each element of the path.
func forEach(_ body: @escaping (CGPathElement) -> Void) {
var info = body
self.apply(info: &info) { (infoPtr, elementPtr) in
let opaquePtr = OpaquePointer(infoPtr!)
let body = UnsafeMutablePointer<(CGPathElement) -> Void>(opaquePtr).pointee
body(elementPtr.pointee)
@kristopherjohnson
kristopherjohnson / distanceAboveLineSegment.swift
Created December 28, 2016 03:50
Calculate Y-axis distance between a point and a line segment.
import CoreGraphics
extension CGPoint {
/// Calculate Y-axis distance between a point and a line segment.
///
/// - parameter endpointA: One endpoint of the line segment.
/// - parameter endpointB: The other endpoint of the line segment.
///
/// - returns: Distance, or `nil` if this point's x-coordinate is not between those of the endpoints.
@kristopherjohnson
kristopherjohnson / halloween2016tcm.txt
Created October 25, 2016 23:04
List of "good" movies on TCM for the last week of October 2016
----------
2016-10-25
----------
06:15 AM - F.B.I. Story, The (1959)
Description: A dedicated FBI agent thinks back on the agency's battles against the Klan, organized crime and Communist spies.
Genres: Drama, Crime, Adaptation
Directed by: Mervyn LeRoy, David Silver, Gil Kissel, Jack Boland
Written by: Richard L. Breen, John Twist
@kristopherjohnson
kristopherjohnson / tcm_whats_on_now.js
Last active October 21, 2016 01:05
Get what's currently showing on Turner Classic Movies
#!/usr/bin/env node
const request = require('request');
// See <http://www.tcm.com/tcmws/v1/docs/welcome.html>
const url_base = 'http://www.tcm.com/tcmws/v1'
function showWhatsOnNow() {
request(`${url_base}/schedule/whatsOnNow/us.json`, (error, response, body) => {
if (error) {
@kristopherjohnson
kristopherjohnson / .vimrc
Created September 11, 2016 12:57
My .vimrc
set nocompatible
" Global variables {{{1
"
let g:kj_foldcolumn=4
let g:kj_quickfix_is_open = 0
" Options {{{1
@kristopherjohnson
kristopherjohnson / make-android-app-icons.sh
Created August 28, 2016 18:23
bash script to create all necessary sizes of app icons for Android applications
#!/bin/bash
#
# Given a source image, create icons in all sizes needed for an Android launcher icon.
#
# First (required) argument is path to source file.
#
# Second (optional) argument is the filename to be used for the output files.
# If not specified, defaults to "ic_launcher.png".
#
# Third (optional) argument is path to the GraphicsMagick gm executable.
@kristopherjohnson
kristopherjohnson / make-ios-app-icons.sh
Last active January 1, 2024 06:32
bash script to create all necessary sizes of app icons for iOS applications
#!/bin/bash
#
# Given a source image, create icons in all sizes needed for an iOS app icon.
# See <https://developer.apple.com/library/ios/qa/qa1686/_index.html> for details.
#
# First (required) argument is path to source file.
#
# Second (optional) argument is the prefix to be used for the output files.
# If not specified, defaults to "app_icon_".
#