Skip to content

Instantly share code, notes, and snippets.

View harlanhaskins's full-sized avatar

Harlan Haskins harlanhaskins

View GitHub Profile
harlanhaskins / projecting.swift
Created April 3, 2021 20:52
Project a UnitPoint to a CGRect
extension CGRect {
func projecting(_ point: UnitPoint) -> CGPoint {
return CGPoint(x: minX + (width * point.x),
y: minY + (height * point.y))
harlanhaskins / TMNT Symbols
Created July 4, 2020 05:59
All the iOS 14/macOS 11/tvOS 14/watchOS 7 symbols that are singable to the TMNT theme song
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -o %t/Test~partial.swiftmodule -module-name Test -primary-file %s
// RUN: %target-swift-frontend -merge-modules -emit-module -o %t/Test.swiftmodule %t/Test~partial.swiftmodule
// RUN: %target-swift-ide-test -print-module -module-to-print=Test -source-filename=x -I %t | %FileCheck %s
// RUN: %target-swift-frontend -emit-interface-path %t.swiftinterface -enable-resilience -emit-module -o /dev/null %s
// RUN: %FileCheck %s < %t.swiftinterface
// CHECK: func hasClosureDefaultArg(_ x: () -> Void = {
harlanhaskins / BitArray.swift
Created July 20, 2018 22:04
Swift BitArray that behaves like an array of bools.
import Foundation
extension BinaryInteger {
/// Gets the bit at the specified bit index in the receiver, reading from
/// least to most-significant bit.
/// For example,
/// ```
/// 0b0010.bit(at: 0) == false
/// 0b0010.bit(at: 1) == true
harlanhaskins / BitstreamWriter.swift
Created June 19, 2018 16:25
LLVM Bitstream Writer in Swift
import Foundation
typealias UnsignedIntegralType = UnsignedInteger & ExpressibleByIntegerLiteral
struct BitstreamRecord {
private(set) var values = [UInt32]()
mutating func append<IntType: UnsignedIntegralType>(_ int: IntType) {
import Foundation
import SwiftSyntax
# Syntax Rewriting
SwiftSyntax provides a class called `SyntaxRewriter`, which will walk a Syntax tree and
perform transformations over the nodes.
By default, these transformations don't do anything. It's the subclass's job to
harlanhaskins / StringScanner.swift
Last active August 4, 2020 23:45
Swift String Scanner
import Foundation
/// `StringScanner` is a fast scanner for Strings and String-like objects.
/// It's used to extract structured bits from unstructured strings, while
/// avoiding making extra copies of string bits until absolutely necessary.
/// You can build Scanners over Substrings, allowing you to scan
/// parts of strings and use smaller, more specialized scanners to extract bits
/// of that String without needing to reuse another scanner.
public struct StringScanner<Input: StringProtocol> {
let input: Input
/Applications/ -module-name NIOOpenSSL -incremental -emit-dependencies -emit-module -emit-module-path /Users/harlan/Code/Swift/WWDC-Photohunt/.build/x86_64-apple-macosx10.10/debug/NIOOpenSSL.swiftmodule -output-file-map /Users/harlan/Code/Swift/WWDC-Photohunt/.build/x86_64-apple-macosx10.10/debug/ -parse-as-library -num-threads 8 -c /Users/harlan/Code/Swift/WWDC-Photohunt/.build/checkouts/swift-nio-ssl.git-1370587408992578247/Sources/NIOOpenSSL/IdentityVerification.swift /Users/harlan/Code/Swift/WWDC-Photohunt/.build/checkouts/swift-nio-ssl.git-1370587408992578247/Sources/NIOOpenSSL/OpenSSLClientHandler.swift /Users/harlan/Code/Swift/WWDC-Photohunt/.build/checkouts/swift-nio-ssl.git-1370587408992578247/Sources/NIOOpenSSL/OpenSSLHandler.swift /Users/harlan/Code/Swift/WWDC-Photohunt/.build/checkouts/swift-nio-ssl.git-1370587408992578247/Sources/NIOOpenSSL/OpenSSLServerHandler.swift /Users/harlan/C
harlanhaskins / scryfall-example.swift
Created March 5, 2018 22:09
import Foundation
enum Result<Value> {
case success(Value)
case failure(Error)
func promote() throws -> Value {
switch self {
case .success(let value):
return value
harlanhaskins / ArduinoUtils.h
Last active January 27, 2018 04:16
A C++API for Arduino
#include <Arduino.h>
/// DigitalPin is a base class for digital I/O pins. On its own, it just
/// declares the data layout -- it's up to subclasses to implement features
/// that are specific to input or output.
class DigitalPin {