Skip to content

Instantly share code, notes, and snippets.

View Ackerman
public func ack(_ m: Int, _ n: Int) -> Int {
if m == 0 { return n + 1 }
if n == 0 { return ack(m - 1, 1) }
return ack(m - 1, ack(m, n - 1))
}
for i in (0...5) {
for j in (0...5) {
print("ack(\(i), \(j)) is \(ack(i, j))")
}
@proxpero
proxpero / aoc2018d2.swift
Created Dec 2, 2018
Advent of Code 2018, day 2
View aoc2018d2.swift
// Part 1
extension String {
var characterCounts: [Character: Int] {
return self.reduce(into: [Character: Int]()) { $0[$1, default: 0] += 1 }
}
}
extension Bool {
var intValue: Int {
@proxpero
proxpero / aoc2018d1.swift
Last active Dec 2, 2018
Advent of Code 2018, day 1
View aoc2018d1.swift
// prepare the puzzle input
let input = loadFile(named: "day1") // convenience function in resources
.split(separator: "\n")
.compactMap(Int.init) // extension on Int to init? with String.Subsequence
// part 1
let part1 = input.reduce(0, +)
// part 2
@proxpero
proxpero / Iso3166_1a2.swift
Last active Oct 7, 2021
A Swift 4.0 enum representing ISO 3166-1 alpha-2 (two letter country codes) Includes properties for country name and unicode flag.
View Iso3166_1a2.swift
// taken 2018-03-19 from wikipedia. https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
enum Iso3166_1a2: String {
case af = "AF"
case ax = "AX"
case al = "AL"
case dz = "DZ"
case `as` = "AS"
case ad = "AD"
case ao = "AO"
@proxpero
proxpero / Iso369_1.swift
Created Mar 19, 2018
A Swift 4.0 enum representing ISO 639-1 codes (used to classify languages)
View Iso369_1.swift
// taken 2018-03-19 from wikipedia. https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
public enum Iso639_1: String {
case ab = "AB"
case aa = "AA"
case af = "AF"
case ak = "AK"
case sq = "SQ"
case am = "AM"
case ar = "AR"
@proxpero
proxpero / zip3.swift
Created Dec 28, 2017
An implementation of zip that creates a sequence of three-item tuples from the combination of three sequences. Much like the standard library's `zip` function for a pair of sequences.
View zip3.swift
/// Creates a sequence of tuple-3s built out of three underlying sequences.
/// Based on Zip2Seqence from the Swift Standard Library.
/// https://github.com/apple/swift/blob/9361a6b66f6f8351e89c090f604d7e1f42e2a045/stdlib/public/core/Zip.swift
///
/// - Parameters:
/// - sequence1: The first sequence or collection to zip.
/// - sequence2: The second sequence or collection to zip.
/// - sequence3: The third sequence or collection to zip.
/// - Returns: A sequence of tuple triples, where the elements of each triplet are
/// corresponding elements of `sequence1`, `sequence2`, and `sequence3`.
@proxpero
proxpero / Barcode2.swift
Created Jul 17, 2017
Encode and decode an enum with associated types.
View Barcode2.swift
// Accompanying blog post: http://127.0.0.1:4000/2017/07/11/encoding-and-decoding-custom-enums-with-associated-values-in-swift-4
// Also: https://gist.github.com/proxpero/189a723fb96bb88fac5bf9e11d6cf9e2
enum Barcode {
case upc(Int, Int, Int, Int)
case qrCode(String)
}
extension Barcode: Codable {
private enum CodingKeys: String, CodingKey {
@proxpero
proxpero / Barcode.swift
Last active May 12, 2019
Encoding and Decoding Enums with Associated Values
View Barcode.swift
// See http://proxpero.com/2017/07/11/encoding-and-decoding-custom-enums-with-associated-values-in-swift-4/
enum Barcode {
case upc(Int, Int, Int, Int)
case qrCode(String)
}
extension Barcode: Codable {
init(from decoder: Decoder) throws {
self = try Barcode.Coding.init(from: decoder).barcode()
}
View keybase.md

Keybase proof

I hereby claim:

  • I am proxpero on github.
  • I am proxpero (https://keybase.io/proxpero) on keybase.
  • I have a public key whose fingerprint is ABAF 07DE 0D7E EA58 7C60 1131 CB36 31B5 E8CA 11C7

To claim this, I am signing this object:

@proxpero
proxpero / md5.swift
Last active Feb 16, 2017
Small, zero-dependency extensions on `String` and Collections of `UInt8` that return the md5 hash of self.
View md5.swift
// As described in: http://proxpero.com/2017/02/14/an-implementation-of-md5/
extension Collection where Iterator.Element == UInt8 {
/// Returns the md5 hash of self.
public var md5: String {
return self.md5Digest.lazy.reduce("") {
var s = String($1, radix: 16)
if s.characters.count == 1 {
s = "0" + s