Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Why foldRight is beautiful
Search
Jun Tomioka
July 18, 2018
Technology
0
190
Why foldRight is beautiful
Explains why "foldRight" is beautiful.
Jun Tomioka
July 18, 2018
Tweet
Share
More Decks by Jun Tomioka
See All by Jun Tomioka
Dotty で軽量な DI ライブラリをかいてみた
jooohn
1
280
ソフトウェアエンジニアとしてモナドを完全に理解する / make-perfect-sense-of-monad
jooohn
14
7.5k
ScalaのコンパイラにFizzBuzzを解いてもらう(Dottyもあるよ)
jooohn
1
940
Write stack safe non-tailrec recursive functions
jooohn
4
870
Introduction to Clean Architecture
jooohn
1
530
人類には早すぎる、謎の計算ロジックに立ち向かう / Strugle with the most complicated logic ever
jooohn
1
1.6k
Work at M3 USA
jooohn
0
1.2k
クラウド電子カルテを支えるテクノロジーの光と闇
jooohn
0
1.2k
怖くないCats
jooohn
0
730
Other Decks in Technology
See All in Technology
JAWS UG 青森(弘前)クラウド・AWS入門
hiragahh
0
170
セキュリティ運用って包括的にできていますか?SaaSを使って次のステップへ / Comprehensive Cyber Security Operations for Cloud Services Using SaaS
sakaitakeshi
0
270
50以上のマイクロサービスを支えるアプリケーションプラットフォームの設計・構築の後悔と進化 #CNDW2024 / regrets and evolution of application platform
toshi0607
5
610
専門領域に特化したチームの挑戦
leveragestech
0
200
そろそろOn-Callの通知音について考えてみよう (PagerDuty編)
tk3fftk
1
220
レガシーシステムへのDatadog APM導入奮闘記
mtakeya4062
0
130
【Oracle Cloud ウェビナー】【入門&再入門】はじめてのOracle Cloud Infrastructure [+最新情報]
oracle4engineer
PRO
2
110
Kubernetesを知る
logica0419
15
3.8k
GPUと画像生成AIが拓くマーケティングとビジネスの未来:次世代の可能性
iotcomjpadmin
0
180
B11-SharePoint サイトのストレージ管理を考えよう
maekawa123
0
110
SAP Community and Developer Update
sygyzmundovych
0
360
クラウドインフラ構築における.NETとその他IaCの比較
ymd65536
1
180
Featured
See All Featured
Why Our Code Smells
bkeepers
PRO
334
57k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
400
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
The Language of Interfaces
destraynor
154
24k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Optimizing for Happiness
mojombo
376
70k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
A designer walks into a library…
pauljervisheath
204
24k
Teambox: Starting and Learning
jrom
133
8.8k
Making the Leap to Tech Lead
cromwellryan
133
8.9k
Transcript
Why foldRight is beautiful Jun Tomioka (M3, inc.)
M3, Inc. Jun Tomioka Twitter: @jooohn1234 Github: jooohn Love: Our
very first baby Had great paternity leave!
None
foldLeft / foldRight
trait List[+A]
foldLeft[B](z: B)(op: (B, A) => B): B A A A
A A B A A A A B B OP ...
foldRight[B](z: B)(op: (A, B) => B): B A A A
A A B A A A A B B OP ...
So what’s the difference between them?
Suppose you define your own Linked List
foldLeft would be like this
foldRight would be like this
This naive foldRight can cause stack overflow
If so, why can foldRight be beautiful?
Let’s implement “map” with foldLeft
A A A A A List[B] A A A A
OP ... List[B] List[B]
Let’s implement “map” with foldLeft
foldRight
Let’s implement “map” with foldRight
A A A A A List[B] A A A A
List[B] List[B] OP ...
Why is it that natural to write “map” with foldRight?
Immutable recursive data structure
Bigger part holds smaller part
We must create them from smaller part to bigger part
This is the folding order of foldRight
foldRight folds items by its creation order
Why foldRight is beautiful • Immutable recursive data structure is
built from smaller part to bigger part • In this sense, foldRight folds items from smaller part to bigger part rather than from right to left ◦ Is quite natural to treat immutable recursive data structure
Thanks!