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
200
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
290
ソフトウェアエンジニアとしてモナドを完全に理解する / make-perfect-sense-of-monad
jooohn
14
7.5k
ScalaのコンパイラにFizzBuzzを解いてもらう(Dottyもあるよ)
jooohn
1
950
Write stack safe non-tailrec recursive functions
jooohn
4
880
Introduction to Clean Architecture
jooohn
1
530
人類には早すぎる、謎の計算ロジックに立ち向かう / 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
730
Other Decks in Technology
See All in Technology
APIとはなにか
mikanichinose
0
100
新機能VPCリソースエンドポイント機能検証から得られた考察
duelist2020jp
0
230
PHPからGoへのマイグレーション for DMMアフィリエイト
yabakokobayashi
1
170
UI State設計とテスト方針
rmakiyama
2
650
AI時代のデータセンターネットワーク
lycorptech_jp
PRO
1
290
【re:Invent 2024 アプデ】 Prompt Routing の紹介
champ
0
150
MLOps の現場から
asei
7
650
統計データで2024年の クラウド・インフラ動向を眺める
ysknsid25
2
850
PHP ユーザのための OpenTelemetry 入門 / phpcon2024-opentelemetry
shin1x1
1
490
NilAway による静的解析で「10 億ドル」を節約する #kyotogo / Kyoto Go 56th
ytaka23
3
380
ずっと昔に Star をつけたはずの思い出せない GitHub リポジトリを見つけたい!
rokuosan
0
150
Oracle Cloud Infrastructure:2024年12月度サービス・アップデート
oracle4engineer
PRO
0
210
Featured
See All Featured
Typedesign – Prime Four
hannesfritz
40
2.4k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
95
17k
The Cost Of JavaScript in 2023
addyosmani
45
7k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5k
We Have a Design System, Now What?
morganepeng
51
7.3k
The Cult of Friendly URLs
andyhume
78
6.1k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.6k
Statistics for Hackers
jakevdp
796
220k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
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!