现代C++函数式编程
作者簡(jiǎn)介:?祁宇,武漢烽火云創(chuàng)軟件技術(shù)有限公司研發(fā)中心技術(shù)總監(jiān),《深入應(yīng)用C++11》作者,C++開源社區(qū)purecpp.org創(chuàng)始人,致力于C++11的應(yīng)用、研究和推廣。樂于研究和分享技術(shù),愛好C++,愛好開源。
導(dǎo)讀:?本文作者從介紹函數(shù)式編程的概念入手,分析了函數(shù)式編程的表現(xiàn)形式和特性,最終通過(guò)現(xiàn)代C++的新特性以及一些模板云技巧實(shí)現(xiàn)了一個(gè)非常靈活的pipeline,展示了現(xiàn)代C++實(shí)現(xiàn)函數(shù)式編程的方法和技巧,同時(shí)也體現(xiàn)了現(xiàn)代C++的強(qiáng)大威力和無(wú)限可能。
概述
函數(shù)式編程是一種編程范式,它有下面的一些特征:
- 函數(shù)是一等公民,可以像數(shù)據(jù)一樣傳來(lái)傳去。
- 高階函數(shù)
- 遞歸
- pipeline
- 惰性求值
- 柯里化
- 偏應(yīng)用函數(shù)
C++98/03中的函數(shù)對(duì)象,和C++11中的Lambda表達(dá)式、std::function和std::bind讓C++的函數(shù)式編程變得容易。我們可以利用C++11/14里的新特性來(lái)實(shí)現(xiàn)高階函數(shù)、鏈?zhǔn)秸{(diào)用、惰性求值和柯理化等函數(shù)式編程特性。本文將通過(guò)一些典型示例來(lái)講解如何使用現(xiàn)代C++來(lái)實(shí)現(xiàn)函數(shù)式編程。
高階函數(shù)和pipeline的表現(xiàn)形式
高階函數(shù)就是參數(shù)為函數(shù)或返回值為函數(shù)的函數(shù),經(jīng)典的高階函數(shù)就是map、filter、fold和compose函數(shù),比如Scala中高階函數(shù):
-
map
numbers.map((i: Int) => i * 2)對(duì)列表中的每個(gè)元素應(yīng)用一個(gè)函數(shù),返回應(yīng)用后的元素所組成的列表。
-
filter
numbers.filter((i: Int) => i % 2 == 0)移除任何對(duì)傳入函數(shù)計(jì)算結(jié)果為false的元素。
-
fold
numbers.fold(0) { (z, i) => a + i }將一個(gè)初始值和一個(gè)二元函數(shù)的結(jié)果累加起來(lái)。
-
compose
val fComposeG = f _ compose g _ fComposeG("x")組合其它函數(shù)形成一個(gè)新函數(shù)f(g(x))。
上面的例子中,有的是參數(shù)為函數(shù),有的是參數(shù)和返回值都是函數(shù)。高階函數(shù)不僅語(yǔ)義上更加抽象泛化,還能實(shí)現(xiàn)“函數(shù)是一等公民”,將函數(shù)像data一樣傳來(lái)傳去或者組合,非常靈活。其中,compose還可以實(shí)現(xiàn)惰性求值,compose的返回結(jié)果是一個(gè)函數(shù),我們可以保存起來(lái),在后面需要的時(shí)候調(diào)用。
pipeline把一組函數(shù)放到一個(gè)數(shù)組或是列表中,然后把數(shù)據(jù)傳給這個(gè)列表。數(shù)據(jù)就像一個(gè)鏈條一樣順序地被各個(gè)函數(shù)所操作,最終得到我們想要的結(jié)果。它的設(shè)計(jì)哲學(xué)就是讓每個(gè)功能就做一件事,并把這件事做到極致,軟件或程序的拼裝會(huì)變得更為簡(jiǎn)單和直觀。?
Scala中的鏈?zhǔn)秸{(diào)用是這樣的:
用法和Unix Shell的管道操作比較像,|前面的數(shù)據(jù)或函數(shù)作為|后面函數(shù)的輸入,順序執(zhí)行直到最后一個(gè)函數(shù)。
這種管道方式的函數(shù)調(diào)用讓邏輯看起來(lái)更加清晰明了,也非常靈活,允許你將多個(gè)高階函數(shù)自由組合成一個(gè)鏈條,同時(shí)還可以保存起來(lái)實(shí)現(xiàn)惰性求值。現(xiàn)代C++實(shí)現(xiàn)這種pipeline也是比較容易的,下面來(lái)講解如何充分借助C++11/14的新特性來(lái)實(shí)現(xiàn)這些高階函數(shù)和pipeline。
實(shí)現(xiàn)pipeline的關(guān)鍵技術(shù)
根據(jù)前面介紹的pipeline表現(xiàn)形式,可以把pipeline分解為幾部分:高階函數(shù),惰性求值,運(yùn)算符|、柯里化和pipeline,把這幾部分實(shí)現(xiàn)之后就可以組成一個(gè)完整的pipeline了。下面來(lái)分別介紹它們的實(shí)現(xiàn)技術(shù)。
高階函數(shù)
函數(shù)式編程的核心就是函數(shù),它是一等公民,最靈活的函數(shù)就是高階函數(shù),現(xiàn)代C++的算法中已經(jīng)有很多高階函數(shù)了,比如for_each, transform:
std::vector<int> vec{1,2,3,4,5,6,7,8,9} //接受一個(gè)打印的Lambda表達(dá)式 std::for_each(vec.begin(), vec.end(), [](auto i){ std::cout<<i<<std::endl; }); //接受一個(gè)轉(zhuǎn)換的Lambda表達(dá)式 transform(vec.begin(), vec.end(), vec.begin(), [](int i){ return i*i; });這些高階函數(shù)不僅可以接受Lambda表達(dá)式,還能接受std::function、函數(shù)對(duì)象、普通的全局函數(shù),很靈活。需要注意的是,普通的全局函數(shù)在pipeline時(shí)存在局限性,因?yàn)樗幌窈瘮?shù)對(duì)象一樣可以保存起來(lái)延遲調(diào)用,所以我們需要一個(gè)方法將普通的函數(shù)轉(zhuǎn)換為函數(shù)對(duì)象。std::bind也可以將函數(shù)轉(zhuǎn)化為函數(shù)對(duì)象,但是bind不夠通用,使用的時(shí)候它只能綁定有限的參數(shù),如果函數(shù)本身就是可變參數(shù)的就無(wú)法bind了,所以,這個(gè)函數(shù)對(duì)象必須是泛化的,類似于這樣:
class universal_functor { public: template <typename... Args> auto operator()(Args&&... args) const ->decltype(globle_func(std::forward<Args>(args)...)){return globle_func(std::forward<Args>(args)...);} };上面的函數(shù)對(duì)象內(nèi)部包裝了一個(gè)普通函數(shù)的調(diào)用,當(dāng)函數(shù)調(diào)用的時(shí)候?qū)嶋H上會(huì)調(diào)用普通函數(shù)globle_func,但是這個(gè)代碼不通用,它無(wú)法用于其他的函數(shù)。為了讓這個(gè)轉(zhuǎn)換變得通用,我們可以借助一個(gè)宏來(lái)實(shí)現(xiàn)function到functor的轉(zhuǎn)換。
#define define_functor_type(func_name) class tfn_##func_name {\ public: template <typename... Args> auto operator()(Args&&... args) const ->decltype(func_name(std::forward<Args>(args)...))\ { return func_name(std::forward<Args>(args)...); } }//test code int add(int a, int b) {return a + b; } int add_one(int a) { return 1 + a; }define_functor_type(add); define_functor_type(add_one);int main() {tnf_add add_functor;add_functor(1, 2); //result is 3tfn_add_one add_one_functor;add_one_functor(1); //result is 2return 0; }我們先定義了一個(gè)宏,這個(gè)宏根據(jù)參數(shù)來(lái)生成一個(gè)可變參數(shù)的函數(shù)對(duì)象,這個(gè)函數(shù)對(duì)象的類型名為tfn_加普通函數(shù)的函數(shù)名,之所以要加一個(gè)前綴tfn_,是為了避免類型名和函數(shù)名重名。define_functor_type宏只是定義了一個(gè)函數(shù)對(duì)象的類型,用起來(lái)略感不便,還可以再簡(jiǎn)化一下,讓使用更方便。我們可以再定義一個(gè)宏來(lái)生成轉(zhuǎn)換后的函數(shù)對(duì)象:
#define make_globle_functor(NAME, F) const auto NAME = define_functor_type(F); //test code make_globle_functor(fn_add, add); make_globle_functor(fn_add_one, add_one);int main() {fn_add(1, 2);fn_add_one(1);return 0; }make_globle_functor生成了一個(gè)可以直接使用的全局函數(shù)對(duì)象,使用起來(lái)更方便了。用這個(gè)方法就可以將普通函數(shù)轉(zhuǎn)成pipeline中的函數(shù)對(duì)象了。接下來(lái)我們來(lái)探討實(shí)現(xiàn)惰性求值的關(guān)鍵技術(shù)。
惰性求值
惰性求值是將求值運(yùn)算延遲到需要值時(shí)候進(jìn)行,通常的做法是將函數(shù)或函數(shù)的參數(shù)保存起來(lái),在需要的時(shí)候才調(diào)用函數(shù)或者將保存的參數(shù)傳入函數(shù)實(shí)現(xiàn)調(diào)用。現(xiàn)代C++里已經(jīng)提供可以保存起來(lái)的函數(shù)對(duì)象和lambda表達(dá)式,因此需要解決的問題是如何將參數(shù)保存起來(lái),然后在需要的時(shí)候傳給函數(shù)實(shí)現(xiàn)調(diào)用。我們可以借助std::tuple、type_traits和可變模版參數(shù)來(lái)實(shí)現(xiàn)目標(biāo)。
template<typename F, size_t... I, typename ... Args> inline auto tuple_apply_impl(const F& f, const std::index_sequence<I...>&, const std::tuple<Args...>& tp) {return f(std::get<I>(tp)...); }template<typename F, typename ... Args> inline auto tuple_apply(const F& f, const std::tuple<Args...>& tp) -> decltype(f(std::declval<Args>()...)) {return tuple_apply_impl(f, std::make_index_sequence<sizeof... (Args)>{}, tp); }int main() { //test code auto f = [](int x, int y, int z) { return x + y - z; }; //將函數(shù)調(diào)用需要的參數(shù)保存到tuple中 auto params = make_tuple(1, 2, 3);//將保存的參數(shù)傳給函數(shù)f,實(shí)現(xiàn)函數(shù)調(diào)用 auto result = tuple_apply(f, params); //result is 0return 0; }上面的測(cè)試代碼中,我們先把參數(shù)保存到一個(gè)tuple中,然后在需要的時(shí)候?qū)?shù)和函數(shù)f傳入tuple_apply,最終實(shí)現(xiàn)了f函數(shù)的調(diào)用。tuple_apply實(shí)現(xiàn)了一個(gè)“魔法”將tuple變成了函數(shù)的參數(shù),來(lái)看看這個(gè)“魔法”具體是怎么實(shí)現(xiàn)的。
tuple_apply_impl實(shí)現(xiàn)的關(guān)鍵是在于可變模版參數(shù)的展開,可變模版參數(shù)的展開又借助了std::index_sequence
運(yùn)算符operator|
pipeline的一個(gè)主要表現(xiàn)形式是通過(guò)運(yùn)算符|來(lái)將data和函數(shù)分隔開或者將函數(shù)和函數(shù)組成一個(gè)鏈條,比如像下面的unix shell命令:
ps auwwx | awk '{print $2}' | sort -n | xargs echoC++實(shí)現(xiàn)類似的調(diào)用可以通過(guò)重載運(yùn)算符來(lái)實(shí)現(xiàn),下面是data和函數(shù)通過(guò)|連接的實(shí)現(xiàn)代碼:
template<typename T, class F> auto operator|(T&& param, const F& f) -> decltype(f(std::forward<T>(param))) {return f(std::forward<T>(param)); } //test code auto add_one = [](auto a) { return 1 + a; }; auto result = 2 | add_one; //result is 3除了data和函數(shù)通過(guò)|連接之外,還需要實(shí)現(xiàn)函數(shù)和函數(shù)通過(guò)|連接,我們通過(guò)可變參數(shù)來(lái)實(shí)現(xiàn):
template<typename... FNs, typename F> inline auto operator|(fn_chain<FNs...> && chain, F&& f) {return chain.add(std::forward<F>(f)); }//test code auto chain = fn_chain<>() | (filter >> [](auto i) { return i % 2 == 0; }) | ucount | uprint;其中fn_chain是一個(gè)可以接受任意個(gè)函數(shù)的函數(shù)對(duì)象,它的實(shí)現(xiàn)將在后面介紹。通過(guò)|運(yùn)算符重載我們可以實(shí)現(xiàn)類似于unix shell的pipeline表現(xiàn)形式。
柯里化
函數(shù)式編程中比較靈活的一個(gè)地方就是柯里化(currying),柯里化是把多個(gè)參數(shù)的函數(shù)變換成單參數(shù)的函數(shù),并返回一個(gè)新函數(shù),這個(gè)新函數(shù)處理剩下的參數(shù)。以Scala的柯里化為例:
- 未柯里化的函數(shù)
- 柯里化之后
currying之后add(1)(2)等價(jià)于add(1,2),這種currying默認(rèn)是從左到右的,如果希望從右到左呢,然而大部分編程語(yǔ)言沒有實(shí)現(xiàn)更靈活的curring。C++11里面的std::bind可以實(shí)現(xiàn)currying,但要實(shí)現(xiàn)向左或向右靈活的currying比較困難,可以借助tuple和前面介紹的tuple_apply來(lái)實(shí)現(xiàn)一個(gè)更靈活的currying函數(shù)對(duì)象。
template<typename F, typename Before = std::tuple<>, typename After = std::tuple<>> class curry_functor { private:F f_; ///< main functorBefore before_; ///< curryed argumentsAfter after_; ///< curryed argumentspublic:curry_functor(F && f) : f_(std::forward<F>(f)), before_(std::tuple<>()), after_(std::tuple<>()) {}curry_functor(const F & f, const Before & before, const After & after) : f_(f), before_(before), after_(after) {}template <typename... Args>auto operator()(Args... args) const -> decltype(tuple_apply(f_, std::tuple_cat(before_, make_tuple(args...), after_))){// execute via tuplereturn tuple_apply(f_, std::tuple_cat(before_, make_tuple(std::forward<Args>(args)...), after_));}// currying from left to righttemplate <typename T>auto curry_before(T && param) const{using RealBefore = decltype(std::tuple_cat(before_, std::make_tuple(param)));return curry_functor<F, RealBefore, After>(f_, std::tuple_cat(before_, std::make_tuple(std::forward<T>(param))), after_);}// currying from righ to lefttemplate <typename T>auto curry_after(T && param) const{using RealAfter = decltype(std::tuple_cat(after_, std::make_tuple(param)));return curry_functor<F, Before, RealAfter>(f_, before_, std::tuple_cat(after_, std::make_tuple(std::forward<T>(param))));} };template <typename F> auto fn_to_curry_functor(F && f) {return curry_functor<F>(std::forward<F>(f)); }//test code void test_count() {auto f = [](int x, int y, int z) { return x + y - z; };auto fn = fn_to_curry_functor(f);auto result = fn.curry_before(1)(2, 3); //0result = fn.curry_before(1).curry_before(2)(3); //0result = fn.curry_before(1).curry_before(2).curry_before(3)(); //0result = fn.curry_before(1).curry_after(2).curry_before(3)(); //2result = fn.curry_after(1).curry_after(2).curry_before(2)(); //1 }從測(cè)試代碼中可以看到這個(gè)currying函數(shù)對(duì)象,既可以從左邊currying又可以從右邊currying,非常靈活。不過(guò)使用上還不太方便,沒有fn(1)(2)(3)這樣方便,我們可以通過(guò)運(yùn)算符重載來(lái)簡(jiǎn)化書寫,由于C++標(biāo)準(zhǔn)中不允許重載全局的operater()符,并且operater()符無(wú)法區(qū)分到底是從左邊還是從右邊currying,所以我們選擇重載<<和>>操作符來(lái)分別表示從左至右currying和從右至左currying。
// currying from left to right template<typename UF, typename Arg> auto operator<<(const UF & f, Arg && arg) -> decltype(f.template curry_before<Arg>(std::forward<Arg>(arg))) {return f.template curry_before<Arg>(std::forward<Arg>(arg)); }// currying from right to left template<typename UF, typename Arg> auto operator>>(const UF & f, Arg && arg) -> decltype(f.template curry_after<Arg>(std::forward<Arg>(arg))) {return f.template curry_after<Arg>(std::forward<Arg>(arg)); }有了這兩個(gè)重載運(yùn)算符,測(cè)試代碼可以寫得更簡(jiǎn)潔了。
void test_currying() {auto f = [](int x, int y, int z) { return x + y - z; };auto fn = fn_to_curry_functor(f);auto result = (fn << 1)(2, 3); //0result = (fn << 1 << 2)(3); //0result = (fn << 1 << 2 << 3)(); //0result = (fn << 1 >> 2 << 3)(); //2result = (fn >> 1 >> 2 << 3)(); //1 }curry_functor利用了tuple的特性,內(nèi)部有兩個(gè)空的tuple,一個(gè)用來(lái)保存left currying的參數(shù),一個(gè)用來(lái)保存right currying的參數(shù),不斷地currying時(shí),通過(guò)tuple_cat把新currying的參數(shù)保存到tuple中,最后調(diào)用的時(shí)候?qū)uple成員和參數(shù)組成一個(gè)最終的tuple,然后通過(guò)tuple_apply實(shí)現(xiàn)調(diào)用。有了前面這些基礎(chǔ)設(shè)施之后我們實(shí)現(xiàn)pipeline也是水到渠成。
pipeline
通過(guò)運(yùn)算符|重載,我們可以實(shí)現(xiàn)一個(gè)簡(jiǎn)單的pipeline:
template<typename T, class F> auto operator|(T&& param, const F& f) -> decltype(f(std::forward<T>(param))) {return f(std::forward<T>(param)); } //test code void test_pipe() { auto f1 = [](int x) { return x + 3; };auto f2 = [](int x) { return x * 2; };auto f3 = [](int x) { return (double)x / 2.0; };auto f4 = [](double x) { std::stringstream ss; ss << x; return ss.str(); };auto f5 = [](string s) { return "Result: " + s; };auto result = 2|f1|f2|f3|f4|f5; //Result: 5 }這個(gè)簡(jiǎn)單的pipeline雖然可以實(shí)現(xiàn)管道方式的鏈?zhǔn)接?jì)算,但是它只是將data和函數(shù)通過(guò)|連接起來(lái)了,還沒有實(shí)現(xiàn)函數(shù)和函數(shù)的連接,并且是立即計(jì)算的,沒有實(shí)現(xiàn)延遲計(jì)算。因此我們還需要實(shí)現(xiàn)通過(guò)|連接函數(shù),從而實(shí)現(xiàn)靈活的pipeline。我們可以通過(guò)一個(gè)function chain來(lái)接受任意個(gè)函數(shù)并組成一個(gè)函數(shù)鏈。利用可變模版參數(shù)、tuple和type_traits就可以實(shí)現(xiàn)了。
template <typename... FNs> class fn_chain { private:const std::tuple<FNs...> functions_;const static size_t TUPLE_SIZE = sizeof...(FNs);template<typename Arg, std::size_t I>auto call_impl(Arg&& arg, const std::index_sequence<I>&) const ->decltype(std::get<I>(functions_)(std::forward<Arg>(arg))){return std::get<I>(functions_)(std::forward<Arg>(arg));}template<typename Arg, std::size_t I, std::size_t... Is>auto call_impl(Arg&& arg, const std::index_sequence<I, Is...>&) const ->decltype(call_impl(std::get<I>(functions_)(std::forward<Arg>(arg)), std::index_sequence<Is...>{})){return call_impl(std::get<I>(functions_)(std::forward<Arg>(arg)), std::index_sequence<Is...>{});}template<typename Arg>auto call(Arg&& arg) const-> decltype(call_impl(std::forward<Arg>(arg), std::make_index_sequence<sizeof...(FNs)>{})){return call_impl(std::forward<Arg>(arg), std::make_index_sequence<sizeof...(FNs)>{});}public:fn_chain() : functions_(std::tuple<>()) {}fn_chain(std::tuple<FNs...> functions) : functions_(functions) {}// add function into chaintemplate< typename F >inline auto add(const F& f) const{return fn_chain<FNs..., F>(std::tuple_cat(functions_, std::make_tuple(f)));}// call whole functional chaintemplate <typename Arg>inline auto operator()(Arg&& arg) const -> decltype(call(std::forward<Arg>(arg))){return call(std::forward<Arg>(arg));} };// pipe function into functional chain via | operator template<typename... FNs, typename F> inline auto operator|(fn_chain<FNs...> && chain, F&& f) {return chain.add(std::forward<F>(f)); }#define tfn_chain fn_chain<>()//test code void test_pipe() { auto f1 = [](int x) { return x + 3; };auto f2 = [](int x) { return x * 2; };auto f3 = [](int x) { return (double)x / 2.0; };auto f4 = [](double x) { std::stringstream ss; ss << x; return ss.str(); };auto f5 = [](string s) { return "Result: " + s; };auto compose_fn = tfn_chain|f1|f2|f3|f4|f5; //compose a chaincompose_fn(2); // Result: 5 }測(cè)試代碼中用一個(gè)fn_chain和運(yùn)算符|將所有的函數(shù)組合成了一個(gè)函數(shù)鏈,在需要的時(shí)候調(diào)用,從而實(shí)現(xiàn)了惰性求值。
fn_chain的實(shí)現(xiàn)思路是這樣的:內(nèi)部有一個(gè)std::tuple
template<typename Arg, std::size_t I>auto call_impl(Arg&& arg, const std::index_sequence<I>&) const ->decltype(std::get<I>(functions_)(std::forward<Arg>(arg))){return std::get<I>(functions_)(std::forward<Arg>(arg));}template<typename Arg, std::size_t I, std::size_t... Is>auto call_impl(Arg&& arg, const std::index_sequence<I, Is...>&) const ->decltype(call_impl(std::get<I>(functions_)(std::forward<Arg>(arg)), std::index_sequence<Is...>{})){return call_impl(std::get<I>(functions_)(std::forward<Arg>(arg)), std::index_sequence<Is...>{});}在調(diào)用call_impl的過(guò)程中,將std::index_sequence不斷展開,先從tuple中獲取第I個(gè)function,然后調(diào)用獲得第I個(gè)function的執(zhí)行結(jié)果,將這個(gè)執(zhí)行結(jié)果作為下次調(diào)用的參數(shù),不斷地遞歸調(diào)用,直到最后一個(gè)函數(shù)完成調(diào)用為止,返回最終的鏈?zhǔn)秸{(diào)用的結(jié)果。
至此我們實(shí)現(xiàn)具備惰性求值、高階函數(shù)和currying特性的完整的pipeline,有了這個(gè)pipeline,我們可以實(shí)現(xiàn)經(jīng)典的流式計(jì)算和AOP,接下來(lái)我們來(lái)看看如何利用pipeline來(lái)實(shí)現(xiàn)流式的mapreduce和靈活的AOP。
實(shí)現(xiàn)一個(gè)pipeline形式的mapreduce和AOP
前面的pipeline已經(jīng)可以實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用了,要實(shí)現(xiàn)pipeline形式的mapreduce關(guān)鍵就是實(shí)現(xiàn)map、filter和reduce等高階函數(shù)。下面是它們的具體實(shí)現(xiàn):
// MAP template <typename T, typename... TArgs, template <typename...>class C, typename F> auto fn_map(const C<T, TArgs...>& container, const F& f) -> C<decltype(f(std::declval<T>()))> {using resultType = decltype(f(std::declval<T>()));C<resultType> result;for (const auto& item : container)result.push_back(f(item));return result; }// REDUCE (FOLD) template <typename TResult, typename T, typename... TArgs, template <typename...>class C, typename F> TResult fn_reduce(const C<T, TArgs...>& container, const TResult& startValue, const F& f) {TResult result = startValue;for (const auto& item : container)result = f(result, item);return result; }// FILTER template <typename T, typename... TArgs, template <typename...>class C, typename F> C<T, TArgs...> fn_filter(const C<T, TArgs...>& container, const F& f) {C<T, TArgs...> result;for (const auto& item : container)if (f(item))result.push_back(item);return result; }這些高階函數(shù)還需要轉(zhuǎn)換成支持currying的functor,前面我們已經(jīng)定義了一個(gè)普通的函數(shù)對(duì)象轉(zhuǎn)換為柯里化的函數(shù)對(duì)象的方法:
template <typename F> auto fn_to_curry_functor(F && f) {return curry_functor<F>(std::forward<F>(f)); }通過(guò)下面這個(gè)宏讓currying functor用起來(lái)更簡(jiǎn)潔:
#define make_globle_curry_functor(NAME, F) define_functor_type(F); const auto NAME = fn_to_curry_functor(tfn_##F());make_globle_curry_functor(map, fn_map); make_globle_curry_functor(reduce, fn_reduce); make_globle_curry_functor(filter, fn_filter);我們定義了map、reduce和filter支持柯里化的三個(gè)全局函數(shù)對(duì)象,接下來(lái)我們就可以把它們組成一個(gè)pipeline了。
void test_pipe() {//test map reducevector<string> slist = { "one", "two", "three" };slist | (map >> [](auto s) { return s.size(); })| (reduce >> 0 >> [](auto a, auto b) { return a + b; })| [](auto a) { cout << a << endl; };//test chain, lazy evalauto chain = tfn_chain | (map >> [](auto s) { return s.size(); })| (reduce >> 0 >> [](auto a, auto b) { return a + b; })| ([](int a) { std::cout << a << std::endl; });slist | chain; }上面的例子實(shí)現(xiàn)了pipeline的mapreduce,這個(gè)pipeline支持currying還可以任意組合,非常方便和靈活。
有了這個(gè)pipeline,實(shí)現(xiàn)靈活的AOP也是很容易的:
struct person {person get_person_by_id(int id){this->id = id;return *this;}int id;std::string name; }; void test_aop() { const person& p = { 20, "tom" };auto func = std::bind(&person::get_person_by_id, &p, std::placeholders::_1); auto aspect = tfn_chain | ([](int id) { cout << "before"; return id + 1; })| func| ([](const person& p) { cout << "after" << endl; });aspect(1); }上面的測(cè)試?yán)又?#xff0c;核心邏輯是func函數(shù),我們可以在func之前或之后插入切面邏輯,切面邏輯可以不斷地加到鏈條前面或者后面,實(shí)現(xiàn)很巧妙,使用很常靈活。
總結(jié)
本文通過(guò)介紹函數(shù)式編程的概念入手,分析了函數(shù)式編程的表現(xiàn)形式和特性,最終通過(guò)現(xiàn)代C++的新特性和一些模版元技巧實(shí)現(xiàn)了一個(gè)非常靈活的pipeline,展示了現(xiàn)代C++實(shí)現(xiàn)函數(shù)式編程的方法和技巧,同時(shí)也提現(xiàn)了現(xiàn)代C++的強(qiáng)大威力和無(wú)限的可能性。文中完整的代碼可以從我的GitHub(https://github.com/qicosmos/cosmos/blob/master/modern_functor.hpp)上查看。
本文的代碼和思路參考和借鑒了http://vitiy.info/templates-as-first-class-citizens-in-cpp11/,在此表示感謝。
總結(jié)
以上是生活随笔為你收集整理的现代C++函数式编程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu 16.04 LTS下编译G
- 下一篇: 统计学习那些事