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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
to4iki
February 18, 2018
Programming
560
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Lens introduction
in `swift` implementation
see:
https://github.com/to4iki/LensKit
to4iki
February 18, 2018
More Decks by to4iki
See All by to4iki
Claude Code の活用事例
to4iki
0
180
Swift Concurrencyを利用したUIViewController表示の排他制御の実装
to4iki
0
3.7k
ケースに応じたUICollectionViewのレイアウト実装パターン
to4iki
1
5.6k
ビューインプレッションの計測方法
to4iki
1
1.1k
秘伝の `gitconfig`
to4iki
1
470
Abema iOS Architecture
to4iki
12
3.6k
timetable-bot
to4iki
0
15k
BLoC Pattern Introduction with Swift
to4iki
2
1.4k
nel
to4iki
0
190
Other Decks in Programming
See All in Programming
New "Type" system on PicoRuby
pocke
1
830
Lessons from Spec-Driven Development
simas
PRO
0
170
AIチームを指揮するOSS「TAKT」活用術 / How to Use “TAKT,” an OSS Tool for Orchestrating AI Teams
nrslib
6
880
The Arts and Crafts of Work in the AI Era — Toward Mastery in Software Development
kuranuki
1
750
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
210
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
120
Technical Debt: Understanding it Rightly, Engaging it Rightly #LaravelLiveJP
shogogg
0
220
キャリア迷子上等 ─ "ない道"は自分で作ればいい
16bitidol
3
2k
Dataformのリポジトリを立ち上げるときにまずやること / dataform-day0-2026
snhryt
0
150
The NotImplementedError Problem in Ruby
koic
1
710
代数的データ型って何が嬉しいの? #frontend_phpcon_do
kajitack
8
3.3k
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
250
Featured
See All Featured
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
250
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.5k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
62
54k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.3k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
160
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
570
Joys of Absence: A Defence of Solitary Play
codingconduct
1
390
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
170
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
160
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