Skip to content

Instantly share code, notes, and snippets.

struct SortedArray<Element, ComparableProperty: Comparable> {
let propertyAccessor: (Element) -> ComparableProperty
private var elements: [Element]
public init(propertyAccessor: @escaping (Element) -> ComparableProperty) {
self.elements = []
self.propertyAccessor = propertyAccessor
}
extension Character: Strideable {
public typealias Stride = Int
public func distance(to other: Character) -> Character.Stride {
guard let first = self.unicodeScalars.first else {
fatalError()
}
guard let other = other.unicodeScalars.first else {
fatalError()
}
import Foundation
public enum Method: String {
case GET
case POST
case PUT
case PATCH
case DELETE
}
//Copyright 2019 Soroush Khanlou
//
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE O
import SwiftUI
struct IdentifiableError: Identifiable {
let error: Error
var id: String {
let nsError = error as NSError
return "\(nsError.domain) \(nsError.code)"
}
@khanlou
khanlou / CleanUpGit.md
Last active July 7, 2021 08:08
Clean up all merged branches

To clean up git, run:

git remote prune origin; git branch --merged | grep -v '^* master$' | grep -v '^  master$' | xargs git branch -d

If it doesn't like that because it thinks the repo is not there, change the remote from http://github.com to http://khanlou@github.com

This is a quick way to approximate how many person-hours have put into a git repo.

git log --pretty=format:"%ae %ad" --date=format:'%Y-%m-%d %H' | uniq | wc -l

The --pretty=format gives you the author email and commit date, and the date is formatted to just include the year, month, day and hour, like so: 2020-12-22 16. You can add a grep before or after the unique to filter out by author. wc -l tells you the number of lines, which are uniqued, meaning if you had two commits in the same hour, those are filtered out.

@khanlou
khanlou / CanCan.swift
Created February 14, 2021 03:09
A version of Ruby's cancan for Swift.
import Foundation
struct CanCan<Identity, Action: Equatable> {
var registry: [(String, (Identity, Any) -> [Action])] = []
mutating func register<T>(_ type: T.Type, _ block: @escaping (Identity, T) -> [Action]) {
self.registry.append((String(describing: type), { (identity: Identity, object: Any) -> [Action] in
guard let casted = object as? T else {
return []
//
// IndexedForEach.swift
//
//
// Created by Soroush Khanlou on 1/21/20.
//
//
import Foundation
import SwiftUI
import SwiftUI
enum ScalableFont {
case system(size: CGFloat, weight: Font.Weight = .regular, design: Font.Design = .default)
case custom(_ name: String, size: CGFloat)
var size: CGFloat {
switch self {