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
260
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
360
ソフトウェアエンジニアとしてモナドを完全に理解する / make-perfect-sense-of-monad
jooohn
14
7.9k
ScalaのコンパイラにFizzBuzzを解いてもらう(Dottyもあるよ)
jooohn
1
1.1k
Write stack safe non-tailrec recursive functions
jooohn
4
980
Introduction to Clean Architecture
jooohn
1
580
人類には早すぎる、謎の計算ロジックに立ち向かう / Strugle with the most complicated logic ever
jooohn
1
1.7k
Work at M3 USA
jooohn
0
1.4k
クラウド電子カルテを支えるテクノロジーの光と闇
jooohn
0
1.4k
怖くないCats
jooohn
0
870
Other Decks in Technology
See All in Technology
Regional_NAT_Gatewayについて_basicとの違い_試した内容スケールアウト_インについて_IPv6_dual_networkでの使い分けなど.pdf
cloudevcode
1
160
3リポジトリーを2ヶ月でモノレポ化した話 / How I turned 3 repositories into a monorepo in 2 months
kubode
0
120
Agentic Coding 実践ワークショップ
watany
41
27k
[Iceberg Meetup #4] ゼロからはじめる: Apache Icebergとはなにか? / Apache Iceberg for Beginners
databricksjapan
0
500
メルカリのAI活用を支えるAIセキュリティ
s3h
7
4.5k
Tebiki Engineering Team Deck
tebiki
0
23k
re:Inventで出たインフラエンジニアが嬉しかったアップデート
nagisa53
4
210
Lambda Durable FunctionsでStep Functionsの代わりはできるのかを試してみた
smt7174
2
140
「全社導入」は結果。1人の熱狂が組織に伝播したmikanのn8n活用
sota_mikami
0
510
オープンウェイトのLLMリランカーを契約書で評価する / searchtechjp
sansan_randd
0
170
BPaaSオペレーション・kubell社内 n8n活用による効率化検証事例紹介
kentarofujii
0
310
エンジニアとして長く走るために気づいた2つのこと_大賀愛一郎
nanaism
1
250
Featured
See All Featured
Learning to Love Humans: Emotional Interface Design
aarron
275
41k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.1k
Everyday Curiosity
cassininazir
0
120
sira's awesome portfolio website redesign presentation
elsirapls
0
130
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
1
83
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
83
Google's AI Overviews - The New Search
badams
0
890
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
130
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.6k
How to build a perfect <img>
jonoalderson
1
4.9k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.8k
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!