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
210
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
300
ソフトウェアエンジニアとしてモナドを完全に理解する / make-perfect-sense-of-monad
jooohn
14
7.6k
ScalaのコンパイラにFizzBuzzを解いてもらう(Dottyもあるよ)
jooohn
1
970
Write stack safe non-tailrec recursive functions
jooohn
4
900
Introduction to Clean Architecture
jooohn
1
540
人類には早すぎる、謎の計算ロジックに立ち向かう / Strugle with the most complicated logic ever
jooohn
1
1.6k
Work at M3 USA
jooohn
0
1.2k
クラウド電子カルテを支えるテクノロジーの光と闇
jooohn
0
1.2k
怖くないCats
jooohn
0
760
Other Decks in Technology
See All in Technology
30分でわかる『アジャイルデータモデリング』
hanon52_
9
2.7k
君も受託系GISエンジニアにならないか
sudataka
2
430
The Future of SEO: The Impact of AI on Search
badams
0
190
Developers Summit 2025 浅野卓也(13-B-7 LegalOn Technologies)
legalontechnologies
PRO
0
710
アジャイル開発とスクラム
araihara
0
170
データの品質が低いと何が困るのか
kzykmyzw
6
1.1k
MC906491 を見据えた Microsoft Entra Connect アップグレード対応
tamaiyutaro
1
540
関東Kaggler会LT: 人狼コンペとLLM量子化について
nejumi
3
580
2024.02.19 W&B AIエージェントLT会 / AIエージェントが業務を代行するための計画と実行 / Algomatic 宮脇
smiyawaki0820
13
3.3k
抽象化をするということ - 具体と抽象の往復を身につける / Abstraction and concretization
soudai
16
3.6k
Platform Engineeringは自由のめまい
nwiizo
4
2.1k
AndroidデバイスにFTPサーバを建立する
e10dokup
0
250
Featured
See All Featured
Writing Fast Ruby
sferik
628
61k
BBQ
matthewcrist
87
9.5k
Rails Girls Zürich Keynote
gr2m
94
13k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Raft: Consensus for Rubyists
vanstee
137
6.8k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.7k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
40
2k
Large-scale JavaScript Application Architecture
addyosmani
511
110k
A Philosophy of Restraint
colly
203
16k
Producing Creativity
orderedlist
PRO
344
39k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
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!