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
240
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
350
ソフトウェアエンジニアとしてモナドを完全に理解する / make-perfect-sense-of-monad
jooohn
14
7.8k
ScalaのコンパイラにFizzBuzzを解いてもらう(Dottyもあるよ)
jooohn
1
1.1k
Write stack safe non-tailrec recursive functions
jooohn
4
950
Introduction to Clean Architecture
jooohn
1
560
人類には早すぎる、謎の計算ロジックに立ち向かう / Strugle with the most complicated logic ever
jooohn
1
1.7k
Work at M3 USA
jooohn
0
1.3k
クラウド電子カルテを支えるテクノロジーの光と闇
jooohn
0
1.3k
怖くないCats
jooohn
0
850
Other Decks in Technology
See All in Technology
自動テストのコストと向き合ってみた
qa
0
170
AI駆動開発を推進するためにサービス開発チームで 取り組んでいること
noayaoshiro
0
180
コンテキストエンジニアリングとは? 考え方と応用方法
findy_eventslides
4
900
Pure Goで体験するWasmの未来
askua
1
180
SOC2取得の全体像
shonansurvivors
1
390
20250929_QaaS_vol20
mura_shin
0
110
GA technologiesでのAI-Readyの取り組み@DataOps Night
yuto16
0
270
M5製品で作るポン置きセルラー対応カメラ
sayacom
0
150
生成AIとM5Stack / M5 Japan Tour 2025 Autumn 東京
you
PRO
0
220
o11yで育てる、強い内製開発組織
_awache
3
120
Findy Team+のSOC2取得までの道のり
rvirus0817
0
340
Trust as Infrastructure
bcantrill
0
340
Featured
See All Featured
Why You Should Never Use an ORM
jnunemaker
PRO
59
9.6k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.1k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
54
3k
Typedesign – Prime Four
hannesfritz
42
2.8k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.4k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
127
53k
Speed Design
sergeychernyshev
32
1.1k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
140
34k
Embracing the Ebb and Flow
colly
88
4.8k
Git: the NoSQL Database
bkeepers
PRO
431
66k
Code Review Best Practice
trishagee
72
19k
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!