$30 off During Our Annual Pro Sale. View Details »
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
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
チーリンについて
hirotomotaguchi
6
2.1k
コンテキスト情報を活用し個社最適化されたAI Agentを実現する4つのポイント
kworkdev
PRO
1
1.6k
マイクロサービスへの5年間 ぶっちゃけ何をしてどうなったか
joker1007
16
6.6k
Lambdaの常識はどう変わる?!re:Invent 2025 before after
iwatatomoya
1
630
学習データって増やせばいいんですか?
ftakahashi
2
500
文字列の並び順 / Unicode Collation
tmtms
3
610
【U/day Tokyo 2025】Cygames流 最新スマートフォンゲームの技術設計 〜『Shadowverse: Worlds Beyond』におけるアーキテクチャ再設計の挑戦~
cygames
PRO
2
730
ウェルネス SaaS × AI、1,000万ユーザーを支える 業界特化 AI プロダクト開発への道のり
hacomono
PRO
0
150
AI 駆動開発勉強会 フロントエンド支部 #1 w/あずもば
1ftseabass
PRO
0
400
会社紹介資料 / Sansan Company Profile
sansan33
PRO
11
390k
ExpoのインダストリーブースでみたAWSが見せる製造業の未来
hamadakoji
0
150
re:Invent 2025 ~何をする者であり、どこへいくのか~
tetutetu214
0
220
Featured
See All Featured
Rebuilding a faster, lazier Slack
samanthasiow
85
9.3k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
63
Utilizing Notion as your number one productivity tool
mfonobong
2
180
Producing Creativity
orderedlist
PRO
348
40k
Code Review Best Practice
trishagee
74
19k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
110
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.3k
HDC tutorial
michielstock
0
250
Faster Mobile Websites
deanohume
310
31k
Navigating Team Friction
lara
191
16k
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!