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
Anonymous Recursion in C++
Search
na-o-ys
May 19, 2016
Programming
0
400
Anonymous Recursion in C++
C++で無名再帰を行う
source code:
https://gist.github.com/na-o-ys/56434282baa5705efe7e3bd05e3f04d1
na-o-ys
May 19, 2016
Tweet
Share
More Decks by na-o-ys
See All by na-o-ys
IoTと監視
naoys
1
710
RubyとJIT
naoys
0
140
将棋盤を画像認識したかった
naoys
0
1.5k
Rust で乗り換え案内
naoys
0
610
疎行列と Jaccard 類似度の高速計算
naoys
1
560
有理数集合の濃度
naoys
2
110
YARVの最適化について調べた
naoys
0
110
転職会議サービスのAWS移行記録
naoys
0
51
入門AlphaGo
naoys
5
3.7k
Other Decks in Programming
See All in Programming
クリエイティブコーディングとRuby学習 / Creative Coding and Learning Ruby
chobishiba
0
3.9k
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
770
ソフトウェアの振る舞いに着目し 複雑な要件の開発に立ち向かう
rickyban
0
890
ドメインイベント増えすぎ問題
h0r15h0
2
300
Symfony Mapper Component
soyuka
2
730
命名をリントする
chiroruxx
1
400
創造的活動から切り拓く新たなキャリア 好きから始めてみる夜勤オペレーターからSREへの転身
yjszk
1
130
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
140
これが俺の”自分戦略” プロセスを楽しんでいこう! - Developers CAREER Boost 2024
niftycorp
PRO
0
190
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
270
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
94
13k
A Tale of Four Properties
chriscoyier
157
23k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
Product Roadmaps are Hard
iamctodd
PRO
49
11k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
Embracing the Ebb and Flow
colly
84
4.5k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Code Reviewing Like a Champion
maltzj
520
39k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Transcript
Anonymous Recursion in C++ 0x64ޠ ୈ08 “Functional Programming” @na_o_ys
Anonymous Recursion (ແ໊࠶ؼ)
ී௨ͷ࠶ؼ (ϑΟϘφον) #include <iostream> int fib(int n) { return n
<= 1 ? 1 : fib(n-1) + fib(n-2); } int main(int argc, char const* argv[]) { std::cout << fib(4) << std::endl; return 0; }
Έ #include <iostream> int fib(int n) { return n <=
1 ? 1 : fib(n-1) + fib(n-2); } int main(int argc, char const* argv[]) { std::cout << fib(4) << std::endl; return 0; } ͜͜Ͱ͔͠Θͳ͍ͷʹ ͍͍ͪͪؔఆٛ͠ͳ͖Ό͍͚ͳ͍
ղܾҊ: ϥϜμؔΛଈ࣌ධՁ͢Δ
ϥϜμͰॻ͍ͯΈΔ #include <iostream> int main(int argc, char const* argv[]) {
std::cout << ([](int n) -> int { return n <= 1 ? 1 : fib(n-1) + fib(n-2); })(4) << std::endl; return 0; } ※ίϯύΠϧͰ͖·ͤΜ
#include <iostream> int main(int argc, char const* argv[]) {
std::cout << ([](int n) -> int { return n <= 1 ? 1 : fib(n-1) + fib(n-2); })(4) << std::endl; return 0; } ໊લ͕ແ͍ͷͰࣗࣗΛݺͳ͍ = ࠶ؼͰ͖ͳ͍ʂ
ແ໊࠶ؼͱ • ϥϜμؔʹ͓͍ͯ࠶ؼΛߦ͏͜ͱ • ෆಈίϯϏωʔλΛ͏ํ๏͕Ұൠత • 1. ҾʹࣗࣗΛड͚औΔϥϜμΛॻ͍ͯ • 2.
ෆಈίϯϏωʔλʹ৯ΘͤΔ
1. ҾʹࣗࣗΛड͚औΔ ϥϜμΛॻ͍ͯ [](auto self, int n) -> int {
return n <= 1 ? 1 : self(n-1) + self(n-2); } ࣗࣗ ※Ҿͷܕʹ auto Λࢦఆͨ͠ϥϜμɺςϯϓϨʔτؔΦϒδΣΫτͱͯ͠ ѻΘΕΔ(generic lambda: C++14 ͰՃ)
2. ෆಈίϯϏωʔλʹ৯ΘͤΔ // 不動点コンビネータを作る template<typename Func> struct fixed { Func
f; template<typename... Args> auto operator()(Args... args) { return f(fix(f), args...); } }; template<typename Func> fixed<Func> fix(Func f) { return fixed<Func> { f }; } ~~~~~~ // 食わせる fix( [](auto self, int n) -> int { return n <= 1 ? 1 : self(n-1) + self(n-2); } )
ಈ͘ʂ #include <iostream> // 省略 int main(int argc, char const*
argv[]) { std::cout << (fix( [](auto self, int n) -> int { return n <= 1 ? 1 : self(n-1) + self(n-2); } ))(4) << std::endl; return 0; } $ g++-6 -std=c++1y sample.cpp $ ./a.out > 5
GCD (࠷খެഒ) ͱ͔ • ࠶ؼͰॻ͚ҰߦͰࡁΉͭ #include <iostream> // 省略 int
main(int argc, char const* argv[]) { std::cout << (fix( [](auto self, int a, int b) -> int { return b == 0 ? a : self(b, a % b); } ))(15, 9) << std::endl; return 0; } $ g++-6 -std=c++1y sample.cpp $ ./a.out > 3
͓·͚: ෆಈίϯϏωʔλ
ෆಈίϯϏωʔλ • ఆٛ: fix(f)(args) = f(fix(f), args) • ্ࣜΛͣͬͱோΊͯΔͱɺಈ͘ཧ༝͕ݟ͑ͯ ͖·͢
• ώϯτ • ӈล f: ఆٛͨ͠ϥϜμࣜ • f(self, args) • ࠨล fix(f): argsͷΈΛड͚औΔؔ • = self template<typename Func> struct fixed { Func f; template<typename... Args> auto operator()(Args... args) { return f(fix(f), args...); } }; template<typename Func> fixed<Func> fix(Func f) { return fixed<Func> { f }; }
YίϯϏωʔλ? • ಈ͔͢͜ͱ͕తͳΒYίϯϏωʔλͱ͔Θͳ͍Ͱྑ͍ • ࠓճΈ͍ͨʹෆಈίϯϏωʔλͷ࠶ؼతͳఆٛΛͦͷ· ·ॻ͖Լͤྑ͍ • ྫ͑ Haskell ͷෆಈίϯϏωʔλͱͯγϯϓϧ:
fix f = f (fix f) • YίϯϏωʔλͱ͔ZίϯϏωʔλඇ࠶ؼతͳͷͰ͍͢͝ • ཧతʹॏཁɺΒ͍͠
ࢀߟ • Recursive Lambda functions in C++14 | Stack Overflow
http://stackoverflow.com/a/18085333