Swift 有没有一边倒的步伐?
Does Swift have one-sided strides?
我想做这样的事情:
let sequence1 = stride(from: Int32(2), by: 2)
for (i, address) in zip(sequence1, addresses) {
sqlite3_bind_int64(stmt, i, address.userID)
sqlite3_bind_int(stmt, i+1, address.deviceID)
}
但是 stride(from:by:)
不存在。
我知道我可以将第一行更改为:
let sequence1 = stride(from: Int32(2), through: Int32.max, by: 2)
或:
let sequence1 = sequence(first: Int32(2), next: { [=13=] + 2 })
而且我知道 Swift 有单向范围,例如 PartialRangeFrom
,但是 Swift 有单向步幅吗?
相关:
不,Swift没有一边倒的步伐。
对于一般的(延迟评估的)序列迭代每个第二个整数你的
let sequence1 = sequence(first: Int32(2), next: { [=10=] + 2 })
是一个简单、清晰且灵活的解决方案。另一种选择是
let sequence1 = (Int32(1)...).lazy.map { [=11=] * 2 }
在你的特殊情况下,我会简单地使用单边范围:
for (i, address) in zip(Int32(1)..., addresses) {
sqlite3_bind_int64(stmt, 2 * i, address.userID)
sqlite3_bind_int(stmt, 2 * i + 1, address.deviceID)
}
就像他们说的,不。但无论如何你都不想要那个。加分太多了。打破你的部分范围! ⚒️
public extension Sequence {
/// Splits a `Sequence` into equal "chunks".
/// - Parameter maxArrayCount: The maximum number of elements in a chunk.
/// - Returns: `Array`s with `maxArrayCount` `counts`,
/// until the last chunk, which may be smaller.
subscript(maxArrayCount maxCount: Int) -> AnySequence<[Element]> {
.init(
sequence( state: makeIterator() ) { iterator in
Optional(
(0..<maxCount).compactMap { _ in iterator.next() },
nilWhen: \.isEmpty
)
}
)
}
}
public extension Optional {
/// Wraps a value in an optional, based on a condition.
/// - Parameters:
/// - wrapped: A non-optional value.
/// - getIsNil: The condition that will result in `nil`.
init(
_ wrapped: Wrapped,
nilWhen getIsNil: (Wrapped) throws -> Bool
) rethrows {
self = try getIsNil(wrapped) ? nil : wrapped
}
}
zip( (2...)[maxArrayCount: 2], addresses ).forEach {
sqlite3_bind_int64(stmt, [=12=][0], .userID)
sqlite3_bind_int(stmt, [=12=][1], .deviceID)
}
我想做这样的事情:
let sequence1 = stride(from: Int32(2), by: 2)
for (i, address) in zip(sequence1, addresses) {
sqlite3_bind_int64(stmt, i, address.userID)
sqlite3_bind_int(stmt, i+1, address.deviceID)
}
但是 stride(from:by:)
不存在。
我知道我可以将第一行更改为:
let sequence1 = stride(from: Int32(2), through: Int32.max, by: 2)
或:
let sequence1 = sequence(first: Int32(2), next: { [=13=] + 2 })
而且我知道 Swift 有单向范围,例如 PartialRangeFrom
,但是 Swift 有单向步幅吗?
相关:
不,Swift没有一边倒的步伐。
对于一般的(延迟评估的)序列迭代每个第二个整数你的
let sequence1 = sequence(first: Int32(2), next: { [=10=] + 2 })
是一个简单、清晰且灵活的解决方案。另一种选择是
let sequence1 = (Int32(1)...).lazy.map { [=11=] * 2 }
在你的特殊情况下,我会简单地使用单边范围:
for (i, address) in zip(Int32(1)..., addresses) {
sqlite3_bind_int64(stmt, 2 * i, address.userID)
sqlite3_bind_int(stmt, 2 * i + 1, address.deviceID)
}
就像他们说的,不。但无论如何你都不想要那个。加分太多了。打破你的部分范围! ⚒️
public extension Sequence {
/// Splits a `Sequence` into equal "chunks".
/// - Parameter maxArrayCount: The maximum number of elements in a chunk.
/// - Returns: `Array`s with `maxArrayCount` `counts`,
/// until the last chunk, which may be smaller.
subscript(maxArrayCount maxCount: Int) -> AnySequence<[Element]> {
.init(
sequence( state: makeIterator() ) { iterator in
Optional(
(0..<maxCount).compactMap { _ in iterator.next() },
nilWhen: \.isEmpty
)
}
)
}
}
public extension Optional {
/// Wraps a value in an optional, based on a condition.
/// - Parameters:
/// - wrapped: A non-optional value.
/// - getIsNil: The condition that will result in `nil`.
init(
_ wrapped: Wrapped,
nilWhen getIsNil: (Wrapped) throws -> Bool
) rethrows {
self = try getIsNil(wrapped) ? nil : wrapped
}
}
zip( (2...)[maxArrayCount: 2], addresses ).forEach {
sqlite3_bind_int64(stmt, [=12=][0], .userID)
sqlite3_bind_int(stmt, [=12=][1], .deviceID)
}