Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Why foldRight is beautiful
Search
Jun Tomioka
July 18, 2018
Technology
310
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
390
ソフトウェアエンジニアとしてモナドを完全に理解する / make-perfect-sense-of-monad
jooohn
14
8.1k
ScalaのコンパイラにFizzBuzzを解いてもらう(Dottyもあるよ)
jooohn
1
1.2k
Write stack safe non-tailrec recursive functions
jooohn
4
1k
Introduction to Clean Architecture
jooohn
1
620
人類には早すぎる、謎の計算ロジックに立ち向かう / 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
940
Other Decks in Technology
See All in Technology
AIに任せられるFlutterコードベースを作る 〜仕様をコードで表し、ハーネスで守る〜
myohei
1
580
【Oracle AI SPOTLIGHT ウェビナー】事例から見る!なぜOCIが選ばれたのか ― AI時代のクラウド戦略を紐解く ―
oracle4engineer
PRO
1
140
LLMの知識とAIエージェント
happysamurai294
0
130
Agent × Maestro で始める モバイル E2E 端末準備からテスト実行・結果確認まで
sansantech
PRO
0
240
クラウドエージェントの先駆け Devin。今も最先端を走る、他との違い
kinopeee
1
520
推論基盤と Scale-Up Interconnect の現状と制約から俯瞰する技術動向
markunet
2
670
AIで選択肢が広がった組織設計の理想と現実
konifar
16
7.2k
[General] 개발자로 해외 기업에서 일하기
skydoves
1
120
kintoneで在庫管理システムを作るときの要件定義・設計ポイント
kai0707
0
190
仕様駆動開発の、その先へ。決める力と確かめる仕組み
inariku
1
140
自律的なチームを作るためにTechPMがやらないこと
ny7760
0
270
バイブコーディングの特徴と「コケて直す」ループ
happysamurai294
0
130
Featured
See All Featured
sira's awesome portfolio website redesign presentation
elsirapls
0
480
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
2
2.2k
So, you think you're a good person
axbom
PRO
2
2.2k
Writing Fast Ruby
sferik
630
63k
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.6k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3.1k
The Cult of Friendly URLs
andyhume
79
7k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
47k
Designing Powerful Visuals for Engaging Learning
tmiket
1
620
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
290
Site-Speed That Sticks
csswizardry
13
1.6k
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!