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
270
0
Share
Why foldRight is beautiful
Explains why "foldRight" is beautiful.
Jun Tomioka
July 18, 2018
More Decks by Jun Tomioka
See All by Jun Tomioka
Dotty で軽量な DI ライブラリをかいてみた
jooohn
1
380
ソフトウェアエンジニアとしてモナドを完全に理解する / make-perfect-sense-of-monad
jooohn
14
8k
ScalaのコンパイラにFizzBuzzを解いてもらう(Dottyもあるよ)
jooohn
1
1.1k
Write stack safe non-tailrec recursive functions
jooohn
4
1k
Introduction to Clean Architecture
jooohn
1
590
人類には早すぎる、謎の計算ロジックに立ち向かう / Strugle with the most complicated logic ever
jooohn
1
1.8k
Work at M3 USA
jooohn
0
1.4k
クラウド電子カルテを支えるテクノロジーの光と闇
jooohn
0
1.4k
怖くないCats
jooohn
0
900
Other Decks in Technology
See All in Technology
最初の一歩を踏み出せなかった私が、誰かの背中を押したいと思うようになるまで / give someone a push
mii3king
0
160
60分で学ぶ最新Webフロントエンド
mizdra
PRO
34
18k
AWS Agent Registry の基礎・概要を理解する/aws-agent-registry-intro
ren8k
3
360
2026年、知っておくべき最新 サーバレスTips10選/serverless-10-tips
slsops
13
5.1k
AIエージェントの権限管理 2: データ基盤の Fine grained access control 編
ren8k
0
120
LLM時代の検索アーキテクチャと技術的意思決定
shibuiwilliam
3
1k
CloudSec JP #005 後締め ~ソフトウェアサプライチェーン攻撃から開発者のシークレットを守る~
lhazy
0
230
AgentCore×VPCでの設計パターンn選と勘所
har1101
3
250
Standards et agents IA : un tour d’horizon de MCP, A2A, ADK et plus encore
glaforge
0
140
社内エンジニア勉強会の醍醐味と苦しみ/tamadev
nishiuma
0
150
Amazon S3 Filesについて
yama3133
2
200
AIを共同作業者にして書籍を執筆する方法 / How to Write a Book with AI as a Co-Creator
ama_ch
2
130
Featured
See All Featured
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.4k
A Modern Web Designer's Workflow
chriscoyier
698
190k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.9k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
0
200
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
1
180
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.2k
Side Projects
sachag
455
43k
Reality Check: Gamification 10 Years Later
codingconduct
0
2.1k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
160
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.8k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Context Engineering - Making Every Token Count
addyosmani
9
820
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!