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
func prop<Root, Value>( | |
_ kp: WritableKeyPath<Root, Value> | |
) | |
-> (@escaping (Value) -> Value) | |
-> (Root) -> Root { | |
return { transformPart in | |
return { root in | |
var root = root | |
root[keyPath: kp] = transformPart(root[keyPath: kp]) | |
return root |
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
struct User { | |
var name: String | |
var location: String | |
var age: Int | |
} | |
var user = User(name: "Blob", location: "NYC", age: 42) | |
\User.name | |
// WritableKeyPath<User, String> |
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
func mapFirst<A, B, C>( | |
_ f: @escaping (A) -> B) | |
-> ((A, C)) -> (B, C) { | |
return { pair in (f(pair.0), pair.1) } | |
} | |
let incrFirst2: ((Int, String)) -> (Int, String) = mapFirst(incr) | |
pair |> mapFirst(incr) |
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
// Currying: | |
// curry((A, B) _> C) -> ((A) -> ((B) -> C) | |
func map<A, B>( | |
_ f: @escaping (A) -> B) | |
-> ([A]) -> [B] { | |
return { xs in xs.map(f) } | |
} | |
map(incr) // (Array<Int>) -> Array<Int> |
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
precedencegroup FunctionApplication { | |
associativity: left | |
higherThan: AssignmentPrecedence | |
} | |
infix operator |>: FunctionApplication | |
func |> <A,B>(a: A, f: (A) -> B) -> B { | |
return f(a) | |
} |
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
private extension UIActivityType { | |
static let postToLINE = UIActivityType("jp.naver.line.Share") | |
static let gmail = UIActivityType("com.google.Gmail.ShareExtension") | |
static let postToSlack = UIActivityType("com.tinyspeck.chatlyio.share") | |
static let facebookMessanger = UIActivityType("com.facebook.Messenger.ShareExtension") | |
static let tweetbot4 = UIActivityType("com.tapbots.Tweetbot4.shareextension") | |
static let evernote = UIActivityType("com.evernote.iPhone.Evernote.EvernoteShare") | |
static let bear = UIActivityType("net.shinyfrog.bear-iOS.Bear-iPhone-Sharing-Extension") | |
} |
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
enum Hoge { | |
case oneValue(one: String) | |
case twoValue(one: String, two: String) | |
var description: String { | |
switch self { | |
case .oneValue(let one): return "\(one)" | |
case .twoValue(let one): return "\(one)" // `one` is treated as tuple, unexpectedly | |
} | |
} |
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 String { | |
func split(_ size: Int) -> [String] { | |
return stride(from: 0, to: self.count, by: size).map { i -> String in | |
let startIndex = self.index(self.startIndex, offsetBy: i) | |
let endIndex = self.index(startIndex, offsetBy: size, limitedBy: self.endIndex) ?? self.endIndex | |
return String(self[startIndex..<endIndex]) | |
} | |
} | |
} | |
print("γγγΎγγ¦γγγ§γ¨γγγγγΎγγδ»εΉ΄γγ©γγγγγγγι‘γγγΎγγ".split(5)) |
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
public struct Identifier { | |
fileprivate let _rawValue: RawValue | |
public typealias RawValue = Int | |
public init(_ rawValue: RawValue) { | |
self._rawValue = rawValue | |
} |
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 data: Data = """ | |
{ | |
"id": 1, | |
"url": "" | |
} | |
""".data(using: .utf8)! | |
struct Hoge: Codable { | |
let id: Int | |
private let jsonUrl: String? |