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
960
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
InsightX 会社説明資料/ Company deck
insightx
0
200
GCASアップデート(202508-202510)
techniczna
0
330
Oracle Database@Google Cloud:サービス概要のご紹介
oracle4engineer
PRO
0
430
AIの個性を理解し、指揮する
shoota
3
630
AI時代に必要なデータプラットフォームの要件とは by @Kazaneya_PR / 20251107
kazaneya
PRO
4
640
20251106 Offers DeepDive 知識を民主化!あらゆる業務のスピードと品質を 改善するためのドキュメント自動更新・活用術
masashiyokota
0
150
GPUをつかってベクトル検索を扱う手法のお話し~NVIDIA cuVSとCAGRA~
fshuhe
0
380
AIエージェントによる業務効率化への飽くなき挑戦-AWS上の実開発事例から学んだ効果、現実そしてギャップ-
nasuvitz
5
1.6k
Copilotの精度を上げる!カスタムプロンプト入門.pdf
ismk
1
150
어떤 개발자가 되고 싶은가?
arawn
1
430
Spec Driven Development入門/spec_driven_development_for_learners
hanhan1978
1
630
プロダクトエンジニアとしてのマインドセットの育み方 / How to improve product engineer mindset
saka2jp
1
180
Featured
See All Featured
Stop Working from a Prison Cell
hatefulcrawdad
272
21k
How to Think Like a Performance Engineer
csswizardry
27
2.2k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
9
950
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
36
6.1k
How to train your dragon (web standard)
notwaldorf
97
6.3k
Embracing the Ebb and Flow
colly
88
4.9k
Automating Front-end Workflow
addyosmani
1371
200k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
140
34k
Art, The Web, and Tiny UX
lynnandtonic
303
21k
GraphQLとの向き合い方2022年版
quramy
49
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!