Skip to content

Instantly share code, notes, and snippets.

View rizumita's full-sized avatar
🏠
Working from home

Ryoichi Izumita rizumita

🏠
Working from home
View GitHub Profile
var i: Character?
test("Optional Binding") {
for _ in 0...repeatCount {
if let us = s {
i = us.first
} else {
i = "i".first
}
}
}
test("Optional Binding") {
for _ in 0...repeatCount {
let unrapped: String
if let tmp = s {
unrapped = tmp
} else {
unrapped = "default"
}
}
}
func runSpeedTests() {
let s: String? = "test"
let repeatCount = 10000000000
var i = 0
test("Optional Binding") {
for _ in 0...repeatCount {
if let _ = s { i += 1 }
}
}
func runSpeedTests() {
let s: String? = "test"
let repeatCount = 100000000
test("Optional Binding") {
for _ in 0...repeatCount {
if let _ = s {}
}
}
@rizumita
rizumita / DictionaryDecoding.swift
Created January 18, 2018 11:35
Decode [String:Any] property by Decodable in Swift
struct AnyCodingKeys: CodingKey {
var stringValue: String
var intValue: Int?
init?(stringValue: String) { self.stringValue = stringValue }
init?(intValue: Int) {
self.stringValue = String(intValue)
self.intValue = intValue
}
@rizumita
rizumita / CodePiece.swift
Created December 15, 2017 00:16
こういう実装はありなんだろうか。 #swift #CodePiece
struct Permission<Holder: PermissionHolder> {
let holder: Holder
}
protocol PermissionHolder {
var can: Permission<Self> { get }
}
extension PermissionHolder {
var can: Permission<Self> { return Permission(holder: self) }
let json = """
{
"id": 1,
"url":"",
"size":{ "width": 100, "height": 50 }
}
""".data(using: .utf8)!
protocol MyCodable: Codable {
static func decode(from decoder: Decoder) throws -> Self
func const<A, B>(_ value: A) -> (B) -> A {
return { _ in value }
}
let f_const: (String) -> () = { const(())(foo("bar")($0)) }
f_const("hoge") // 返値がVoidなのでWarningが発生しない
func foo(_ string: String) -> (String) -> String {
return { s in
let result = string + " " + s
print(result)
return result
}
}
let f: (String) -> String = foo("bar")
f("baz") // result of call is unused が発生
func operation(_ sub1: () -> (), _ sub2: () -> ()) {
sub1()
sub2()
}