Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Lens introduction
Search
to4iki
February 18, 2018
Programming
0
510
Lens introduction
in `swift` implementation
see:
https://github.com/to4iki/LensKit
to4iki
February 18, 2018
Tweet
Share
More Decks by to4iki
See All by to4iki
suspend-view-controller-sample
to4iki
0
3.2k
ケースに応じたUICollectionViewのレイアウト実装パターン
to4iki
1
4.7k
ビューインプレッションの計測方法
to4iki
1
1.1k
秘伝の `gitconfig`
to4iki
1
430
Abema iOS Architecture
to4iki
12
3.4k
timetable-bot
to4iki
0
14k
BLoC Pattern Introduction with Swift
to4iki
2
1.3k
nel
to4iki
0
140
[iOS] ビデオチームのスモールスクラム
to4iki
0
65
Other Decks in Programming
See All in Programming
大LLM時代にこの先生きのこるには-ITエンジニア編
fumiyakume
8
3.3k
Cline with Amazon Bedrockで爆速開発体験ハンズオン/ 株式会社ブリューアス登壇資料
mhan
0
110
By the way Google Cloud Next 2025に行ってみてどうだった
ymd65536
0
110
GitHub Copilot for Azureを使い倒したい
ymd65536
1
310
Cursorを活用したAIプログラミングについて 入門
rect
0
160
SwiftDataのカスタムデータストアを試してみた
1mash0
0
140
Golangci-lint v2爆誕: 君たちはどうすべきか
logica0419
1
230
Boost Your Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
760
Fiber Scheduler vs. General-Purpose Parallel Client
hayaokimura
1
290
flutter_kaigi_mini_4.pdf
nobu74658
0
140
Deoptimization: How YJIT Speeds Up Ruby by Slowing Down / RubyKaigi 2025
k0kubun
1
1.9k
Improve my own Ruby
sisshiki1969
0
100
Featured
See All Featured
Designing for humans not robots
tammielis
253
25k
The Power of CSS Pseudo Elements
geoffreycrofte
75
5.8k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
60k
Six Lessons from altMBA
skipperchong
28
3.7k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
The Pragmatic Product Professional
lauravandoore
33
6.6k
Rebuilding a faster, lazier Slack
samanthasiow
81
9k
RailsConf 2023
tenderlove
30
1.1k
Navigating Team Friction
lara
185
15k
Statistics for Hackers
jakevdp
798
220k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.2k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Transcript
Lens Shinjuku.LT#17 @to4iki 1
Me • Takezawa • • ! • ☕ •
♨ 2
what Lens? 3
Def. / Feature • getter/setterͷநԽɾম͖͠(൚༻Խ) • ༷ʑͳσʔλΛڞ௨ͷI/FͰૢ࡞Ͱ͖Δ • ಛఆͷݴޠʹґଘ͠ͳ͍֓೦/ܕ •
functional • ෆมߏ / ߹Մೳ • Lens Laws 4
ekmett/lens 5
• Monocle.Lens - elm-monocle • julien-truffaut/Monocle 6
https://github.com/julien-truffaut/Monocle/blob/master/image/class- diagram.png 7
getter setter 8
getter • ΦϒδΣΫτS͔ΒAΛऔΓग़͢ • get: (S) -> A setter •
ΦϒδΣΫτSͷҙͷΛAʹมߋ͢Δ • set: (S, A) -> S == set: (S) -> A -> S 9
͜ΕΒͷ͍͢͝ getter/setter1Λ ҙͷΦϒδΣΫτͷ ҙͷϝϯόʹద༻͢Δͷ͕ Lens 1 https://qiita.com/to4iki/items/f0cc28e1102cf53be85d 10
࣮ͯ͠ΈΔ 11
struct Lens<S, T> { private let _get: (S) -> T
private let _set: (S, T) -> S init(get: @escaping (S) -> T, set: @escaping (S, T) -> S) func get(_ source: S) -> T func set(_ source: S, value: T) -> S } 12
͜Μ͚ͩ 13
struct User { let name: String } let user =
User(name: "hoge") let nameLens = Lens<User, String>( get: { $0.name }, set: { User(name: $1) } ) nameLens.get(user) // "hoge" nameLens.set("fuga") // User(name:"fuga)" 14
Usecase? 15
ωετͨ͠σʔλͷ͋ΔҰ෦ ͚ͩΛมߋ͍ͨ͠ struct Street { let name: String } struct
Address { let street: Street } struct Company { let address: Address } struct Employee { let company: Company } let employee = Employee(company: Company(address: Address(street: Street(name: "street")))) 16
! let updatedEmployee = Employee(company: Company(address: Address(street: Street(name: employee.company.address.street.name.capitalizedString) )
) ) 17
Lens߹Λ͍ɺ ෆมߏΛอͪͳ͕Β ΞΫηεग़དྷΔ 18
struct Lens<S, T> { ... func compose<U>(_ other: Lens<T, U>)
-> Lens<S, U> { return Lens<S, U>( get: { (source: S) -> U in other.get(self.get((source))) }, set: { (source: S, value: U) -> S in self.set(source, value: other.set(self.get(source), value: value)) } ) } } 19
(operatorఆٛ͠ͱ͘) /// Left-to-Right Composition infix operator >>> : MultiplicationPrecedence func
>>> <S, T, U>(lhs: Lens<S, T>, rhs: Lens<T, U>) -> Lens<S, U> { return lhs.compose(rhs) } 20
Demo 21
! let nameLens = Lens<Street, String>(get: { $0.name }, set:
{ Street(name: $1) } ) let streetLens = Lens<Address, Street> ... let addressLens = Lens<Company, Address> ... let companyLens = Lens<Employee, Company> ... let lens: Lens<Employee, String> = companyLens >>> addressLens >>> streetLens >>> nameLens let employee = Employee(company: Company(address: Address(street: Street(name: "street")))) let updatedEmployee = lens.modify(employee) { $0.capitalized } // Employee(Company(Address(Street(name: "Street") 22
https://github.com/ to4iki/LensKit 23
Conclusion • Lens ͍͢͝getter/setter • ෆมߏΛอͪͳ͕Β • ਂ͍ߏͰ •
ʹΞΫηεग़དྷΔ • Ϣʔεέʔεਖ਼͍͠ • ςετ࣌ͷαϙʔτͱͯ͠͏? 24
See also • ࠓ͔Β࢝ΊΔ Lens/Prism 25