Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Join list returned from QtConcurrent::map into a single list
Forum Updated to NodeBB v4.3 + New Features

Join list returned from QtConcurrent::map into a single list

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 917 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Defohin
    wrote on last edited by
    #1

    I was wondering how to join multiple lists (QStringList) returned by QtConcurrent::map into a single list?

    QStringList magic(const QString &name)
    {
        return { name.toLower(), name.toUpper() };
    }
    
    QStringList names = { "john", "jane" };
    QFuture<QStringList> example = QtConcurrent::mapped(names, magic);
    

    In example.results() I will get (("john", "JOHN"), ("jane", "JANE")), I want to reduce it to ("john", "JOHN", "jane", "JANE") in a single QStringList.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      you can either merge them afterwards:

      QStringList finalRes;
      const auto interRes = example.results();
      for(auto i=interRes.constBegin();i!=interRes.constEnd();++i)
      finalRes.append(*i);
      

      or use mappedReduced

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Defohin
        wrote on last edited by Defohin
        #3

        I tried to use mappedReduced as it seems to be the more appropriated way, but it doesn't work as I expected.

        The first thing I noticed is that the reduce method can't be a lambda, and...

        void reduce(QStringList &result, const QStringList &names)
        {
            // ...
        }
        

        And the second thing is that mappedReduced will call reduce twice and I don't know how to merge from here...

        I don't know where to go now...

        VRoninV 1 Reply Last reply
        0
        • D Defohin

          I tried to use mappedReduced as it seems to be the more appropriated way, but it doesn't work as I expected.

          The first thing I noticed is that the reduce method can't be a lambda, and...

          void reduce(QStringList &result, const QStringList &names)
          {
              // ...
          }
          

          And the second thing is that mappedReduced will call reduce twice and I don't know how to merge from here...

          I don't know where to go now...

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          @Defohin said in Join list returned from QtConcurrent::map into a single list:

          The first thing I noticed is that the reduce method can't be a lambda

          Not really, you just need to explicitly set the template parameter.

          @Defohin said in Join list returned from QtConcurrent::map into a single list:

          And the second thing is that mappedReduced will call reduce twice

          yes, 2 elements in the list = 2 calls.

          using your example and a lambda:

          QFuture<QStringList> example = QtConcurrent::mappedReduced<QStringList /*this has to be explicitly set to the same type as the first argument in the reduce lambda*/>(
          names
          , magic
          , [](QStringList& redu,const QStringList& val)->void{redu.append(val);}
          , QtConcurrent::OrderedReduce // ensures the order is the same as the input
          );
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          3
          • D Offline
            D Offline
            Defohin
            wrote on last edited by
            #5

            Oh gosh, you are amazing, thank you.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved