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
//: Second Type Erasure | |
import Foundation | |
protocol Pokemon { | |
typealias Power | |
func attack(power: Power) | |
} | |
private class _AnyPokemonBoxBase<Power>: Pokemon { |
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 Actionable { | |
func action() | |
} | |
enum Planets: Actionable { | |
case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune | |
} | |
protocol PlanetsKind {} |
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 UIKit | |
import XCPlayground | |
var str = "Hello, playground" | |
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true | |
let config = NSURLSessionConfiguration.defaultSessionConfiguration() | |
config.HTTPAdditionalHeaders = ["foo": "bar"] |
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 PokeCorrall { | |
let rows: Int, columns: Int | |
var grid: [Double] | |
init(rows: Int, columns: Int) { | |
self.rows = rows | |
self.columns = columns | |
grid = Array(count: rows * columns, repeatedValue: 0.0) | |
} | |
func indexIsValidForRow(row: Int, column: Int) -> Bool { |
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 UIKit | |
extension String { | |
var isBlank: Bool { | |
return trimmingCharacters(in: CharacterSet.whitespaces).isEmpty | |
} | |
} |
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
//: Cards | |
import Foundation | |
enum CardColor { | |
case red, black | |
} | |
enum CardSuit: CustomStringConvertible { | |
case heart, diamond, club, spade |
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 Foundation | |
private enum RandomSource { | |
static private let file = fopen("/dev/urandom","r")! | |
static private let queue = DispatchQueue(label: "random") | |
static func get(count: Int) -> [Int8] { | |
let capacity = count + 1 // fgets adds null termination | |
var data = UnsafeMutablePointer<Int8>.allocate(capacity: capacity) | |
defer { | |
data.deallocate(capacity: capacity) |
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
let str = "acxz" | |
func isFunny(_ s: AnySequence<UInt8>) -> Bool { | |
let n1: [Int] = zip(s.dropFirst(), s).map { Int($0)-Int($1) } | |
let n2: [Int] = zip(s.reversed().dropFirst(), s.reversed()).map { Int($1)-Int($0) } | |
return n1 == n2 | |
} | |
isFunny(AnySequence(str.utf8)) |
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
let str = "Hello, Keita" | |
var index = str.characters.startIndex | |
while index < str.characters.endIndex { | |
print(str.characters[index]) | |
str.characters.formIndex(after: &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 enum LinkedList<T> { | |
case end | |
indirect case node(value: T, next: LinkedList) | |
public func cons(_ value: T) -> LinkedList { | |
return .node(value: value, next: self) | |
} | |
} |