function template and adding array with lambda expression

closed account (26q2b7Xj)
I have been learning the basics of templates and lambda expression, and there's one exercise that I am having with knowing how to invoke the lambda function. it was a prototype given, but I am trying to understand how do I go about passing in arguments for the lambda. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template <typename Fn, typename In, typename Out>
constexpr Out fold(Fn function, In *input, size_t length, Out initial)
{
    for(size_t i{}; i < length; ++i)
    {
        initial += function(//);
    }

    return initial;
}


int main()
{
    int data[]{100, 200, 300, 400, 500};
    size_t  data_len{ 5 };

    auto sum{ fold([](auto x, auto y){ return x + y;}, data, data_len, 0) };

    printf("Sum: %d\n", sum);
}
Line 6 could read
initial = function(std::move(initial), input[i]);
Last edited on
This seems like a horrible example. Here's one way of "solving" it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
template <typename Fn, typename In, typename Out>
constexpr Out fold(Fn function, In *input, size_t length, Out initial)
{
	for (size_t i{1}; i < length; i+=2)
	{
		initial += function(input[i-1], input[i]);
	}

	if (length % 2 == 1)
		initial += input[length-1];

	return initial;
}


int main()
{
	int data[]{ 100, 200, 300, 400, 500 };
	size_t  data_len{ 5 };

	auto sum{ fold([](auto x, auto y) { return x + y; }, data, data_len, 0) };

	printf("Sum: %d\n", sum);
}


You pass in the lambda as a function that can now be used in fold(). You already know that it takes in 2 variables and outputs their sum. Using it is no different then using a regular old function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
template <typename Auto>
auto function(Auto x, Auto y)
{
	return x + y;
}

template <typename In, typename Out>
constexpr Out fold(In *input, size_t length, Out initial)
{
	for (size_t i{1}; i < length; i+=2)
	{
		initial += function(input[i-1], input[i]);
	}

	if (length % 2 == 1)
		initial += input[length-1];

	return initial;
}


int main()
{
	int data[]{ 100, 200, 300, 400, 500 };
	size_t  data_len{ 5 };

	auto sum{ fold(data, data_len, 0) };

	printf("Sum: %d\n", sum);
}
closed account (26q2b7Xj)
Thanks for the examples! Reading your examples causes confusion in what the person was trying to go for. This was his description:

Implement a fold function template with the following prototype:
1
2
template <typename Fn, typename In, typename Out>
constexpr Out fold(Fn function, In* input, size_t length, Out initial);

For example, your implementation must support the following usage:
1
2
3
4
5
6
7
int main() {
 int data[]{ 100, 200, 300, 400, 500 };
 size_t data_len = 5;
 auto sum = fold([](auto x, auto y) { return x + y; }, data, data_len,
0);
 print("Sum: %d\n", sum);
}

The value of sum should be 1,500. Use fold to calculate the following quantities: the maximum, the minimum, and the number of elements greater than 200.
Last edited on
All answers given here will have sum be equal to 1,500.

Calculating max, min, and the number of elements above 200 won't be using the lambda.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <utility>
#include <stddef.h>
    
template <typename Fn, typename In, typename Out>
constexpr Out fold(Fn function, In *input, size_t length, Out initial)
{
    for (size_t i = 0; i < length; ++i) 
        initial = function(std::move(initial), input[i]);
    return initial;
}

int main()
{
    constexpr int len = 5;
    int data[len] = {100, 200, 300, 400, 500};
    
    auto bsum = [](auto x, auto y) { return x + y; };
    auto bmax = [](auto x, auto y) { return x > y? x: y; };
    auto bmin = [](auto x, auto y) { return x < y? x: y; };
    auto n_over_200 = [](auto x, auto y) { return y > 200? 1 + x: x; };
    
    std::cout << "sum:   " << fold(bsum, data, len, 0) << '\n'
              << "max:   " << fold(bmax, data, len, data[0]) << '\n'
              << "min:   " << fold(bmin, data, len, data[0]) << '\n'
              << "> 200: " << fold(n_over_200, data, len, 0) << '\n';
}

http://coliru.stacked-crooked.com/a/4a0343dc1484b8d6
closed account (26q2b7Xj)
Yeah that was going to be my approach. But was start on how to pass two arguments. Thanks guys!
Topic archived. No new replies allowed.