This file contains hidden or 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
| // from Swiftz | |
| public class K1<A> { public init() {} } | |
| protocol Monad { | |
| typealias A | |
| typealias B | |
| typealias FB = K1<B> | |
| func bind(A -> FB) -> FB; | |
| static func ret(A) -> Self | |
| } |
This file contains hidden or 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
| var x: X? = X() | |
| // leaks if closure1 is called, and it does not capture self by [unowned self] | |
| //x!.closure1() | |
| // if capture self, crashes because x is deallocated befor closure is called. | |
| x!.method() | |
| x = nil | |
| extension NSTimer { |
This file contains hidden or 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 Eq { | |
| typealias A = Self | |
| func equalsTo(a: A) -> Bool | |
| func notEqualsTo(a: A) -> Bool | |
| } | |
| extension Eq { | |
| func equalsTo(a: A) -> Bool { return !self.notEqualsTo(a) } | |
| func notEqualsTo(a: A) -> Bool { return self.equalsTo(a) } | |
| } |
This file contains hidden or 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 MyEq a where | |
| equalsTo :: a -> a -> Bool | |
| equalsTo x y = not (notEqualsTo x y) | |
| notEqualsTo :: a -> a -> Bool | |
| notEqualsTo x y = not (equalsTo x y) | |
| instance MyEq Integer where | |
| equalsTo x y = x == y | |
| main = do putStrLn (show $ notEqualsTo (1::Integer) (2::Integer)) |
This file contains hidden or 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 XCTest | |
| //@testable import Playground | |
| class StructVsClassTests: XCTestCase { | |
| let times = 1000000 | |
| // 0.023 sec | |
| // 0.002 sec on Release build | |
| func testPerformanceStructConstructor() { | |
| // This is an example of a performance test case. |
This file contains hidden or 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 UIKit | |
| var str = "Hello, playground" | |
| /* | |
| class (Eq a, Show a) => Num a where | |
| (+) :: a -> a -> a |
This file contains hidden or 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 | |
| // Light-weight utility of observable stream | |
| // https://gist.github.com/staltz/868e7e9bc2a7b8c1f754 | |
| class Observer<T> { | |
| typealias EventHandler = T -> () | |
| let eventHandler: EventHandler | |
| init(eventHandler: EventHandler) { | |
| self.eventHandler = eventHandler | |
| } |
This file contains hidden or 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
| // 1. same name of functions with different signature in both protocol and protocol extension | |
| // 2. someone is conforming to the protocol | |
| // This should be compile error though, error does not happen. Instead Compiler crashes in AST -> SIL conversion phase. | |
| protocol FooProtocol { | |
| func sameName(differentSignature: Bool) -> UITableViewCell? | |
| } | |
| extension FooProtocol { | |
| func sameName() -> UITableViewCell { | |
| return UITableViewCell() |
This file contains hidden or 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 Tag { | |
| let tagName: NSString | |
| let element: NSString | |
| init(tagName: NSString, element: NSString) { | |
| self.tagName = tagName | |
| self.element = element | |
| } | |
| static let open: NSString = "<" |
This file contains hidden or 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 MyProtocol: class { | |
| typealias MyAlias | |
| var something: MyAlias { get } | |
| } | |
| extension MyProtocol where MyAlias: YourProtocol { | |
| func bar() { | |
| // !!! Compile Error happens !!! | |
| // expected an argument list of type '(Self.AliasTypeAlias)' | |
| // self.something.foo { _ in print("X") } |