From the course: Introducing Functional Programming in C++
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
accumulate instead of reduce - C++ Tutorial
From the course: Introducing Functional Programming in C++
accumulate instead of reduce
- [Instructor] At last we've reached the Accumulate function. So here we will do int results equals accumulate and accumulate takes four parameters. It's going to take a filtered collection dot begin that is the begin iterator, a filtered collection dot end for end iterator, do a comma and then go to the next line. Then it takes a, and in this case it's going to be an integer value, which is the initial value for the accumulate. And in our case, it's going to be zero 'cause we want to add all our numbers together and we're going to start at zero. Then it takes the lambda function, which is a binary operation. And so this takes two values, the first one's going to be int total and the second one we're going to call int current. Then inside we're going to say return total plus current. And then a semi-colon, end of our curly brace, and then we're going to have a semi-colon. And right here we'll do a c out. And we'll call this accumulate and an end l, then we'll do another c out. And…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
(Locked)
Enabling C++ 174m 24s
-
(Locked)
transform instead of map5m 30s
-
(Locked)
copy_if instead of filter2m 59s
-
(Locked)
accumulate instead of reduce2m 51s
-
(Locked)
bind4m 56s
-
(Locked)
Functions6m 54s
-
(Locked)
Lambda functions: Curry3m 38s
-
(Locked)
Lambda functions: Closures2m 20s
-
(Locked)
Challenge: Factorial continuous add33s
-
(Locked)
Solution: Factorial continuous add3m 15s
-
(Locked)
-
-
-