Skip to content

Instantly share code, notes, and snippets.

View lukeredpath's full-sized avatar
🏠
Working from home

Luke Redpath lukeredpath

🏠
Working from home
View GitHub Profile
extension Data {
// Returns a base64 encoded string, replacing reserved characters
// as per https://tools.ietf.org/html/rfc7636#section-4.2
func pkce_base64EncodedString() -> String {
base64EncodedString()
.replacingOccurrences(of: "+", with: "-")
.replacingOccurrences(of: "/", with: "_")
.replacingOccurrences(of: "=", with: "")
.trimmingCharacters(in: .whitespaces)
}
private func hexString(_ iterator: Array<UInt8>.Iterator) -> String {
return iterator.map { String(format: "%02x", $0) }.joined()
}
let exampleVerifier = "kM83K571n5KFW9u29Xu2qSqgoUwep4I2jZw8FGZg4Yr"
let verifierData = exampleVerifier.data(using: .utf8)!
// CommonCrypto implementation
var buffer = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
_ = verifierData.withUnsafeBytes { CC_SHA256($0.baseAddress, CC_LONG(verifierData.count), &buffer) }
@lukeredpath
lukeredpath / RawRepresentable+Parsing.swift
Created January 3, 2021 01:45
Swift enum parsing using the swift-parsing library
extension RawRepresentable where Self.RawValue == String {
/// Parses out the first matching raw value from a list of possible values, or returns nil.
///
/// Example:
///
/// enum SomeValues: String, CaseIterable {
/// case one
/// case two
/// }
///
struct TaggedOutput<Output> {
let token: AnyHashable
let output: Output
}
struct Effect<Output>: Publisher {
typealias Failure = Never
let base: AnyPublisher<TaggedOutput<Output>, Failure>
let token: AnyHashable
2020-08-04 19:56:05.471109+0100 AudioSimulatorBug[5723:385998] libMobileGestalt MobileGestaltCache.c:38: No persisted cache on this platform.
2020-08-04 19:56:05.494965+0100 AudioSimulatorBug[5723:385162] [SceneConfiguration] Encoded configuration for UIWindowSceneSessionRoleApplication contained a UISceneDelegate class named "(null)", but no class with that name could be found.
2020-08-04 19:56:10.523921+0100 AudioSimulatorBug[5723:385162] HALCADClient::ConnectToServer: failed to find the AHS sub-server port, Error: 0x10004003
2020-08-04 19:56:15.525034+0100 AudioSimulatorBug[5723:385162] HALCADClient::ConnectToServer: failed to find the AHS sub-server port, Error: 0x10004003
2020-08-04 19:56:15.525272+0100 AudioSimulatorBug[5723:385162] HALDefaultDevice::Initialize: couldn't add the default input device listener, Error: 268451843 (\^P)
2020-08-04 19:56:20.525255+0100 AudioSimulatorBug[5723:385162] HALCADClient::ConnectToServer: failed to find the AHS sub-server port, Error: 0x10004003
2020-08-04 19:56:2
2020-07-23 17:54:20.302611+0100 Poker Clock[15031:1458990] libMobileGestalt MobileGestaltCache.c:38: No persisted cache on this platform.
2020-07-23 17:54:25.428029+0100 Poker Clock[15031:1454775] HALCADClient::ConnectToServer: failed to find the AHS sub-server port, Error: 0x10004003
2020-07-23 17:54:30.429086+0100 Poker Clock[15031:1454775] HALCADClient::ConnectToServer: failed to find the AHS sub-server port, Error: 0x10004003
2020-07-23 17:54:30.429312+0100 Poker Clock[15031:1454775] HALDefaultDevice::Initialize: couldn't add the default input device listener, Error: 268451843 (\^P)
2020-07-23 17:54:35.430178+0100 Poker Clock[15031:1454775] HALCADClient::ConnectToServer: failed to find the AHS sub-server port, Error: 0x10004003
2020-07-23 17:54:35.430464+0100 Poker Clock[15031:1454775] HALDefaultDevice::Initialize: couldn't add the default output device listener, Error: 268451843 (\^P)
2020-07-23 17:54:40.431523+0100 Poker Clock[15031:1454775] HALCADClient::ConnectToServer: failed to find the AHS su
@lukeredpath
lukeredpath / Weird.swift
Last active July 1, 2020 14:34
Weird Swift 5.2 error - works in Swift 5.3
// full definition omitted:
public struct ValidatorOf<Value, Error> {
public let validate: (Value) -> Validated<Value, Error>
}
extension ValidatorOf {
static func its<T>(_ transform: @escaping (Value) -> T, _ validator: ValidatorOf<T, Error>) -> Self {
validator.pullback(transform)
}
}
@lukeredpath
lukeredpath / Validations.swift
Last active October 16, 2021 17:57
A little experiment with functional Rails-style validators built on top of pointfreeco/Validated
import UIKit
import Validated
extension Validated {
func mapErrors<LocalError>(_ transform: (Error) -> LocalError) -> Validated<Value, LocalError> {
switch self {
case let .valid(value):
return .valid(value)
case let .invalid(errors):
return .invalid(errors.map(transform))
@lukeredpath
lukeredpath / keybase.md
Created September 12, 2018 10:57
keybase.md

Keybase proof

I hereby claim:

  • I am lukeredpath on github.
  • I am lukeredpath (https://keybase.io/lukeredpath) on keybase.
  • I have a public key ASAUz6CDWah_lWnyO27v-PoWTwHcOye7npHtcYVaBjlwrwo

To claim this, I am signing this object:

@lukeredpath
lukeredpath / FizzBuzz.rb
Last active June 13, 2018 11:30
FizzBuzz with mocks
# Given a range as input, reports fizz, buzz or fizzbuz.
#
# FizzBuzz logic is separated from presentation logic which is a concern for the reporter.
#
class FizzBuzz
def generate(range, reporter)
range.each do |n|
if n % 3 == 0 && n % 5 == 0
reporter.report(:fizzbuzz)
elsif n % 3 == 0