Skip to content

Instantly share code, notes, and snippets.

View twostraws's full-sized avatar

Paul Hudson twostraws

View GitHub Profile
// A URLSession extension that fetches data from a URL and decodes to some Decodable type.
// Usage: let user = try await URLSession.shared.decode(UserData.self, from: someURL)
// Note: this requires Swift 5.5.
extension URLSession {
func decode<T: Decodable>(
_ type: T.Type = T.self,
from url: URL,
keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy = .useDefaultKeys,
dataDecodingStrategy: JSONDecoder.DataDecodingStrategy = .deferredToData,
dateDecodingStrategy: JSONDecoder.DateDecodingStrategy = .deferredToDate
@twostraws
twostraws / gasp.swift
Created March 13, 2020 21:39
A quick clone of the watchOS Breathe UI, for Swift Over Coffee
//
// Gasp – a quick clone of the watchOS Breathe UI, for Swift Over Coffee
// https://github.com/twostraws/SwiftOverCoffee
//
import SwiftUI
struct ContentView: View {
@State private var flowerOut = false
var body: some View {
@twostraws
twostraws / arrays.swift
Created January 28, 2020 23:44
A microbenchmark for a potential Swift standard library addition
//
// FOR FOLKS STUMBLING ACROSS THIS LATER
// This is an example of code that allows us to create arrays by calling a function a fixed number of times.
// In my example below I'm just sending in 5 because I don't want to cloud the benchmark with generating
// a random number or similar, but with this change you can replace the 5 with `Int.random(in: 1...10)` to
// get an array of random numbers.
//
// HERE'S THE STDLIB ADDITION TO MAKE THIS WORK:
// @inlinable
// @_semantics("array.init")
@twostraws
twostraws / sundell.swift
Created October 17, 2017 15:23
Sundell turns 30
import Foundation
let sundelL = { (t: Int) in return -(~t) }
let sundeLl = { return sundelL(sundelL($0)) }
let sundEll = { return sundeLl(sundeLl($0)) }
let sunDell = { return sundEll(sundEll($0)) }
let suNdell = { return sunDell(sunDell($0)) }
let sUndell = { return suNdell(suNdell($0)) }
let Sundell = { return sUndell(sUndell($0)) }
let sundell = { (t: Int) -> Int in print(UnicodeScalar(t)!, terminator: ""); return 0 }
@twostraws
twostraws / type-layout-fuzzer.swift
Created September 21, 2017 19:36
A Swift 4 port of Joe Groff's type-layout-fuzzer.py
//
// type-layout-fuzzer.swift
// Paul Hudson / @twostraws / www.hackingwithswift.com
//
// This is a Swift 4 port of Joe Groff's type-layout-fuzzer.py.
// I haven't attempted to Swiftify the code – it's pretty
// much a line-by-line port.
//
// License: public domain / CC0 / seriously, do whatever
// you want with it.
@twostraws
twostraws / TopSekritPokemonGoSourceCode.swift
Created July 17, 2016 21:15
Totally Top Sekrit Pokémon Go Source Code
//
// PokemonGoViewController.swift
// Totally Top Sekrit Pokémon Go Source Code
//
// Created by Niantic on 07/01/2016.
// Copyright © 2016 Niantic rights reserved.
//
import UIKit
@twostraws
twostraws / gist:3d673d4eba36de173f6f
Last active August 29, 2015 14:23
Love Wins in Swift
//
// loveWins(): a simple function that accepts a UIImage and
// returns the same image blended with the rainbow flag
// of the LGBT pride movement.
//
// This is released for pedagogical reasons (I've tried to make
// the code as easy to follow as possible!) but you're welcome
// to use it for any purpose – consider the code yours.
//
// If you're using Xcode 7 / Swift 2, you need to make a tiny
//
// SimpleScrollingStack.swift
// A super-simple demo of a scrolling UIStackView in iOS 9
//
// Created by Paul Hudson on 10/06/2015.
// Learn Swift at www.hackingwithswift.com
// @twostraws
//
import UIKit