Functional Programming in C++ 学习笔记FPpr2&1

这是2的笔记

//删除字符串空白符
#include <iostream>
#include <numeric>
#include <functional>
#include <string>
#include <algorithm>
using namespace std;

string trim_left(string s)
{
	s.erase(s.begin(), find_if(s.begin(), s.end(), !' '/*is_not_space*/));
	//在这里留下一个疑问,书上并没有写is_not_space 的define,那究竟如何表达呢?我感觉和我写的是等价,但是无法运行
	return(s);
}
int main()
{
	string s = "        dfdfesdfc";
	cout << trim_left(s) << endl;
}

string trim_right(string s)
{
	s.erase(find_if(s.rbegin(), s.rend(), !' ').base(),s.end());//这一行代码不是很懂,base()是什么意思
	return(s);
}

这是1的笔记

//新思路:FP vector求平均值
#include <iostream>
#include <vector>
#include <numeric>
#include <functional>
#include <algorithm>
#include <string>
using namespace std;

double average_score(const vector<int>& scores)//新的vector<int>向量中引入scores向量
{
	int sum = 0;
	for (int score : scores) {//取完scores内所有元素,并且将每个元素在循环内都命名为score
		sum += score;
	}
	return sum / (double)scores.size();
}

double average_score_fp(const vector<int>& scores)//利用STL,numeric库去进行FP求平均值
{
	return (double)accumulate(scores.cbegin(), scores.cend(), 0) / scores.size();
}

double scores_product(const vector<int>& scores)//利用STL,functional库去FP相乘(作为accumulate的算子)
{
	return accumulate(scores.cbegin(), scores.cend(), 1, multiplies<int>());
}

void know_multiplies(void)//实现两个向量内数字分别相乘
{
	vector<int> v1 = { 10, 20, 30, 40, 50 };
	vector<int> v2 = { 1, 2, 3, 4, 5 };
	vector<int> result(5);
	transform(v1.begin(), v1.end(), v2.begin(), result.begin(), multiplies<int>());

	for (int i : result) {
		cout << i << " ";
	}
	cout << std::endl;
	return;
}

//以下为统计换行符的程序,帮助理解accumulate的折叠算法
int help(int previous_count, char c)
{
	return (c != ' ') ? previous_count : previous_count + 1;
}

int count_space(const string& s) {
	return accumulate(s.cbegin(), s.cend(), 0, help);
}

int main() {
	/*
	int a[6] = { 1,2,3,4,5,6 };
	vector<int> b;
	for (int i = 0; i <= 5; i++)
		b.push_back(a[i]);//push_back的意思是尾部添加
	cout<<average_score_fp(b)<<endl;
	know_multiplies();
	*/
	cout<<count_space("fuck you bitch")<<endl;
	return 0;
}
//如果要进行右折叠,可以使用crbegin和crend的方法(c_reverse_begin)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Learning C++ Functional Programming by Wisnu Anggoro English | 10 Aug. 2017 | ISBN: 1787281973 | ASIN: B06WVD7CVT | 304 Pages | AZW3 | 2.4 MB Key Features Modularize your applications and make them highly reusable and testable Get familiar with complex concepts such as metaprogramming, concurrency, and immutability A highly practical guide to building functional code in C++ filled with lots of examples and real-world use cases Book Description Functional programming allows developers to divide programs into smaller, reusable components that ease the creation, testing, and maintenance of software as a whole. Combined with the power of C++, you can develop robust and scalable applications that fulfill modern day software requirements. This book will help you discover all the C++ 17 features that can be applied to build software in a functional way. The book is divided into three modules—the first introduces the fundamentals of functional programming and how it is supported by modern C++. The second module explains how to efficiently implement C++ features such as pure functions and immutable states to build robust applications. The last module describes how to achieve concurrency and apply design patterns to enhance your application's performance. Here, you will also learn to optimize code using metaprogramming in a functional way. By the end of the book, you will be familiar with the functional approach of programming and will be able to use these techniques on a daily basis. What you will learn Get to know the difference between imperative and functional approaches See the use of first-class functions and pure functions in a functional style Discover various techniques to apply immutable state to avoid side effects Design a recursive algorithm effectively Create faster programs using lazy evaluation Structure code using design patterns to make the design process easier Use concurrency techniques to develop responsive software Learn how to use the C++ Standard Template Library and metaprogramming in a functional way to improve code optimization About the Author Wisnu Anggoro is a Microsoft Certified Professional in C# programming and an experienced C/C++ developer. He has also authored the books Boost.Asio C++ Network Programming - Second Edition and Functional C# by Packt. He has been programming since he was in junior high school, which was about 20 years ago, and started developing computer applications using the BASIC programming language in the MS-DOS environment. He has solid experience in smart card programming, as well as desktop and web application programming, including designing, developing, and supporting the use of applications for SIM Card Operating System Porting, personalization, PC/SC communication, and other smart card applications that require the use of C# and C/C++. He is currently a senior smart card software engineer at CIPTA, an Indonesian company that specializes in innovation and technology for smart cards. He can be reached through his email at wisnu@anggoro.net. Table of Contents Diving into Modern C++ Manipulating functions in functional programming Applying immutable state to the function Recurring method invocation using recursive algorithm Procrastinating the execution process using Lazy Evaluation Optimizing code with Metaprogramming Running parallel execution using Concurrency Creating and debugging application in functional approach
Explore functional programming and discover new ways of thinking about code. You know you need to master functional programming, but learning one functional language is only the start. In this book, through articles drawn from PragPub magazine and articles written specifically for this book, you'll explore functional thinking and functional style and idioms across languages. Led by expert guides, you'll discover the distinct strengths and approaches of Clojure, Elixir, Haskell, Scala, and Swift and learn which best suits your needs. Contributing authors: Rich Hickey, Stuart Halloway, Aaron Bedra, Michael Bevilacqua-Linn, Venkat Subramaniam, Paul Callaghan, Jose Valim, Dave Thomas, Natasha Murashev, Tony Hillerson, Josh Chisholm, and Bruce Tate. Functional programming is on the rise because it lets you write simpler, cleaner code, and its emphasis on immutability makes it ideal for maximizing the benefits of multiple cores and distributed solutions. So far nobody's invented the perfect functional language - each has its unique strengths. In Functional Programming: A PragPub Anthology, you'll investigate the philosophies, tools, and idioms of five different functional programming languages. See how Swift, the development language for iOS, encourages you to build highly scalable apps using functional techniques like map and reduce. Discover how Scala allows you to transition gently but deeply into functional programming without losing the benefits of the JVM, while with Lisp-based Clojure, you can plunge fully into the functional style. Learn about advanced functional concepts in Haskell, a pure functional language making powerful use of the type system with type inference and type classes. And see how functional programming is becoming more elegant and friendly with Elixir, a new functional language built on the powerful Erlang base.The industry has been embracing functional programming more and more, driven by the need for concurrency and parallelism. This collection of articles will lead you to mastering the functional approach to problem solving. So put on your explorer's hat and prepare to be surprised. The goal of exploration is always discovery. What You Need: Familiarity with one or more programming languages.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值