This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ShapeScript document | |
detail 32 | |
define roundedcube { | |
option radius 0.25 | |
option size 1 1 1 | |
detail max(4 detail) | |
define size_ size | |
define diameter radius * 2 / size_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
enum OSDocumentError: Error { | |
case unknownFileFormat | |
} | |
#if canImport(UIKit) | |
import UIKit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// Here's a pretty typical scenario where you want to encode a polymorphic type - | |
// in this case a Shape type that can be either a Square or Circle. Swift provides | |
// a nice pattern for doing this in a type-safe way using enums: | |
struct Circle: Codable { | |
var radius: Int | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UITextField { | |
/// Add a trailing placeholder label that tracks the text as it changes | |
func addTrailingPlaceholder(_ placeholder: String) { | |
let label = UILabel() | |
label.text = placeholder | |
label.alpha = 0.3 | |
label.isHidden = true | |
let container = UIView() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A quick test to compare performance of different dictionary lookups in Swift. | |
// Note: Results recorded on a 2020 M1 MBP. be sure to run with "Optimize for Speed [-O] | |
import Foundation | |
let iterations = 1000000 | |
var dictionary = [Int: Int]() | |
for i in 0 ..< iterations { | |
dictionary[i] = Int.random(in: 0 ..< 1000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This gist demonstrates how you can implement versioning for a Codable struct to support loading | |
// old serialized data after changing the structure. Notable features of this solution: | |
// | |
// * No need to make new properties optional, or to perform post-processing on the struct after | |
// loading in ordeer to populate missing values | |
// * No need to change the call site - from the outside this struct behaves just the same | |
// as if we had implemented codable directly in the normal way. | |
// * Versioning can be applied individually to parents or leaves in a larger tree of | |
// structs without affecting the other elements | |
// * This approach will work even if the original struct was not designed with versioning in mind |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Data { | |
init?(hexString: String) { | |
let count = hexString.count / 2 | |
var data = Data(capacity: count) | |
var i = hexString.startIndex | |
for _ in 0 ..< count { | |
let j = hexString.index(after: i) | |
if var byte = UInt8(hexString[i ... j], radix: 16) { | |
data.append(&byte, count: 1) | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
// Create color using P3 color space | |
let linearColor = UIColor(displayP3Red: 1, green: 0.5, blue: 0.2, alpha: 1) | |
do { | |
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0 | |
linearColor.getRed(&r, green: &g, blue: &b, alpha: &a) | |
print(r, g, b, a) // 1.07, 0.46, 0.0, 1.0 - not the expected values | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// The Jaro-Winkler edit distance between two strings (0 - 1) | |
func editDistance(_ lhs: String, _ rhs: String) -> Double { | |
return 1 - jaroWinklerSimilarity(Array(lhs), Array(rhs)) | |
} | |
/// Jaro-Winkler similarity between two strings (0 - 1) | |
/// https://www.geeksforgeeks.org/jaro-and-jaro-winkler-similarity/ | |
private func jaroWinklerSimilarity(_ s1: [Character], _ s2: [Character]) -> Double { | |
var jaro = jaroSimilarity(s1, s2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
enum LexingError: Error, Equatable { | |
case syntaxError(String) | |
case unexpectedEOF | |
} | |
enum ParsingError: Error { | |
case expected(String) | |
case unexpectedToken(Token) |
NewerOlder