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
700
RubyとJIT
naoys
0
140
将棋盤を画像認識したかった
naoys
0
1.5k
Rust で乗り換え案内
naoys
0
610
疎行列と Jaccard 類似度の高速計算
naoys
1
550
有理数集合の濃度
naoys
2
110
YARVの最適化について調べた
naoys
0
110
転職会議サービスのAWS移行記録
naoys
0
50
入門AlphaGo
naoys
5
3.7k
Other Decks in Programming
See All in Programming
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
900
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
210
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
ヤプリ新卒SREの オンボーディング
masaki12
0
130
Remix on Hono on Cloudflare Workers
yusukebe
1
290
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.1k
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
170
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
330
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
120
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
CSC509 Lecture 12
javiergs
PRO
0
160
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
Featured
See All Featured
A Tale of Four Properties
chriscoyier
156
23k
The Cult of Friendly URLs
andyhume
78
6k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Side Projects
sachag
452
42k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
410
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Why Our Code Smells
bkeepers
PRO
334
57k
GraphQLとの向き合い方2022年版
quramy
43
13k
Raft: Consensus for Rubyists
vanstee
136
6.6k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
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