C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Can I simulate list comprehensions?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
seguso
Guest





PostPosted: Mon Dec 20, 2004 12:44 pm    Post subject: Can I simulate list comprehensions? Reply with quote



Hello,

For the sake of efficiency, I have chosen C++ as the language for a
"Free Adventure Construction Kit" I am developing ([url]http://fack.sf.net)[/url].

I am looking for a way to simulate list comprehensions a-la
Haskell/Python, in order to make the logic of my code more readable.
Any way to do that would be welcome, even if this means to use macros.

Here is the code I would like to make more readable (the line marked
with ***):

list<pt
perform_lighting ( const list<pt unlit_layers,
const set<string> & on_lights,
const map<set& precomputed_factors)
{
// BEGIN grouping declarations here to keep the logic readable
list<pt::const_iterator iter;
pt<layer_unlit_0> u;
list<light>::const_iterator iter2;
light l;
list<pt li;
list<pt unlit1;
list<pt::const_iterator iter3;
pt<layer_unlit_1> u2;
map<set::const_iterator norm_factor;
pt<layer_surf> sum;
list<pt ret;
// END

foreach3(u, unlit_layers, iter){

// BEGIN *** li = [ l.surf for l in u->on_and_off_lights
// if contains(on_lights, l.name) ]
li.clear();
foreach3(l, u->on_and_off_lights, iter2){
if (contains(on_lights, l.name) ){
li.push_back(l.surf); }}
// END

u2 = pt<layer_unlit_1>(new layer_unlit_1(li, u->y_walkbehind));
unlit1.push_back(u2);}

norm_factor = precomputed_factors.find(on_lights);

//assert(norm_factor != on_lights.end()); // @@ why doesn't this
compile?

foreach3(u2, unlit1, iter3){
sum = sum_each_pixel( u2->on_lights);
normalize_layer(*sum, norm_factor->second);
ret.push_back( pt<layer_lit>(new layer_lit(sum,
u2->y_walkbehind) )); }
return ret;
}

For reference, here are the auxiliary functions and macros I am using:

#define foreach3(i, l,iter) for((iter) = (l).begin(), i = *iter; \
(iter) != (l).end(); (iter)++, i = *iter)

#define pt shared_ptr // boost::shared_ptr

/// test whether a set of X contains a given X.
template<class Set>
bool
contains(Set s,
typename Set::key_type i)
{
// BEGIN decl
typename Set::const_iterator x;
// END

x = s.find(i);
return x != s.end();
}


Thanks for any hint,

---
Maurizio Colucci
http://onefinger.sf.net
Back to top
Jonathan Mcdougall
Guest





PostPosted: Mon Dec 20, 2004 3:36 pm    Post subject: Re: Can I simulate list comprehensions? Reply with quote



seguso wrote:
Quote:
Hello,

