GithubHelp home page GithubHelp logo

Improve `ureact::collect` about ureact HOT 7 CLOSED

YarikTH avatar YarikTH commented on August 22, 2024
Improve `ureact::collect`

from ureact.

Comments (7)

YarikTH avatar YarikTH commented on August 22, 2024

Test code have written to check the feature

TEST_CASE( "CollectAssociative" )
{
    ureact::context ctx;

    auto src = ureact::make_source<std::pair<std::string, int>>( ctx );
    auto src_tuple = ureact::transform(
        src, []( const auto& pair ) { return std::make_tuple( pair.first, pair.second ); } );

    ureact::signal<std::map<std::string, int>> collected_map;
    ureact::signal<std::map<std::string, int>> collected_map_2;

    SUBCASE( "Functional syntax" )
    {
        collected_map = ureact::collect<std::map>( src );
        collected_map_2 = ureact::collect<std::map>( src_tuple );
    }
    SUBCASE( "Piped syntax" )
    {
        collected_map = src | ureact::collect<std::map>;
        collected_map_2 = src_tuple | ureact::collect<std::map>;
    }

    src << std::pair( "A", 1 );
    src << std::pair( "B", 2 );
    src << std::pair( "C", 3 );

    using namespace std::string_literals;
    const auto expected = std::map{
        std::pair( "A"s, 1 ),
        std::pair( "B"s, 2 ),
        std::pair( "C"s, 3 ),
    };

    CHECK_EQ( collected_map.get(), expected );
    CHECK_EQ( collected_map_2.get(), expected );
}

from ureact.

YarikTH avatar YarikTH commented on August 22, 2024

std::to proposal https://open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1206r7.pdf

auto l = std::views::iota(1, 10);

// create a vector with the elements of l
auto vec = ranges::to<std::vector<int>>(l); // or vector{std::from_range, l};

//Specify an allocator
auto b = ranges::to<std::vector<int, Alloc>>(l, alloc); // or vector{std::from_range, l, alloc};

//deducing value_type
auto c = ranges::to<std::vector>(l);

// explicit conversion int -> long
auto d = ranges::to<std::vector<long>>(l);

//Supports converting associative container to sequence containers
auto f = ranges::to<std::vector>(m);

//Supports converting sequence containers to associative ones
auto g = ranges::to<std::map>(f);

//Pipe syntaxe
auto g = l | ranges::view::take(42) | ranges::to<std::vector>();

//Pipe syntax with allocator
auto h = l | ranges::view::take(42) | ranges::to<std::vector>(alloc);

//The pipe syntax also support specifying the type and conversions
auto i = l | ranges::view::take(42) | ranges::to<std::vector<long>>();

// Nested ranges
std::list<std::forward_list<int>> lst = {{0, 1, 2, 3}, {4, 5, 6, 7}};
auto vec1 = ranges::to<std::vector<std::vector<int>>>(lst);
auto vec2 = ranges::to<std::vector<std::deque<double>>>(lst);

from ureact.

YarikTH avatar YarikTH commented on August 22, 2024

Some checks in the godbolt https://godbolt.org/z/jqePW8a7h. It seems that it is impossible to accept both concrete type and parameterized type at the same time if it is not an overloaded template function.

Optional passing of allocator is another can of worm I don't want to touch for now. The same with nested containers. It doesn't look practical for ureact anyway.

from ureact.

YarikTH avatar YarikTH commented on August 22, 2024

Update on godbolt https://godbolt.org/z/9hsvjfzfv.

from ureact.

YarikTH avatar YarikTH commented on August 22, 2024

Associative container support is added. But there is at least some ambiguity with std::map: it has both .insert() method that doesn't override previous values and operator[] that does override. It is not clear which behavior is desired. For now operator[] has bigger priority than .insert().

from ureact.

YarikTH avatar YarikTH commented on August 22, 2024

So, no allocator support for now and no passing of specific class for now (it require a new adaptor name like collect_in<std::vector>). I have no idea how to use allocators, so it's hard to make by me.

from ureact.

YarikTH avatar YarikTH commented on August 22, 2024

Closing for now. Reopen if further improvements are wanted

from ureact.

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.