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 / Makefile
Last active October 11, 2019 21:12
C program to convert Roman numerals to integer values
rn: rn.c
test: rn
./rn I IV V VI IX X XI XIV XIX XCIX CI MCMLXVII MD MDC MCD MM
.PHONY: test
@kristopherjohnson
kristopherjohnson / IfElse.swift
Last active April 19, 2019 11:22
Implement "If", "Else", and "ElseIf" in Swift
// Implementation of If, ElseIf, and Else in Swift.
//
// Supports uses such as these:
//
// If (condition) { doAction() }
//
// If (condition) { doAction() }
// .Else { doOtherAction() }
//
// If (condition) { doAction() }
@kristopherjohnson
kristopherjohnson / CommandLineParseResult.swift
Last active June 21, 2018 00:41
Swift 3: Command-line options and arguments parser
import Foundation
extension Array {
/// Returns a new `Array` made by appending a given element to the `Array`.
func appending(_ newElement: Element) -> Array {
var a = Array(self)
a.append(newElement)
return a
}
}
@kristopherjohnson
kristopherjohnson / Array+appending.swift
Last active March 10, 2022 08:10
Swift 3: Create new array by appending an element to existing array
extension Array {
/// Returns a new `Array` made by appending a given element to the `Array`.
func appending(_ newElement: Element) -> Array {
var a = Array(self)
a.append(newElement)
return a
}
}
@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