Skip to content

Instantly share code, notes, and snippets.

import SwiftUI
struct ContentView: View {
var body: some View {
ScrollView {
MyView()
Text("Hello")
}
}
}
// わざわざ async let を使って代入を挟まなくても
// 非同期処理を↓のようにして並行に実行できるようにするのは
//
// await (foo() + bar()) // 並行
// (await foo()) + (await bar()) // 直列
//
// try との整合性の関係で難しそう。
//
// try の仕様が、↓の (A) では "foo" と "bar" が、
// (B) では "foo" のみが表示されるようになっていれば
@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
@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
}

test

@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") {
@koher
koher / ContentView.swift
Created April 3, 2021 16:10
An example how to use onReceive with @published
import SwiftUI
struct ContentView: View {
@StateObject private var state: ContentViewState = .init()
var body: some View {
VStack {
Text(state.count.description)
Button("Count Up") { state.countUp() }
Button("Reset") { state.reset() }
}
func binarySearch<Values>(_ value: Values.Element, in values: Values) -> Int? where Values: Collection, Values.Element: Comparable, Values.Index == Int {
var values: Values.SubSequence = values[...]
while !values.isEmpty {
let midIndex = values.startIndex + (values.endIndex - values.startIndex) / 2
let midValue = values[midIndex]
if value < midValue {
values = values[..<midIndex]
} else if value > midValue {
values = values[(midIndex + 1)...]
} else {
let pp = readLine()!.split(separator: " ")
let e = pp[0]
let y = Int(pp[1])!
let m = Int(pp[2])!
let d = Int(pp[3])!
let dd = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
struct YMD: Comparable {
let y: Int
let aa = (1...100).shuffled().dropFirst(50).sorted()
print(aa.count, 100)
print(aa.map(\.description).joined(separator: " "))
let aaSet = Set(aa)
var count = 0
var ans: [Int] = []
for i in 1 ... 1000 {
if aaSet.contains(i) { continue }