Skip to content

Instantly share code, notes, and snippets.

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

Scott Gardner scotteg

🏠
Working from home
View GitHub Profile
@evansunleyjames
evansunleyjames / ConditionalTipModifier.swift
Last active June 6, 2024 16:20
A tip for showing TipKit tips conditionally in SwiftUI
/**
Conditionally include a tip - could be achieved with parameters on the tip, but this could save repetition if there are enough tips that depend on the same condition.
Usage:
.showTipConditionally(someCondition) { view in
view
.popoverTip(myTip)
.whateverOtherModifiers()
}
*/
@JacopoMangiavacchi
JacopoMangiavacchi / MLCompute.swift
Last active January 15, 2021 07:14
MLCompute Playground
import Foundation
import MLCompute
import PlaygroundSupport
let page = PlaygroundPage.current
page.needsIndefiniteExecution = true
let tensor1 = MLCTensor(descriptor: MLCTensorDescriptor(shape: [6, 1], dataType: .float32)!)
let tensor2 = MLCTensor(descriptor: MLCTensorDescriptor(shape: [6, 1], dataType: .float32)!)
let tensor3 = MLCTensor(descriptor: MLCTensorDescriptor(shape: [6, 1], dataType: .float32)!)
@Libranner
Libranner / synth.swift
Created June 12, 2020 13:05
AVSpeechSynthesizer + SwiftUI
import SwiftUI
import AVFoundation
struct ContentView: View {
var synthVM = SynthViewModel()
var body: some View {
VStack {
Text("This is Office Hours")
@lattner
lattner / TaskConcurrencyManifesto.md
Last active November 18, 2024 17:28
Swift Concurrency Manifesto
@lattner
lattner / async_swift_proposal.md
Last active October 29, 2024 18:53 — forked from oleganza/async_swift_proposal.md
Concrete proposal for async semantics in Swift

Async/Await for Swift

Introduction

Modern Cocoa development involves a lot of asynchronous programming using closures and completion handlers, but these APIs are hard to use. This gets particularly problematic when many asynchronous operations are used, error handling is required, or control flow between asynchronous calls gets complicated. This proposal describes a language extension to make this a lot more natural and less error prone.

This paper introduces a first class Coroutine model to Swift. Functions can opt into to being async, allowing the programmer to compose complex logic involving asynchronous operations, leaving the compiler in charge of producing the necessary closures and state machines to implement that logic.

@scotteg
scotteg / SwiftCGPointsOnCircle.swift
Last active January 22, 2020 12:57
Returns an array of `CGPoint`s that are evenly distributed around the circumference of a circle for a given radius, center x, center y, and number of decimal points precision.
import UIKit
/**
Returns an array of `CGPoint`s that are evenly distributed around the circumference of a circle for a number of points, center x, center y, and radius of the circle, and maximum number of decimal points precision for the x and y values.
- author: Scott Gardner
- parameter numberOfPoints: the number of points to plot; 1 or more
- parameter centerX: the center `x` of the circle
- parameter centerY: the center `y` of the circle
- parameter radius: the radius of the circle (distance from center)
- parameter precision: the maximum number of decimal places precision, 0 or higher, **defaults to 3**
@scotteg
scotteg / SwiftQueryItemsDictionaryNSURLExtension.swift
Last active November 30, 2015 13:10
Swift NSURL extension that returns a Dictionary of String keys and String values for each key/value pair in an NSURL query string.
import Foundation
/**
Returns a `Dictionary` of `String` keys and `String` values for each key/value pair in an `NSURL` query string.
- author: Scott Gardner
- seealso:
* [Source on GitHub](http://bit.ly/SwiftQueryItemsDictionaryNSURLExtension)
*/
extension NSURL {
@scotteg
scotteg / SwiftArrayOfUIViewsSortInPlaceByTagExtension.swift
Last active April 11, 2020 05:18
Sorts an array of UIViews or subclasses by tag.
extension Array where Element: UIView {
/**
Sorts an array of `UIView`s or subclasses by `tag`. For example, this is useful when working with `IBOutletCollection`s, whose order of elements can be changed when manipulating the views in Interface Builder. Just tag your views in Interface Builder and then call this method on your `IBOutletCollection`s in `viewDidLoad()`.
- author: Scott Gardner
- seealso:
* [Source on GitHub](http://bit.ly/SortUIViewsInPlaceByTag)
*/
mutating func sortUIViewsInPlaceByTag() {
sortInPlace { (left: Element, right: Element) in
@scotteg
scotteg / HashableType.swift
Last active December 16, 2016 15:15
A Swift HashableType that enables nesting a Set of any Hashable type within a Set
func ==(x: HashableType, y: HashableType) -> Bool {
return x.isEqual(y.value)
}
struct HashableType: Hashable {
let value: Any
var hashValue: Int {
return getHashValue()
}
@scotteg
scotteg / SwiftIntIsPrimeExtension.swift
Created August 8, 2015 12:33
A Swift 2 Int extension that checks if an integer is a prime number.
extension Int {
/**
`true` if self is a prime number, i.e., can only be divided evenly by 1 and itself.
- author: Scott Gardner
- seealso:
* [Source on GitHub](http://bit.ly/SwiftIntIsPrimeExtension)
*/
public var isPrime: Bool {
guard self > 1 else {