Skip to content

Instantly share code, notes, and snippets.

@koher
koher / ContentView.swift
Created April 3, 2021 15:44
An example how to trigger animations with SwiftUI
import SwiftUI
struct ContentView: View {
@StateObject private var state: ContentViewState = .init()
var body: some View {
ZStack {
VStack {
Text(state.count.description)
Button("Count Up") {

test

@koher
koher / main.swift
Created February 21, 2021 15:29
https://atcoder.jp/contests/arc113/tasks/arc113_b が「7進法」だった場合の解
func readInt3(line: Int = #line, file: String = #file) -> (Int, Int, Int) {
guard let string = readLine() else {
preconditionFailure("No input (at line \(line) in \(file))")
}
let values: [Int] = string.split(separator: " ").map { part in
guard let value = Int(part) else {
preconditionFailure("Illegal input value: \(string) (at line \(line) in \(file))")
}
return value
}
@koher
koher / main.swift
Last active February 21, 2021 15:20
a^iの7進法での1の位の周期を調べる
func pow<Integer>(_ a: Integer, _ b: Integer, modulus: Integer?) -> Integer where Integer: BinaryInteger {
var result: Integer = 1
var a = a
var b = b
if let modulus = modulus {
while true {
if b & 0x1 != 0 {
result = (result * a) % modulus
}
b >>= 1
// わざわざ async let を使って代入を挟まなくても
// 非同期処理を↓のようにして並行に実行できるようにするのは
//
// await (foo() + bar()) // 並行
// (await foo()) + (await bar()) // 直列
//
// try との整合性の関係で難しそう。
//
// try の仕様が、↓の (A) では "foo" と "bar" が、
// (B) では "foo" のみが表示されるようになっていれば
import SwiftUI
struct ContentView: View {
var body: some View {
ScrollView {
MyView()
Text("Hello")
}
}
}
@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>
import Foundation
func measure(_ body: () -> Void) {
let start = Date.timeIntervalSinceReferenceDate
for _ in 0 ..< 10 {
body()
}
let end = Date.timeIntervalSinceReferenceDate
print((end - start) / 10)
}

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

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

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

var a: ArraySlice<Int> = [2, 3, 5]
_ = a.popFirst()
print(a[1]) // 3
let xzs = [
(3333, 8650),
(3333, 8634),
(3342, 8634),
(3342, 8633),
(3352, 8633),
(3352, 8634),
(3360, 8634),
(3360, 8638),
(3361, 8638),