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
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
350
ソフトウェアエンジニアとしてモナドを完全に理解する / make-perfect-sense-of-monad
jooohn
14
7.8k
ScalaのコンパイラにFizzBuzzを解いてもらう(Dottyもあるよ)
jooohn
1
1.1k
Write stack safe non-tailrec recursive functions
jooohn
4
950
Introduction to Clean Architecture
jooohn
1
570
人類には早すぎる、謎の計算ロジックに立ち向かう / 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
850
Other Decks in Technology
See All in Technology
速習AGENTS.md:5分で精度を上げる "3ブロック" テンプレ
ismk
0
280
Trust as Infrastructure
bcantrill
1
370
リーダーになったら未来を語れるようになろう/Speak the Future
sanogemaru
0
370
[Keynote] What do you need to know about DevEx in 2025
salaboy
0
150
extension 現場で使えるXcodeショートカット一覧
ktombow
0
220
【Kaigi on Rails 事後勉強会LT】MeはどうしてGirlsに? 私とRubyを繋いだRail(s)
joyfrommasara
0
220
Geospatialの世界最前線を探る [2025年版]
dayjournal
1
200
ガバメントクラウドの概要と自治体事例(名古屋市)
techniczna
2
220
AIAgentの限界を超え、 現場を動かすWorkflowAgentの設計と実践
miyatakoji
1
160
OCI Network Firewall 概要
oracle4engineer
PRO
2
7.8k
「使い方教えて」「事例教えて」じゃもう遅い! Microsoft 365 Copilot を触り倒そう!
taichinakamura
0
300
やる気のない自分との向き合い方/How to Deal with Your Unmotivated Self
sanogemaru
0
460
Featured
See All Featured
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.7k
The Straight Up "How To Draw Better" Workshop
denniskardys
237
140k
How to Ace a Technical Interview
jacobian
280
24k
Side Projects
sachag
455
43k
The Pragmatic Product Professional
lauravandoore
36
6.9k
Building a Modern Day E-commerce SEO Strategy
aleyda
43
7.7k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
34
6.1k
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
Keith and Marios Guide to Fast Websites
keithpitt
411
23k
Writing Fast Ruby
sferik
629
62k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
2.7k
Rails Girls Zürich Keynote
gr2m
95
14k
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!