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
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): |
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
// 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 |
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
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 |
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
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 { |
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 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 |
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 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() |
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
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): |
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 Cocoa | |
enum NSAttributedStringAttribute { | |
case font(NSFont) | |
case foregroundColor(NSColor) | |
// ... | |
static func getDictionary(_ attributes: [NSAttributedStringAttribute]) -> [NSAttributedStringKey: Any] { | |
var result: [NSAttributedStringKey: Any] = [:] | |
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
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 { |
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
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() | |
} |
OlderNewer