Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View gregomni's full-sized avatar

Greg Titus gregomni

  • The Omni Group
  • Eugene
View GitHub Profile
enum Foo {
case A(String, Int)
case B(Int, String, String)
case C(Int)
}
func thingy(arg: Foo) -> String {
var r: String
switch(arg) {
case .A(let s, let i), .B(let i, _, let s):
// c.thingy (c.Foo) -> Swift.String
sil hidden @_TF1c6thingyFOS_3FooSS : $@convention(thin) (@owned Foo) -> @owned String {
// %0 // users: %1, %5, %6, %94
bb0(%0 : $Foo):
debug_value %0 : $Foo, let, name "arg", argno 1 // id: %1
%2 = alloc_box $String, var, name "r" // users: %3, %93
%3 = project_box %2 : $@box String // user: %4
%4 = mark_uninitialized [var] %3 : $*String // users: %79, %89, %91
retain_value %0 : $Foo // id: %5
switch_enum %0 : $Foo, case #Foo.A!enumelt.1: bb1, case #Foo.B!enumelt.1: bb2, case #Foo.C!enumelt.1: bb4 // id: %6
struct DefaultedDictionary<Key: Hashable, Value> {
var dict: [Key:Value]
let fallback: Value
subscript(key: Key) -> Value {
get {
return dict[key] ?? fallback
}
set {
dict[key] = newValue
protocol Fallbackable {
associatedtype Value
static var fallback: Value { get }
}
struct DefaultDict<K: Hashable, V, F: Fallbackable where V == F.Value> {
var dict: [K:V]
subscript(key: K) -> V {
get {
extension CGSize {
func scaled(byFactor: CGFloat) -> CGSize {
return CGSize(width: width*byFactor, height: height*byFactor)
}
}
func scaleAndCropImage(image: UIImage, toSize size: CGSize, fitImage: Bool = true) -> UIImage {
guard image.size != size else { return image }
// Calculate scale factor for fit or fill
extension CGSize {
func scaled(byFactor: CGFloat) -> CGSize {
return CGSize(width: width*byFactor, height: height*byFactor)
}
}
extension UIImage {
static func drawnVia(size: CGSize, opaque: Bool = false, _ scale: CGFloat = 0.0, draw: () -> ()) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, opaque, scale)
draw()
public func sequence<T>(first: T, next: (T) -> T?, while test: (T) -> Bool) -> UnfoldSequence<T, (T?, Bool)> {
return sequence(state: (first, true), next: { (state: inout (T?, Bool)) -> T? in
switch state {
case (let value?, true):
state.1 = false
if !test(value) {
state.0 = nil; return nil
}
return value
case (let value?, false):
import Cocoa
enum NSAttributedStringAttribute {
case font(NSFont)
case foregroundColor(NSColor)
// ...
static func getDictionary(_ attributes: [NSAttributedStringAttribute]) -> [NSAttributedStringKey: Any] {
var result: [NSAttributedStringKey: Any] = [:]
protocol Fallbackable {
associatedtype Value
static var fallback: Value { get }
}
struct DefaultDict<K: Hashable, V, F: Fallbackable> where V == F.Value {
var dict: [K:V]
subscript(key: K) -> V {
get {
public protocol Thing {
associatedtype T
func work() throws -> T
}
public struct AnyThing<T> : Thing {
let doWork: () throws -> T
public func work() throws -> T {
return try doWork()
}