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
Why foldRight is beautiful
Search
Jun Tomioka
July 18, 2018
Technology
290
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Why foldRight is beautiful
Explains why "foldRight" is beautiful.
Jun Tomioka
July 18, 2018
More Decks by Jun Tomioka
See All by Jun Tomioka
Dotty で軽量な DI ライブラリをかいてみた
jooohn
1
380
ソフトウェアエンジニアとしてモナドを完全に理解する / make-perfect-sense-of-monad
jooohn
14
8k
ScalaのコンパイラにFizzBuzzを解いてもらう(Dottyもあるよ)
jooohn
1
1.2k
Write stack safe non-tailrec recursive functions
jooohn
4
1k
Introduction to Clean Architecture
jooohn
1
610
人類には早すぎる、謎の計算ロジックに立ち向かう / Strugle with the most complicated logic ever
jooohn
1
1.8k
Work at M3 USA
jooohn
0
1.5k
クラウド電子カルテを支えるテクノロジーの光と闇
jooohn
0
1.4k
怖くないCats
jooohn
0
930
Other Decks in Technology
See All in Technology
ウォーターフォール開発案件のPMとしてAI活用を模索している話
hatahata021
3
280
Swift&Xcodeのバージョンアップにまつわる怖かった思い出 / Scary Memories of Swift and Xcode Updates
bitkey
PRO
0
140
AI驚き屋発見器
yama3133
2
400
セキュリティ研修【MIXI 26新卒技術研修】
mixi_engineers
PRO
33
28k
事業価値と Engineering 2026年度版
recruitengineers
PRO
11
2.4k
【Google Cloud Next Tokyo'26】Gemini Enterprise と Oracle AI Database で実現する、業務データ活用を実現する AI エージェント実装
shisyu_gaku
0
190
新しい SLO が良い感じにハマっている話
z63d
4
2k
『三匹の子ぶた』から学ぶネットワークセキュリティの昔と今 / Network Security: Then and Now Through the Lens of The Three Little Pigs
nttcom
1
1.6k
今こそ聞きたいソフトウェア設計 ドメイン駆動設計再入門
masuda220
PRO
7
2.2k
[MIRU26] Open-Vocabulary Intention-Guided Object Detection in Diverse Scenes
keio_smilab
PRO
0
130
20260608_Codexの可能性_ノンプログラマー向け_大城追記
doradora09
PRO
0
750
論語・武士道・産業革命から見る かわるもの、かわらないもの
ichimichi
8
2.2k
Featured
See All Featured
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
Why Our Code Smells
bkeepers
PRO
340
58k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
450
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
240
A better future with KSS
kneath
240
18k
A Soul's Torment
seathinner
6
3.2k
Evolving SEO for Evolving Search Engines
ryanjones
0
250
Technical Leadership for Architectural Decision Making
baasie
3
460
How GitHub (no longer) Works
holman
316
150k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
260
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
330
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
230
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!