eachPair
without AnySequence
extension Sequence where Self.SubSequence: Sequence {
func eachPair() -> Zip2Sequence<Self, Self.SubSequence> {
return zip(self, self.dropFirst())
}
}
(1...10).eachPair().forEach { print($0) }
eachPair
without AnySequence
extension Sequence where Self.SubSequence: Sequence {
func eachPair() -> Zip2Sequence<Self, Self.SubSequence> {
return zip(self, self.dropFirst())
}
}
(1...10).eachPair().forEach { print($0) }
// Argo
struct User {
let id: Int
let name: String
let email: String?
let role: Role
let companyName: String
let friends: [User]
}
class Animal {} | |
class Cat: Animal {} | |
let cats: Array<Cat> = [Cat()] | |
let animals: Array<Animal> = cats // OK | |
let cat: Optional<Cat> = .some(Cat()) | |
let animal: Optional<Animal> = cat // OK | |
struct Box<Value> { |
open class Animal | |
class Cat: Animal() | |
class Box<out Value> ( // `out` for covariance | |
val value: Value | |
) | |
fun main(args: Array<String>) { | |
val catBox: Box<Cat> = Box(Cat()) | |
val animalBox: Box<Animal> = catBox // OK |
extension CountableRange { | |
public func contains(_ range: CountableRange<Bound>) -> Bool { | |
return lowerBound <= range.lowerBound && range.upperBound <= upperBound | |
} | |
public func contains(_ range: CountableClosedRange<Bound>) -> Bool { | |
return lowerBound <= range.lowerBound && range.upperBound < upperBound | |
} | |
public func contains(_ range: Range<Bound>) -> Bool { |
val a: Int = 999 | |
val b: Int? = a | |
val c: Int? = a | |
println(b === c) // false |
// destroyed by `-Ounchecked` | |
import Foundation | |
let a: Int | |
switch arc4random() % 3 { | |
case 0: | |
print("0") | |
a = 111 | |
case 1: |
let array = [2, 3, 3, 4, 7, 2, 1, 4, 0, 9, 1, 2] | |
// array の要素の値ごとに集計 | |
// 2 が 3 個、 3 が 2 個 などを知りたい | |
var nToCount = [Int: Int]() | |
for n in array { | |
nToCount[n, default: 0] += 1 | |
} | |
// 結果を表示 |
let array = [2, 3, 3, 4, 7, 2, 1, 4, 0, 9, 1, 2] | |
// array の要素の値ごとに集計 | |
// 2 が 3 個、 3 が 2 個 などを知りたい | |
let nToCount: [Int: Int] = array.reduce(into: [:]) { nToCount, n in | |
nToCount[n, default: 0] += 1 | |
} | |
// 結果を表示 | |
for (n, count) in (nToCount.sorted { $0.key < $1.key }) { |
protocol CatProtocol { | |
var name: String { get set } | |
func nya() -> String | |
mutating func sleep() | |
func clone() -> Self | |
func cast<T>(to: T.Type) -> T? | |
} | |
class Cat : CatProtocol { | |
required init() {} |