Skip to content

Instantly share code, notes, and snippets.

struct Person {
var name: String
var age: Int
}
var a = [
Person(name: "Foo", age: 20),
Person(name: "Bar", age: 30),
]
protocol Animal {
func foo() -> Int
}
struct Cat: Animal {
var value: Int = 42
func foo() -> Int { return value }
}
func useAnimal(_ animal: Animal) -> Int {
do { // 一時的な変数 foo のスコープを切る
let foo: Foo
switch bar {
case .a:
foo = ...
case .b:
foo = ...
case .c:
foo = ...
}
struct DirectProduct<Left: Sequence, Right: Sequence>: Sequence {
let left: Left
let right: Right
let iteratorType: Iterator.Type
init(_ left: Left, _ right: Right) {
self.left = left
self.right = right
iteratorType = Iterator1.self
}
// もし if let が存在しなかったら・・・
let a: Int? = Int("...")
if a != nil {
// ここでは a は nil ではないとわかっているけど、
// a の型は Int? なので↓のような計算はできない。
print(a * 2) // コンパイルエラー
// a は nil でないとわかっているのに、↓のように
// `Foo` is an immutable class
final class Foo {
let value: Int
init(_ value: Int) {
self.value = value
}
}
// Adds `mutating func` to `Foo`
protocol Incremental {
let xzs = [
(3333, 8650),
(3333, 8634),
(3342, 8634),
(3342, 8633),
(3352, 8633),
(3352, 8634),
(3360, 8634),
(3360, 8638),
(3361, 8638),

キャパシティが増加したタイミングでバッファが作り直されている気がします

これはそうだと思います。その上で、↓のような疑問を持っています。

まず、 ArraySlice の挙動として、 popFirst した場合 startIndex がインクリメントされるだけで、各要素のインデックスは変化しません。

var a: ArraySlice<Int> = [2, 3, 5]
_ = a.popFirst()
print(a[1]) // 3
import Foundation
func measure(_ body: () -> Void) {
let start = Date.timeIntervalSinceReferenceDate
for _ in 0 ..< 10 {
body()
}
let end = Date.timeIntervalSinceReferenceDate
print((end - start) / 10)
}
@koher
koher / BindingExtension.swift
Created January 9, 2021 10:49
Extensions for `Binding` to make collections available with `ForEach` keeping bindings to their elements
import SwiftUI
extension Binding: Identifiable where Value: Identifiable {
public var id: Value.ID {
wrappedValue.id
}
}
extension Binding: Sequence where Value: MutableCollection, Value: RandomAccessCollection, Value.Element: Identifiable {
public typealias Element = Binding<Value.Element>