Skip to content

Instantly share code, notes, and snippets.

@takecian
Created March 6, 2016 09:04
Show Gist options
  • Save takecian/32bbb3022b93a4ddfb0a to your computer and use it in GitHub Desktop.
Save takecian/32bbb3022b93a4ddfb0a to your computer and use it in GitHub Desktop.
hipsterSwift
//: Playground - noun: a place where people can play
// @noescape
class Kraken {}
class KrakenGeneral {
var soldiers = [Kraken]()
func startTheWar() {
prepareTheKrakenArmyForBattle(punishments: {
soldiers.removeFirst() // ここで self を書く必要がありません
})
}
func prepareTheKrakenArmyForBattle(@noescape punishments punishments: () -> Void) {
punishments()
}
}
let general = KrakenGeneral()
general.soldiers.append(Kraken())
general.startTheWar()
// @autoclosure
class KrakenGeneralEx {
var soldiers = [Kraken]()
var isRemove = true
func punishSoldier(@autoclosure soldierPunishment: () -> Kraken) {
// この例ではここで 兵士 を配列から取り除くかどうかを判定しています
if isRemove {
// ここで実際に兵士を配列から取り除く(removeFirst()を実行しています)
sendSoldierHomeToMommy(soldierPunishment())
} else {
// do nothing
}
}
func sendSoldierHomeToMommy(soldier: Kraken) {
}
}
let generalEx = KrakenGeneralEx()
generalEx.soldiers.append(Kraken())
generalEx.punishSoldier(generalEx.soldiers.removeFirst())
//@autoclosure は closure に適応可能です。 ここでは removeFirst() と同じ関数型(引数と戻り値)を定義します。
// Lazy vars
struct Human {
let name: String
}
class KrakenFoodGenerator {
lazy var lazyHuman: Human = self.generateWithOptions()
func generateWithOptions() -> Human {
return Human(name: "human")
}
deinit {
print("called")
}
}
var gen: KrakenFoodGenerator? = KrakenFoodGenerator()
gen?.lazyHuman
gen = nil
// 可変長引数
func printEverythingWithAKrakenEmojiInBetween(objectsToPrint: String...) {
for object in objectsToPrint {
print("\(object)🐙")
}
}
// Curry
//This will print "Hey🐙Look🐙At🐙Me🐙!🐙"
printEverythingWithAKrakenEmojiInBetween("Hey", "Look", "At", "Me", "!")
func fourChainedFunctions(a: Int) -> (Int -> (Int -> (Int -> Int))) {
return { b in
return { c in
return { d in
return a + b + c + d
}
}
}
}
fourChainedFunctions(1)(2)(3)(4)
func fourChainedFunctionsEx(a: Int)(b: Int)(c: Int)(d: Int) -> Int {
return a + b + c + d
}
fourChainedFunctionsEx(1)(b: 2)(c: 3)(d: 4)
print("a", "b")
//
func wreakHavocOnHumanity(callingFunction: String = __FUNCTION__) {
//ここにバグがある!この関数が呼ばれすぎている!
//原因を突き止めるために呼び出し元を関数名を print 文で書き出す
print(callingFunction, "called \(__FUNCTION__)")
}
func startTheWar() {
wreakHavocOnHumanity()
}
func startEatingDinner(platter: Human) {
wreakHavocOnHumanity()
}
let human = Human(name: "human")
startTheWar() //prints "startTheWar called wreakHavocOnHumanity"
startEatingDinner(human) //prints "startEatingDinner called wreakHavocOnHumanity"
// labeld loop
struct Section {
let name: String
}
struct Row {
var isMagical: Bool
}
func loop() {
var sections = [Section]()
sections.append(Section(name: ""))
sections.append(Section(name: ""))
sections.append(Section(name: ""))
var rows = [Row]()
rows.append(Row(isMagical: true))
rows.append(Row(isMagical: false))
sectionLoop: for section in sections {
print("name = \(section.name)")
rowLoop: for row in rows {
if row.isMagical {
break sectionLoop
}
}
}
}
loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment