[Qt5-feedback] Lambdas

Konrad Rosenbaum konrad at silmor.de
Wed Jun 29 21:27:57 CEST 2011


On Monday 27 June 2011, Thiago Macieira wrote:
> Em Monday, 27 de June de 2011, às 10:33:08, Ivan Cukic escreveu:
> > I'm mostly interested in adding methods for Qt's collections (mapping,
> > filtering, folding... and stuff commonly found in functional pls)
> 
> To be honest, I'd rather we avoid adding more methods to the functional
> and algorithmic parts. We should provide the basic algorithms (qCopy,
> qStableSort, qLowerBind, etc.), but anything else should be just STL.

How about extending the basic methods of collections - just the other day I 
wished I had abilities that are normal for SmallTalk programmers.

One example: you want to know whether a list contains an element that 
matches a specific pattern. I recently had to refactor some code that does 
this.

Old version (the list contains just names):
QStringList mylist;
//...
if(mylist.contains("hello"))doSomething();

New Version (the elements stored have more than names now):
struct mydata { QString name,data; };
QList<mydata> mylist;
//...
for(int i=0;i<mylist.size();i++)
  if(mylist[i].name=="hello"){
    doSomething();
    break;
  }

...this is rather verbose and inelegant.

What I would have liked would have been a lambda based approach:
struct mydata { QString name,data; };
QList<mydata> mylist;
//...
if(mylist.contains( [](mydata&dat){return dat.name=="hello";} ) )
  doSomething();


As a comparison SmallTalk has the following standard methods for all 
collections:

mycollection do: [:x|some code: x]
 -> executes the code for every element in the collection

newcollection := mycollection select: [:x | criteria compare: x]
 -> executes the criteria on every element and expects a boolean result, if 
it is true the element will be included in the result collection
 -> there is also a reject: method that excludes anything that matches

mycollection detect: [:x | criteria compare: x]
 -> returns the index of the first element that matches the criteria
 -> indexOf: works like the one in Qt, detect: uses a lambda instead
 -> includes: is comparable to current Qt contains()

mycollection occurencesOf: [:x | criteria compare: x]
 -> counts how many match

newcollection := mycollection collect: [:x | x * 2]
 -> creates a new collection that contains every element transformed by the 
given expression

mycollection conform: [:x | criteria compare: x]
 -> returns true if ALL elements conform to the criteria

These methods have proven to be extremely useful in the SmallTalk projects 
that I have done. Now that lambdas do exist for C++ I believe they will be 
just as useful here.



	Konrad

PS: for those unfamiliar with SmallTalk:

object method: parameter
 would be object.method(parameter); in C++

[ some code ]
 is a code block, it is comparable to a lambda expression, the last
 statement is the return value of the block, C++ would be:
 [&](){return some.code();}
 -> SmallTalk always uses references, so I translated with & to C++

[ :parameter | some code: parameter ]
 is a code block with parameter, in C++:
 [&](Type &parameter){return some.code(parameter);}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt.nokia.com/pipermail/qt5-feedback/attachments/20110629/9b861c97/attachment.bin 


More information about the Qt5-feedback mailing list