coding_xeno
coding_xeno

Reputation: 49

How can I modify this code to use a function instead of lambda?

I have a working fibonacci function that is returning a list of fibonacci numbers from a dummy list {1,1,1,1,1,1}. Here is my code.

list<int> immutableFibonacci(int position)
{
list<int> oldList(position, int(1));

list<int> newList = accumulate(oldList.begin(), oldList.end(), list<int>{},
    [](const list<int> a, int b)
{
    list<int> d = a;

    if (a.size()<2)
    {
        d.push_back(1);
    }
    else
    {
        auto start = d.rbegin();
        auto first = *start;
        start++;
        auto second = *start;
        d.push_back(first + second);
    }
    return d;
});
    return newList;
}

Instead of passing the lambda expression [](const list a, int b) to calculate, I want to pass the name of a function. How can I go about doing this? It would basically be a function inside the function immutableFibonacci, but I am having trouble in doing this.

Upvotes: 2

Views: 148

Answers (3)

Gerhard Stein
Gerhard Stein

Reputation: 1563

Thanks for using my answer :-) -> How can I use accumulate in C++ to create the Fibonacci sequence?

Here is my solution for your question:

#include <iostream>
#include <vector>
#include <numeric>
#include <string>
#include <functional>



auto fibo (std::vector<int>& a, int b) 
{
     if(a.size()<2)
     {
          a.push_back(1);
     }
     else
     {
          auto start = a.rbegin();
          auto first = *start;
          start++;
          auto second = *start;
          a.push_back(first+second);
     }
     return a;
}

int main()
{
    const std::vector<int> v{1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

    std::vector<int> s = std::accumulate(v.begin(), v.end(), 
                                     std::vector<int>{}, fibo);

    std::cout << "Fibo: " <<'\n';

    for( auto c : s )
    {
        std::cout << c << "-";
    }
    std::cout << '\n';
}

Remember to compile that with: g++ --std=c++14 fibo.cpp -o fibo.

Upvotes: 1

Alex Chudinov
Alex Chudinov

Reputation: 724

You can create a functor inside the immutableFibonacci function like this:

struct
{
    list<int> operator()(const list<int>& a, int b) const
    {
        //...then put your lambda code here...
    }
} funObj;

And then just use the "funObj" name inside the accumulate function instead of lambda. That's all.

Upvotes: 0

Arnav Borborah
Arnav Borborah

Reputation: 11789

Instead of:

list<int> newList = accumulate(oldList.begin(), oldList.end(), list<int>{},
    [](const list<int> a, int b)
{
    list<int> d = a;

    if (a.size()<2)
    {
        d.push_back(1);
    }
    else
    {
        auto start = d.rbegin();
        auto first = *start;
        start++;
        auto second = *start;
        d.push_back(first + second);
    }
    return d;
});

the above, and pass an actual function, like this. You need function pointers:

list<int> newList = accumulate(oldList.begin(), oldList.end(), list<int>{},
    funcname);

//Below is the function    

list<int> funcname(list<int> a, int b) {
    list<int> d = a;

    if (a.size()<2)
    {
        d.push_back(1);
    }
    else
    {
        auto start = d.rbegin();
        auto first = *start;
        start++;
        auto second = *start;
        d.push_back(first + second);
    }
    return d;
}

For a custom comparator, you could either pass a function pointer, or a lambda, and for your example, you used a lambda.

Upvotes: 1

Related Questions