Skip to content

Instantly share code, notes, and snippets.

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
}
// 結果を表示
// destroyed by `-Ounchecked`
import Foundation
let a: Int
switch arc4random() % 3 {
case 0:
print("0")
a = 111
case 1:
val a: Int = 999
val b: Int? = a
val c: Int? = a
println(b === c) // false
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 {
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
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> {
// Argo
struct User {
  let id: Int
  let name: String
  let email: String?
  let role: Role
  let companyName: String
  let friends: [User]
}
@koher
koher / each-pair-without-any-sequence.md
Created March 3, 2017 03:12
`eachPair` without `AnySequence`

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) }
@koher
koher / README.md
Last active February 26, 2017 14:39
SwiftScript Translation Catalog

SwiftScript Translation Catalog (Draft)

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.

Supporting Library

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

throws : async = try : await = catch : wait = rethrows : reasync = Result<T> : Promise<T>

throws : async = try : await

// Asynchronous selection by an action sheet
func actionSheet(itemes: [String]) async -> Int { ... }

// `await` must be in an `async` function
func foo() async {