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
490
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
3k
ケースに応じたUICollectionViewのレイアウト実装パターン
to4iki
1
4.5k
ビューインプレッションの計測方法
to4iki
1
1k
秘伝の `gitconfig`
to4iki
1
410
Abema iOS Architecture
to4iki
12
3.3k
timetable-bot
to4iki
0
14k
BLoC Pattern Introduction with Swift
to4iki
2
1.2k
nel
to4iki
0
140
[iOS] ビデオチームのスモールスクラム
to4iki
0
54
Other Decks in Programming
See All in Programming
Flutterを言い訳にしない!アプリの使い心地改善テクニック5選🔥
kno3a87
1
200
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
EMになってからチームの成果を最大化するために取り組んだこと/ Maximize team performance as EM
nashiusagi
0
100
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
200
初めてDefinitelyTypedにPRを出した話
syumai
0
420
[Do iOS '24] Ship your app on a Friday...and enjoy your weekend!
polpielladev
0
110
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.2k
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.3k
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
300
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
340
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
9
3.3k
CSC509 Lecture 13
javiergs
PRO
0
110
Featured
See All Featured
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
Statistics for Hackers
jakevdp
796
220k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Building Your Own Lightsaber
phodgson
103
6.1k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
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