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
260
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.4k
怖くないCats
jooohn
0
870
Other Decks in Technology
See All in Technology
純粋なイミュータブルモデルを設計してからイベントソーシングと組み合わせるDeciderの実践方法の紹介 /Introducing Decider Pattern with Event Sourcing
tomohisa
1
1.3k
会社紹介資料 / Sansan Company Profile
sansan33
PRO
13
400k
Introduction to Bill One Development Engineer
sansan33
PRO
0
350
kintone開発のプラットフォームエンジニアの紹介
cybozuinsideout
PRO
0
560
SwiftDataを覗き見る
akidon0000
0
290
Behind the Stream - How AbemaTV Engineers Build Video Apps at Scale
ygoto3
0
120
Web Intelligence and Visual Media Analytics
weblyzard
PRO
1
6.8k
Oracle Database@Google Cloud:サービス概要のご紹介
oracle4engineer
PRO
1
950
Sansan Engineering Unit 紹介資料
sansan33
PRO
1
3.7k
Databricks Free Edition講座 データエンジニアリング編
taka_aki
0
2.7k
AI アクセラレータチップ AWS Trainium/Inferentia に 今こそ入門
yoshimi0227
1
300
たかがボタン、されどボタン ~button要素から深ぼるボタンUIの定義について~ / BuriKaigi 2026
yamanoku
1
290
Featured
See All Featured
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
190
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
82
Writing Fast Ruby
sferik
630
62k
Exploring anti-patterns in Rails
aemeredith
2
230
VelocityConf: Rendering Performance Case Studies
addyosmani
333
24k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
99
From π to Pie charts
rasagy
0
120
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
0
3.4k
Producing Creativity
orderedlist
PRO
348
40k
Into the Great Unknown - MozCon
thekraken
40
2.2k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
71
Mind Mapping
helmedeiros
PRO
0
53
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!