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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
370
ソフトウェアエンジニアとしてモナドを完全に理解する / 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
890
Other Decks in Technology
See All in Technology
CloudFrontのHost Header転送設定でパケットの中身はどう変わるのか?
nagisa53
1
230
昔話で振り返るAWSの歩み ~S3誕生から20年、クラウドはどう進化したのか~
nrinetcom
PRO
0
120
ハーネスエンジニアリング×AI適応開発
aictokamiya
1
920
RGBに陥らないために -プロダクトの価値を届けるまで-
righttouch
PRO
0
130
タスク管理も1on1も、もう「管理」じゃない - KiroとBedrock AgentCoreで変わった“判断の仕事”
yusukeshimizu
0
150
「できない」のアウトプット 同人誌『精神を壊してからの』シリーズ出版を 通して得られたこと
comi190327
3
440
DMBOKを使ってレバレジーズのデータマネジメントを評価した
leveragestech
0
490
OCI技術資料 : 証明書サービス概要
ocise
1
7.2k
LLMに何を任せ、何を任せないか
cap120
11
6.7k
不確実性と戦いながら見積もりを作成するプロセス/mitsumori-process
hirodragon112
1
160
GitHub Advanced Security × Defender for Cloudで開発とSecOpsのサイロを超える: コードとクラウドをつなぐ、開発プラットフォームのセキュリティ
yuriemori
1
120
FlutterでPiP再生を実装した話
s9a17
0
240
Featured
See All Featured
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
700
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
190
KATA
mclloyd
PRO
35
15k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
100
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
320
Paper Plane
katiecoart
PRO
0
48k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.4k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
250
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.9k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
400
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!