Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Lens introduction
Search
to4iki
February 18, 2018
Programming
0
530
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
Claude Code の活用事例
to4iki
0
130
Swift Concurrencyを利用したUIViewController表示の排他制御の実装
to4iki
0
3.5k
ケースに応じたUICollectionViewのレイアウト実装パターン
to4iki
1
5.1k
ビューインプレッションの計測方法
to4iki
1
1.1k
秘伝の `gitconfig`
to4iki
1
460
Abema iOS Architecture
to4iki
12
3.5k
timetable-bot
to4iki
0
15k
BLoC Pattern Introduction with Swift
to4iki
2
1.3k
nel
to4iki
0
160
Other Decks in Programming
See All in Programming
AIエージェントを活かすPM術 AI駆動開発の現場から
gyuta
0
420
手が足りない!兼業データエンジニアに必要だったアーキテクチャと立ち回り
zinkosuke
0
700
複数人でのCLI/Infrastructure as Codeの暮らしを良くする
shmokmt
5
2.3k
大体よく分かるscala.collection.immutable.HashMap ~ Compressed Hash-Array Mapped Prefix-tree (CHAMP) ~
matsu_chara
2
220
AIコードレビューがチームの"文脈"を 読めるようになるまで
marutaku
0
350
Canon EOS R50 V と R5 Mark II 購入でみえてきた最近のデジイチ VR180 事情、そして VR180 静止画に活路を見出すまで
karad
0
110
Rediscover the Console - SymfonyCon Amsterdam 2025
chalasr
2
160
C-Shared Buildで突破するAI Agent バックテストの壁
po3rin
0
390
251126 TestState APIってなんだっけ?Step Functionsテストどう変わる?
east_takumi
0
320
Building AI Agents with TypeScript #TSKaigiHokuriku
izumin5210
6
1.3k
Github Copilotのチャット履歴ビューワーを作りました~WPF、dotnet10もあるよ~ #clrh111
katsuyuzu
0
110
著者と進める!『AIと個人開発したくなったらまずCursorで要件定義だ!』
yasunacoffee
0
140
Featured
See All Featured
The Pragmatic Product Professional
lauravandoore
37
7.1k
Side Projects
sachag
455
43k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Site-Speed That Sticks
csswizardry
13
1k
Become a Pro
speakerdeck
PRO
31
5.7k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
36
6.2k
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
Principles of Awesome APIs and How to Build Them.
keavy
127
17k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
BBQ
matthewcrist
89
9.9k
Designing Experiences People Love
moore
143
24k
How GitHub (no longer) Works
holman
316
140k
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