Skip to content

Instantly share code, notes, and snippets.

View d-date's full-sized avatar
🏠
Working from home

Daiki Matsudate d-date

🏠
Working from home
View GitHub Profile
public struct Identifier {
fileprivate let _rawValue: RawValue
public typealias RawValue = Int
public init(_ rawValue: RawValue) {
self._rawValue = rawValue
}
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))
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
}
}
@d-date
d-date / CodePiece.swift
Created September 3, 2018 19:07
Very comfortable to understand function composition! #CodePiece #tryswiftnyc
precedencegroup FunctionApplication {
associativity: left
higherThan: AssignmentPrecedence
}
infix operator |>: FunctionApplication
func |> <A,B>(a: A, f: (A) -> B) -> B {
return f(a)
}
@d-date
d-date / CodePiece.swift
Created September 3, 2018 19:48
talking about currying #CodePiece #tryswiftnyc
// 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>
@d-date
d-date / CodePiece.swift
Created September 3, 2018 20:32
How to map value in tuple #CodePiece #tryswiftnyc
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)
@d-date
d-date / CodePiece.swift
Created September 3, 2018 20:49
How to subscript value using KeyPath #CodePiece #tryswiftnyc
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>
@d-date
d-date / CodePiece.swift
Created September 3, 2018 21:16
When define prop as this, we can mutate property with function composition #CodePiece #tryswiftnyc
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
@d-date
d-date / CodePiece.swift
Created September 3, 2018 21:17
Also, we can user function composition to UI implementation #CodePiece #tryswiftnyc
import UIKit
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
NSMutableParagraphStyle() |> (prop(\.alignment)) { _ in .center }
func set<Root, Value>(
_ kp: WritableKeyPath<Root, Value>,
_ value: Value
)
@d-date
d-date / CodePiece.swift
Created September 3, 2018 21:26
We can apply style like this. Very simple implementation! #CodePiece #tryswiftnyc
let labelColorStyle = { set(\UILabel.textColor, $0) }
let primaryLabelStyle = labelColorStyle(UIColor.green)
let label = UILabel() |> primaryLabelStyle