Skip to content

Instantly share code, notes, and snippets.

@proxpero
proxpero / SelectionSort.swift
Last active January 27, 2016 18:35
Selection sort in Swift implemented as an extension on `MutableCollectionType`
// Swift 2.1
extension MutableCollectionType where Self.Generator.Element : Comparable {
/// Return a new sorted collection of the elements in `source`
/// using [Selection Sort](https://en.wikipedia.org/wiki/Selection_sort).
///
/// Complexity O(n^2).
@warn_unused_result(mutable_variant="selectionSortInPlace")
public func selectionSort() -> Self {
@proxpero
proxpero / InsertionSort.swift
Last active January 27, 2016 18:38
Insertion Sort in Swift implemented as an extension on `MutableCollectionType`
// Swift 2.1
extension MutableCollectionType where Self.Generator.Element : Comparable {
/// Return a new sorted collection of the elements in `source`
/// using [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort).
///
/// Complexity O(n^2).
@warn_unused_result(mutable_variant="insertionSortInPlace")
public func insertionSort() -> Self {
@proxpero
proxpero / ShellSort.swift
Created January 27, 2016 21:19
Sort a collection using Shell's method. Implemented in Swift as an extension on `MutableCollectionType`.
// Swift 2.1
extension MutableCollectionType where Self.Generator.Element : Comparable, Self.Index == Int {
/// Return a new sorted collection of the elements in `source`
/// using [Shell Sort](https://en.wikipedia.org/wiki/Shellsort).
///
/// Complexity O(n^(3/2)).
@warn_unused_result(mutable_variant="shellSortInPlace")
@proxpero
proxpero / MergeSort.swift
Last active February 8, 2016 17:29
A simple implementation of Merge Sort in Swift as an extension on Array
// Swift 2.1
extension Array where Element : Comparable {
private func merge(var left: Array<Element>, var _ right: Array<Element>) -> Array<Element> {
var result: Array<Element> = []
while !left.isEmpty && !right.isEmpty {
@proxpero
proxpero / md5.swift
Last active February 16, 2017 16:48
Small, zero-dependency extensions on `String` and Collections of `UInt8` that return the md5 hash of self.
// 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

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 / Barcode2.swift
Created July 17, 2017 19:08
Encode and decode an enum with associated types.
// 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 / aoc2018d2.swift
Created December 2, 2018 06:59
Advent of Code 2018, day 2
// 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 December 2, 2018 06:59
Advent of Code 2018, day 1
// 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 / Barcode.swift
Last active May 12, 2019 21:26
Encoding and Decoding Enums with Associated Values
// 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()
}