// Argo
struct User {
let id: Int
let name: String
let email: String?
let role: Role
let companyName: String
let friends: [User]
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
// 結果を表示 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// destroyed by `-Ounchecked` | |
import Foundation | |
let a: Int | |
switch arc4random() % 3 { | |
case 0: | |
print("0") | |
a = 111 | |
case 1: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val a: Int = 999 | |
val b: Int? = a | |
val c: Int? = a | |
println(b === c) // false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { |
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) }
This is a catalog of translations by SwiftScript, a transpiler from Swift to JavaScript, for a hackathon. It supports only a subset of Swift and has some incompatibilities.
SwiftScript tries to translate Swift codes to JS codes using builtin JS APIs. However some needs a supporting library to simulate behaviors of Swift. A typical example is ?.
.
If