Skip to content

Instantly share code, notes, and snippets.

import UIKit
import Swinject
protocol InjectionSegueType {
typealias Source: UIViewController
typealias Destination: UIViewController
var sourceController: Source { get }
@takasek
takasek / CodePiece.swift
Created March 3, 2016 23:19
ジェネリクスはタプルにもなるんですよね。なんか色々応用できそう。 #CodePiece
struct Container<T> {
let value: T
}
let a = Container(value: "a")
a.value // "a"
let b = Container(value: ("b", 2))
b.value // (.0 "b", .1 2)
@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