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
980
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
全文検索+セマンティックランカー+LLMの自然文検索サ−ビスで得られた知見
segavvy
2
130
Tech Blogを書きやすい環境づくり
lycorptech_jp
PRO
1
260
Iceberg Meetup Japan #1 : Iceberg and Databricks
databricksjapan
0
110
2025-02-21 ゆるSRE勉強会 Enhancing SRE Using AI
yoshiiryo1
1
420
The Future of SEO: The Impact of AI on Search
badams
0
240
【Developers Summit 2025】プロダクトエンジニアから学ぶ、 ユーザーにより高い価値を届ける技術
niwatakeru
2
1.5k
N=1から解き明かすAWS ソリューションアーキテクトの魅力
kiiwami
0
140
30分でわかる『アジャイルデータモデリング』
hanon52_
10
2.9k
Classmethod AI Talks(CATs) #17 司会進行スライド(2025.02.19) / classmethod-ai-talks-aka-cats_moderator-slides_vol17_2025-02-19
shinyaa31
0
160
CZII - CryoET Object Identification 参加振り返り・解法共有
tattaka
0
420
「海外登壇」という 選択肢を与えるために 〜Gophers EX
logica0419
0
890
ビジネスモデリング道場 目的と背景
masuda220
PRO
9
660
Featured
See All Featured
Music & Morning Musume
bryan
46
6.3k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.3k
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
9
500
Documentation Writing (for coders)
carmenintech
67
4.6k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.2k
Automating Front-end Workflow
addyosmani
1368
200k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
4
420
Large-scale JavaScript Application Architecture
addyosmani
511
110k
The Cost Of JavaScript in 2023
addyosmani
47
7.3k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
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!