Skip to content

Instantly share code, notes, and snippets.

public extension Optional {
func asyncMap<T>(
transform: (Wrapped) async throws -> T
) async rethrows -> T? {
guard case let .some(value) = self else {
return nil
}
return try await transform(value)
}
}
@simonbromberg
simonbromberg / OptionalUnwrap.swift
Created December 24, 2023 17:02
OptionalUnwrap
public struct NilError: Error {
public let file: String
public let line: UInt
public let column: UInt
public let function: String
public init(
file: String = #fileID,
line: UInt = #line,
column: UInt = #column,
@simonbromberg
simonbromberg / BlockConfigurable.swift
Created November 26, 2023 02:32
BlockConfigurable
import UIKit
public protocol BlockConfigurable {}
extension NSObject: BlockConfigurable {}
extension BlockConfigurable {}
public extension UIView {
@discardableResult
@simonbromberg
simonbromberg / MoveRectOrigin.swift
Created August 4, 2022 16:45
Move the origin of a CGRect to fit inside a parent rect by capping its edges — eg make sure a child rect does not get drawn out of bounds
extension CGRect {
mutating func moveOriginToFitIn(_ rect: CGRect) {
origin = .init(
x: min(
max(rect.minX, minX),
rect.maxX - width
),
y: min(
max(rect.minY, minY),
rect.maxY - height
public extension Bool {
func map<T>(
_ closure: () -> T?,
else: (() -> T?)? = nil
) -> T? {
self ? closure() : `else`?()
}
func map<T>(
_ closure: () -> T,
@simonbromberg
simonbromberg / DeleteGitHubActionRuns.swift
Last active September 19, 2021 16:24
Bulk deletion of GitHub Action workflow runs
import PlaygroundSupport
import UIKit
let baseURL = "https://api.github.com/repos/my_repo_owner/my_repo/actions/runs"
let auth = ["Authorization": "Token my_token"]
enum ResponseError: Error {
case invalidURL
case httpError(Error)
case badResponse(HTTPURLResponse)
@simonbromberg
simonbromberg / workflow.yml
Created August 13, 2021 19:03
Github Action Example
on:
workflow_dispatch:
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]
@simonbromberg
simonbromberg / CoinFlip.swift
Created May 27, 2021 15:20
Coin Flip problem
func runTest() -> Bool {
let random = Int.random(in: 0...100)
var coins = Array(repeating: true, count: 20) + Array(repeating: false, count: random)
let mask = coins
coins.shuffle()
var result: [Bool] = []
for (coin, flip) in zip(coins, mask) {
result.append(coin != flip)
}
@simonbromberg
simonbromberg / RestaurantListings.json
Created December 9, 2020 13:35
Restaurant listings I created for an app I was playing around with — a lot of the information is probably outdated, but feel free to use it for your own purposes. This was done in Firebase and the app was focused on kosher restaurants hence the kosher certification information.
This file has been truncated, but you can view the full file.
{
"geofire" : {
"-KARkcAtgda-pqdqSSkm" : {
".priority" : "dpz8b2rtj8",
"g" : "dpz8b2rtj8",
"l" : [ 43.727821, -79.431598 ]
},
"-KARkcAvzGE_TyRF7MZ5" : {
".priority" : "dr5rvp6hzm",
"g" : "dr5rvp6hzm",
@simonbromberg
simonbromberg / ConvertGithubImageLink.js
Last active September 25, 2020 16:49
Apple Automator Javascript to convert Github default embedded image to a resizable HTML img