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
250
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
970
Introduction to Clean Architecture
jooohn
1
570
人類には早すぎる、謎の計算ロジックに立ち向かう / 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
ブロックテーマとこれからの WordPress サイト制作 / Toyama WordPress Meetup Vol.81
torounit
0
530
re:Invent 2025 ~何をする者であり、どこへいくのか~
tetutetu214
0
180
ChatGPTで論⽂は読めるのか
spatial_ai_network
0
860
プロダクトマネージャーが押さえておくべき、ソフトウェア資産とAIエージェント投資効果 / pmconf2025
i35_267
2
590
AWS Trainium3 をちょっと身近に感じたい
bigmuramura
1
130
Uncertainty in the LLM era - Science, more than scale
gaelvaroquaux
0
820
WordPress は終わったのか ~今のWordPress の制作手法ってなにがあんねん?~ / Is WordPress Over? How We Build with WordPress Today
tbshiki
1
540
20251209_WAKECareer_生成AIを活用した設計・開発プロセス
syobochim
5
1.4k
Microsoft Agent 365 を 30 分でなんとなく理解する
skmkzyk
1
1k
学習データって増やせばいいんですか?
ftakahashi
2
280
最近のLinux普段づかいWaylandデスクトップ元年
penguin2716
1
680
新 Security HubがついにGA!仕組みや料金を深堀り #AWSreInvent #regrowth / AWS Security Hub Advanced GA
masahirokawahara
1
1.6k
Featured
See All Featured
The Art of Programming - Codeland 2020
erikaheidi
56
14k
Git: the NoSQL Database
bkeepers
PRO
432
66k
Art, The Web, and Tiny UX
lynnandtonic
303
21k
Raft: Consensus for Rubyists
vanstee
141
7.2k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Typedesign – Prime Four
hannesfritz
42
2.9k
Visualization
eitanlees
150
16k
Speed Design
sergeychernyshev
33
1.4k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.3k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.1k
Practical Orchestrator
shlominoach
190
11k
Rebuilding a faster, lazier Slack
samanthasiow
84
9.3k
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!