日記
今日からタイトルのフォーマットを「n日目 hogehoge」にした.そういえばピザ窯の記事は結局書かなかった.
今日書いたコード
今日はLogical Operatorsの話だった.今日一番時間がかかったのはconditions
を生成するコード.
var conditions: [[Bool]] = [] for i in 0b000...0b111 { conditions.append([ (i >> 2) & 0b001 == 1, (i >> 1) & 0b001 == 1, (i >> 0) & 0b001 == 1, ]) } for condition in conditions { print(condition) print(echo(condition[0]) || echo(condition[1]) && echo(condition[2])) } func echo(_ some: Bool) -> Bool { print("called echo with: \(some)") return some }
初めて知ったこと
なんとなくSwiftもそうだろうなと思ってたけどやっぱりそうだった.そうというのはshort-circuit evaluationのことで,概念は知っていたけど名前は知らなかった.日本語だと短絡評価というらしい.
Swiftは短絡評価を採用しているので上記のコードの実行結果は以下のようになる.
[false, false, false] called echo with: false called echo with: false false [false, false, true] called echo with: false called echo with: false false [false, true, false] called echo with: false called echo with: true called echo with: false false [false, true, true] called echo with: false called echo with: true called echo with: true true [true, false, false] called echo with: true true [true, false, true] called echo with: true true [true, true, false] called echo with: true true [true, true, true] called echo with: true true