For the sake of efficiency, I have chosen C++ as the language for a
"Free Adventure Construction Kit" I am developing ([url]http://fack.sf.net)[/url].

I am looking for a way to simulate list comprehensions a-la
Haskell/Python, in order to make the logic of my code more readable.
Any way to do that would be welcome, even if this means to use macros.

Here is the code I would like to make more readable (the line marked
with ***):

list<pt perform_lighting ( const list unlit_layers,
const set<string> & on_lights,
const map<set& precomputed_factors)
{

Do you know about typedefs?

Quote:
// BEGIN grouping declarations here to keep the logic readable
list<pt::const_iterator iter;
pt<layer_unlit_0> u;
list<light>::const_iterator iter2;
light l;
list<pt li;
list<pt unlit1;
list<pt::const_iterator iter3;
pt<layer_unlit_1> u2;
map<set::const_iterator norm_factor;
pt<layer_surf> sum;
list<pt ret;
// END

<snip>

Humm.. you think that's readable? To understand the logic, we need to
understand the types of the objects involved and that doesn't help. The
rest of the code is unreadble.

Learn to use typedefs and don't use macros. Programmers are used to
certain constructs, such as for_each and and for loops with iterators.
Hiding them in macros only make the code less readable since we are
stuck with things we don't know about. Showing the definitions does not
help.

Start by rewriting the code using standard constructions, eliminate
macros, use typedefs and fragment your code into smaller functions.
Using typedefs will allow you to merge the variables definitions with
the code, making their usage more clear.


Jonathan

Back to top
Howard
Guest





PostPosted: Mon Dec 20, 2004 3:58 pm    Post subject: Re: Can I simulate list comprehensions? Reply with quote




Quote:

// BEGIN grouping declarations here to keep the logic readable
list<pt::const_iterator iter;
pt<layer_unlit_0> u;
list<light>::const_iterator iter2;
light l;
list<pt li;
list<pt unlit1;
list<pt::const_iterator iter3;
pt<layer_unlit_1> u2;
map<set::const_iterator norm_factor;
pt<layer_surf> sum;
list<pt ret;
// END


One other minor suggestion: better variable names might also help. The
lower-case l (L) is about the worst name for a variable I know. In the font
I use, it's indistinguishable from the upper-case I (i) and also closely
resembles the number one (1). And it has no inherent meaning. Longer names
take a little longer to type, but it's much easier to understand
'lightSource" or "theLight" than simply "l" when looking at code, don't you
think?

-Howard





Back to top
seguso
Guest





PostPosted: Mon Dec 20, 2004 4:18 pm    Post subject: Re: Can I simulate list comprehensions? Reply with quote

Jonathan Mcdougall wrote:
Quote:
seguso wrote:

Hello,

For the sake of efficiency, I have chosen C++ as the language for a
"Free Adventure Construction Kit" I am developing ([url]http://fack.sf.net)[/url].

I am looking for a way to simulate list comprehensions a-la
Haskell/Python, in order to make the logic of my code more readable.
Any way to do that would be welcome, even if this means to use macros.

Here is the code I would like to make more readable (the line marked
with ***):

list<pt perform_lighting ( const list unlit_layers,
const set<string> & on_lights,
const map<set& precomputed_factors)
{


Do you know about typedefs?

// BEGIN grouping declarations here to keep the logic readable
list<pt::const_iterator iter;
pt<layer_unlit_0> u;
list<light>::const_iterator iter2;
light l;
list<pt li;
list<pt unlit1;
list<pt::const_iterator iter3;
pt<layer_unlit_1> u2;
map<set::const_iterator norm_factor;
pt<layer_surf> sum;
list<pt ret;
// END


snip

Humm.. you think that's readable?

No, and it's not meant to be read Smile
I have grouped declarations on top to allow the reader to skip them (see
below).

Quote:
To understand the logic, we need to
understand the types of the objects involved and that doesn't help.

I don't want to argue with you, however IMHO type declarations make the
code heavier to read, while giving little (or no) insight regarding the
meaning of the code --- because the meaning of a variable is the way it
is used, not its type.

For example, knowing that the variable "t" has type "TImeStamp" does not
tell me much about the meaning of t ("The last 10 times the user pressed
a button").


Quote:
The
rest of the code is unreadble.

I guess this is only because you don't understand the macros.

Quote:
Learn to use typedefs and don't use macros.

Thanks for the suggestions, but I don't see the point, since IMHO types
must not be read.

Mau

Back to top
Jonathan Mcdougall
Guest





PostPosted: Mon Dec 20, 2004 4:44 pm    Post subject: Re: Can I simulate list comprehensions? Reply with quote

seguso wrote:
Quote:
Jonathan Mcdougall wrote:

To understand the logic, we need to
understand the types of the objects involved and that doesn't help.


I don't want to argue with you, however IMHO type declarations make the
code heavier to read, while giving little (or no) insight regarding the
meaning of the code --- because the meaning of a variable is the way it
is used, not its type.

For example, knowing that the variable "t" has type "TImeStamp" does not
tell me much about the meaning of t ("The last 10 times the user pressed
a button").

That reminds me of Sutter's example :

int x;
int y;
y = x * 3; // might be okay -- who knows?


typedef int Inches;
typedef int Dollars;

Inches x;
Inches y;
y = x * 3; // hmmm... ?

Quote:
Learn to use typedefs and don't use macros.

Thanks for the suggestions, but I don't see the point, since IMHO types
must not be read.

As you wish.


Jonathan

Back to top
seguso
Guest





PostPosted: Mon Dec 20, 2004 5:00 pm    Post subject: Re: Can I simulate list comprehensions? Reply with quote

Jonathan Mcdougall wrote:
Quote:
seguso wrote:

Jonathan Mcdougall wrote:


To understand the logic, we need to understand the types of the
objects involved and that doesn't help.



I don't want to argue with you, however IMHO type declarations make
the code heavier to read, while giving little (or no) insight
regarding the meaning of the code --- because the meaning of a
variable is the way it is used, not its type.

For example, knowing that the variable "t" has type "TImeStamp" does
not tell me much about the meaning of t ("The last 10 times the user
pressed a button").


That reminds me of Sutter's example :

int x;
int y;
y = x * 3; // might be okay -- who knows?


typedef int Inches;
typedef int Dollars;

Inches x;
Inches y;
y = x * 3; // hmmm... ?


The same effect could be achieved without types, by using variable names
more meaningful than x and y.

BTW, do you know python or ocaml? In those languages it is custom habit
to not write types, and few people claim C++ to be more readable than
those languages.

Cheers,
Mau

Back to top
Jeff Flinn
Guest





PostPosted: Mon Dec 20, 2004 5:14 pm    Post subject: Re: Can I simulate list comprehensions? Reply with quote

seguso wrote:
Quote:
Jonathan Mcdougall wrote:
seguso wrote:

Hello,

For the sake of efficiency, I have chosen C++ as the language for a
"Free Adventure Construction Kit" I am developing
([url]http://fack.sf.net)[/url].


....

Quote:
Humm.. you think that's readable?

No, and it's not meant to be read Smile
I have grouped declarations on top to allow the reader to skip them
(see below).

To understand the logic, we need to
understand the types of the objects involved and that doesn't help.

I don't want to argue with you, however IMHO type declarations make
the code heavier to read, while giving little (or no) insight
regarding the meaning of the code --- because the meaning of a
variable is the way it is used, not its type.

I hate to burst your bubble also, but in (well designed)C++ type is
everything.

Jeff Flinn



Back to top
Jonathan Mcdougall
Guest





PostPosted: Mon Dec 20, 2004 6:23 pm    Post subject: Re: Can I simulate list comprehensions? Reply with quote

seguso wrote:
Quote:
Jonathan Mcdougall wrote:

seguso wrote:


Jonathan Mcdougall wrote:


To understand the logic, we need to understand the types of the
objects involved and that doesn't help.



I don't want to argue with you, however IMHO type declarations make
the code heavier to read, while giving little (or no) insight
regarding the meaning of the code --- because the meaning of a
variable is the way it is used, not its type.

For example, knowing that the variable "t" has type "TImeStamp" does
not tell me much about the meaning of t ("The last 10 times the user
pressed a button").


That reminds me of Sutter's example :

int x;
int y;
y = x * 3; // might be okay -- who knows?


typedef int Inches;
typedef int Dollars;

Inches x;
Inches y;
y = x * 3; // hmmm... ?



The same effect could be achieved without types, by using variable names
more meaningful than x and y.

I agree that a good combination of type and names makes a program readable.

Quote:
BTW, do you know python or ocaml?

I don't.

Quote:
In those languages it is custom habit
to not write types, and few people claim C++ to be more readable than
those languages.

Few people claim C++ is more readable than BASIC also. Why do you think
templates are more complicated to read than non-templated code? Because
suddenly you find yourself with un-typed objects.

Typeless languages (such as most scripting languages) lack the
type-safety of e.g. C++ and make, imho, programs less readable. A type
describes what an object can do much more than its name does. Names can
be ill-chosen but types cannot. Heck, names can even be avoided
sometimes, leaving you only with a type.

Specifically, classes describe an interface and a behavior. A name such
as "students" obviously represents a list os some kind, but its type
will show you 1) what operations are allows on that list and 2) how does
that list behaves. For example,

std::vector<Student> students;

and

std::list<Student> students;

may share some functions, but will behave differently and will be used
in different contexts.

There is much more to a variable than its name!


Jonathan

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.