Skip to content

Instantly share code, notes, and snippets.

@takasek
takasek / CodePiece.swift
Created March 3, 2016 23:50
switchとタプルの組み合わせは、ネスト1段でたくさんのケースを網羅的に記述できるので大好きです。 #CodePiece
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")
@takasek
takasek / CodePiece.swift
Created March 21, 2016 14:22
TableViewCellとそのデータを複数パターン用意したい場合は、cellForRowAtIndexPathでswitch使うのが便利だと思う #CodePiece
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:
@takasek
takasek / CodePiece.swift
Created March 29, 2016 05:27
高階関数で植木算、どうしたらエレガントに書けるかな。[10,10,10]にマージン1を足して32を出力したい。enumerateしてindexを見て分岐? #CodePiece
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)
}
@takasek
takasek / CodePiece.swift
Last active April 2, 2016 05:10
しょっぱなに質問したら、要求を実現するための型消去の実装の検討に付き合っていただいた…! #CodePiece #cswift
// 最初のコード
protocol FoodType {}
struct Spirit: FoodType {}
struct Meet: FoodType {}
protocol MonsterType {
typealias Food: FoodType
}
@takasek
takasek / CodePiece.swift
Created April 2, 2016 06:44
うわあ…面白い #CodePiece #cswift
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
@takasek
takasek / CodePiece.swift
Created April 3, 2016 09:02
AnyGeneratorで植木算。なんとかステートレスにしたいんだけど難しいかなぁ… #CodePiece #cswift
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) {
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
}
@takasek
takasek / CodePiece.swift
Last active April 3, 2016 10:58
熊谷さんの https://t.co/VICPp6Pmbg を参考にしつつ、さっき書いた植木算ジェネレータをstruct化してみた。 #CodePiece #cswift
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
@takasek
takasek / CodePiece.swift
Created April 14, 2016 07:55
ジェネリック型を取り扱うカリー化関数を作りたいんだけど、コンパイルエラーでうまくいかない… #CodePiece
// 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ばんめ"
@takasek
takasek / CodePiece.swift
Created April 14, 2016 08:28
カリー化関数自体をジェネリクスとして扱いたいんだけど無理だろうか #CodePiece
// 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: ❓❓❓]