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 Animal { | |
case Cat | |
case Dog(kind: String) | |
case Human | |
} | |
func hoge(animal: Animal, isTamed: Bool, age: Int) { | |
switch (animal, isTamed) { | |
case (.Cat, true): | |
print("tamed cat") |
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
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
switch (tableView.dequeueReusableCellWithIdentifier("cardCell", forIndexPath: indexPath), hand[indexPath.row]) { | |
case (let cell as? CardCell, let data as? Card): | |
cell.fillWith(data) | |
case (let cell as? HogeCell, let data as? Hoge): | |
cell.fillWith(data) | |
case (let cell as? FugaCell, let data as? Fuga): | |
cell.fillWith(data) | |
default: |
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 a: Int = [10,10,10] | |
.enumerate() | |
.reduce(0) { (sum: Int, item: (index: Int, value: Int)) in | |
sum + item.value + (item.index == 0 ? 0 : 1) | |
} |
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 FoodType {} | |
struct Spirit: FoodType {} | |
struct Meet: FoodType {} | |
protocol MonsterType { | |
typealias Food: FoodType | |
} |
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 Hoge { | |
lazy var fuga: String = "fuga" | |
} | |
let hoge = Hoge() | |
let str = hoge.fuga // Cannot use mutating getter on immutable value: 'hoge' is a 'let' constant |
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 uekizan<S: SequenceType>(sources: S, insert margin: S.Generator.Element) -> AnyGenerator<S.Generator.Element> { | |
var gen = sources.generate() | |
var isMargin = false | |
var preserved: S.Generator.Element? = gen.next() | |
return anyGenerator { | |
defer { | |
isMargin = !isMargin | |
} | |
switch (isMargin, preserved) { |
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 uekizanGenerator<S: SequenceType>(sources: S, insert margin: S.Generator.Element) -> AnyGenerator<S.Generator.Element> { | |
var gen = sources.generate() | |
var isMargin = false | |
var preserved: S.Generator.Element? = gen.next() | |
func getMargin() -> S.Generator.Element? { | |
guard preserved != nil else { return nil } | |
return margin | |
} |
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 UekizanGenerator<S: SequenceType, E where S.Generator.Element == E> : GeneratorType { | |
typealias Element = E | |
private let margin: E | |
private var elementGenerator: S.Generator | |
private var preserved: E? | |
private var isMargin = false | |
init(_ sources: S, margin: E) { | |
self.margin = margin |
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
// indexを先に与えてカリー化した関数を返し、そこにcollectionを与えると要素が返ってくる関数 | |
func getObject<C: CollectionType, I: ForwardIndexType where C.Index == I>(index: I) | |
-> C -> C.Generator.Element { | |
return { collection in | |
return collection[index] | |
} | |
} | |
// 一気に呼べばうまくいく | |
getObject(2)(["0ばんめ","1ばんめ","2ばんめ"]) // "2ばんめ" |
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
// Dictionaryを束縛するwhereの書き方がわからず、動かないコードですが、雰囲気は伝わるかな | |
func getValue<D: Dictionary, K: Hashable, V where K == D.Key, V == D.Value>(key: K) | |
-> D -> V { | |
return { dict in | |
return dict[key] | |
} | |
} | |
let gv = getValue("k") as [String: ❓❓❓] |