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
func map<A,B,C>(f1: (B) -> C, f2: (A) -> B) -> (A) -> C { | |
return { f1(f2($0)) } | |
} | |
func addOne(i: Int) -> Int { | |
return i + 1 | |
} | |
func convertToString(i: Int) -> String { |
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
//: Playground - noun: a place where people can play | |
import Cocoa | |
let a: Int? = 3 | |
let b: Int? = 3 | |
public func equateThem <T: Equatable>(lhs : Optional<T>, rhs : Optional<T>) -> Bool { | |
return lhs! == rhs! | |
} |
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 StructTest {} | |
class ClassTest {} | |
class WillBeConstrained<T> {} | |
// Error: type 'T' constrained to non-protocol type 'StructTest' | |
extension WillBeConstrained where T: StructTest { | |
} |
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
// Example of a thing I cannot do with types in Swift | |
struct Operation<Input, Output> { | |
// would also be nice to have a list of operations that yield different types (and be able to dynamically inspect the generic type) | |
var operations: [Operation<>] = [] | |
// This can be accomplished by using type erause | |
// would be nice to be able to leave a type hole here | |
var nextOperation: Operation<Output, _> // second generic argument can be any type from the perspective of this specific type | |
// only important aspect is that input of next operation matches output of this operation |
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 struct NonEmptyCollection<T: CollectionType> { | |
let collection: T | |
init?(collection: T) { | |
if !collection.isEmpty { | |
self.collection = collection | |
} else { | |
return nil | |
} |
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
// Benjamin Encz | |
// Note: Proof of Concept, you should definitely not use this! | |
import Cocoa | |
public protocol OptionalType { | |
init() | |
func unwrap() -> 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
struct AIndex: ForwardIndexType { | |
let index: Int | |
func successor() -> AIndex { | |
return AIndex(index: self.index + 1) | |
} | |
} | |
func == (lhs: AIndex, rhs: AIndex) -> Bool { | |
return lhs.index == rhs.index |
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 X { | |
func _ok(a: Any) | |
} | |
public protocol StoreSubscriber: X { | |
typealias StoreSubscriberStateType | |
typealias AppStateType | |
func newState(state: StoreSubscriberStateType) |
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
class RxObserver<T>: StoreSubscriber { | |
var state: Variable<T?> = Variable(nil) | |
private var store: MainStore | |
init(store: MainStore) { | |
self.store = store | |
self.store.subscribe(self) | |
} |
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
class Parent { | |
var child: Child? { | |
didSet { | |
child.callback = { [weak self] in | |
delegateCallback() | |
} | |
} | |
} | |