Code Monkey home page Code Monkey logo

Comments (5)

Dobiasd avatar Dobiasd commented on June 2, 2024 2

Would you like to create a pull request, adding this to show.hpp, and, if you like, your test case from above to show_test.cpp?

from functionalplus.

Dobiasd avatar Dobiasd commented on June 2, 2024

Hi, thanks for the positive feedback and the excellent idea. 🙂

So far, there is nothing in FunctionalPlus to print tuples.

To keep it generic (and reuse the separator/prefix/suffix logic from the fplus::show_cont... function family), I guess it would make sense not to add something to directly print a tuple but to just convert it into a list of strings.

Would you like to give the implementation a try in a pull request?

Otherwise, I'd see if I can do something, based on the cppreference example.

from functionalplus.

ferdymercury avatar ferdymercury commented on June 2, 2024

Thanks for the swift reply.

To keep it generic (and reuse the separator/prefix/suffix logic from the fplus::show_cont... function family), I guess it would make sense not to add something to directly print a tuple but to just convert it into a list of strings.

You mean using stringstream instead of iostream ?

from functionalplus.

ferdymercury avatar ferdymercury commented on June 2, 2024

This could do the job:

#include <iostream>
#include <tuple>
#include <string>
#include <sstream>
#include <list>

template<class Tuple, std::size_t N>
struct TuplePrinter {
    static void print(const Tuple& t, std::list<std::string>& sl) 
    {
        TuplePrinter<Tuple, N-1>::print(t,sl);
        std::stringstream ss;
        ss << std::get<N-1>(t);
        sl.emplace_back(ss.str());
    }
};
 
template<class Tuple>
struct TuplePrinter<Tuple, 1> {
    static void print(const Tuple& t, std::list<std::string>& sl) 
    {
        std::stringstream ss;
        ss << std::get<0>(t);
        sl.emplace_back(ss.str());
    }
};
 
template<typename... Args, std::enable_if_t<sizeof...(Args) == 0, int> = 0>
void print(const std::tuple<Args...>& t, std::list<std::string>& sl)
{
    return;
}

template<typename... Args, std::enable_if_t<sizeof...(Args) != 0, int> = 0>
std::list<std::string> print(const std::tuple<Args...>& t)
{
    std::list<std::string> sl;
    TuplePrinter<decltype(t), sizeof...(Args)>::print(t,sl);
    return std::move(sl);
}

int main() {
    
    
    std::tuple<int, std::string, float> t1(10, "Test", 3.14);
    std::list<std::string> lt1 = print(t1);
    for(auto &l : lt1) std::cout << l << ", ";
}

from functionalplus.

Dobiasd avatar Dobiasd commented on June 2, 2024

Yes, exactly! 🥳

And then, one could use std::cout << fplus::show_cont(lt1); instead of for(auto &l : lt1) std::cout << l << ", ";.

from functionalplus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.