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
0
260
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
360
ソフトウェアエンジニアとしてモナドを完全に理解する / make-perfect-sense-of-monad
jooohn
14
7.9k
ScalaのコンパイラにFizzBuzzを解いてもらう(Dottyもあるよ)
jooohn
1
1.1k
Write stack safe non-tailrec recursive functions
jooohn
4
980
Introduction to Clean Architecture
jooohn
1
580
人類には早すぎる、謎の計算ロジックに立ち向かう / Strugle with the most complicated logic ever
jooohn
1
1.7k
Work at M3 USA
jooohn
0
1.4k
クラウド電子カルテを支えるテクノロジーの光と闇
jooohn
0
1.3k
怖くないCats
jooohn
0
860
Other Decks in Technology
See All in Technology
Kiro を用いたペアプロのススメ
taikis
4
2.1k
なぜ あなたはそんなに re:Invent に行くのか?
miu_crescent
PRO
0
240
20251222_サンフランシスコサバイバル術
ponponmikankan
2
160
Cloud WAN MCP Serverから考える新しいネットワーク運用 / 20251228 Masaki Okuda
shift_evolve
PRO
0
130
ハッカソンから社内プロダクトへ AIエージェント「ko☆shi」開発で学んだ4つの重要要素
sonoda_mj
6
2k
SES向け、生成AI時代におけるエンジニアリングとセキュリティ
longbowxxx
0
270
2025年 山梨の技術コミュニティを振り返る
yuukis
0
140
モダンデータスタックの理想と現実の間で~1.3億人Vポイントデータ基盤の現在地とこれから~
taromatsui_cccmkhd
2
290
Keynoteから見るAWSの頭の中
nrinetcom
PRO
1
150
ソフトウェアエンジニアとAIエンジニアの役割分担についてのある事例
kworkdev
PRO
1
350
Oracle Database@Azure:サービス概要のご紹介
oracle4engineer
PRO
3
240
日本Rubyの会: これまでとこれから
snoozer05
PRO
6
250
Featured
See All Featured
Building Applications with DynamoDB
mza
96
6.9k
Facilitating Awesome Meetings
lara
57
6.7k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
2
74
How Software Deployment tools have changed in the past 20 years
geshan
0
30k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Embracing the Ebb and Flow
colly
88
4.9k
The agentic SEO stack - context over prompts
schlessera
0
570
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.1k
Stop Working from a Prison Cell
hatefulcrawdad
273
21k
Unsuck your backbone
ammeep
671
58k
New Earth Scene 8
popppiees
0
1.3k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
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!