News for hackers. Stuff that matters.
Re:List Processing in C by alexgartrell on Tuesday December 21, 2010 @04:44AM (#2027408)
Rather than

  #define LIST_FOREACH(list, index, value, code) 
I'd prefer

  #define LIST_FOREACH(list, index, value) \
  for(index = 0, value = list->items[0]; \
      index < list->size; \
      index++, value=list->items[index])
It lets you treat a LIST_FOREACH(...) as a normal for(...) in terms of the other syntax.

edit: I did some looking around and determined that the following is probably BS

[1] If you're wondering why I didn't do

  index++, value = list->items[index]
it's because expressions separated by a comma can be evaluated in any order (I believe). My friend ran up against it with a bug in jscore.
Re:List Processing in C by dzorz on Tuesday December 21, 2010 @04:55AM (#2027417)
> it's because expressions separated by a comma can be evaluated in any order (I believe).

Expressions separated by the comma operator are always evaluated left to right. On the other hand function arguments (which are separated by comma) can be evaluated in any order.

Re:List Processing in C by jpr on Tuesday December 21, 2010 @05:04AM (#2027423)
> Expressions separated by the comma operator are always evaluated left to right.

That's correct (for C), but I feel the need to add that in function calls the parameters are separated by commas, and they can be evaluated in any order.

Re:List Processing in C by mfukar on Tuesday December 21, 2010 @05:10AM (#2027430)
In the contexts of a function declaration/definition/call, comma is not acting as an operator.
Re:List Processing in C by route66 on Tuesday December 21, 2010 @05:26AM (#2027449)
Your preference looks a lot like the linux-kernel list.h.

(e.g. http://isis.poly.edu/kulesh/stuff/src/klist/).

Requiring idiosyncratic setup of your list struct though.

Re:List Processing in C by alexgartrell on Tuesday December 21, 2010 @12:59PM (#2028635)
It's certainly inspired by list.h, but I used the same struct as the original. I just think it's crufty to pass "code" as a parameter to a function (in that I've ever really seen it done well).

Of course, I'm talking only about C, as I love to overuse lambda in Python like it's my job

Re:List Processing in C by vog on Tuesday December 21, 2010 @06:41AM (#2027539)
Why don't you simply replace

    index++, value=list->items[index]
with

    value=list->items[++index]

?
Re:List Processing in C by alexgartrell on Tuesday December 21, 2010 @12:57PM (#2028624)
I did initially, until I looked up [1] and changed it in the edit. I figure separating it is cleaner.

[1] http://en.wikipedia.org/wiki/Comma_operator

Re:List Processing in C by vog on Wednesday December 22, 2010 @09:22AM (#2031358)
But the combined expression solves exactly your problem!

You were not sure whether the increment would always happen before the assignment. In the combined expression you can simply use the pre-increment operator, which will always increment before the rest of the expression.

It's shorter, unambiguous and leaves less questions. How can something be any "cleaner" than that?

Also, the statements "array[index++]" and "array[++index]" are pretty well-understood idioms in C.

Re:List Processing in C by alexgartrell on Tuesday December 21, 2010 @12:56PM (#2028622)
The following comment adds no value to the discussion, but I'm kind of childish and don't want to look like a dumbass in front of the cool kids :)

You'll notice that there's a footnote that isn't actually related to anything in the body of the comment. It's because the footnote was wrong; I just felt like it would be dishonest to hide it in the edit. (this is also the reason the edit: I did some looking around...) portion exists.

Interestingly, the edit happened prior to any child posts on the comment, and so was not really a reaction to being corrected. I'd actually googled and found [1], and realized (as someone else said) that function calls don't use the comma as an operator.

tl;dr; never, EVER expect left-to-right evaluation of arguments to a function :)

[1] http://en.wikipedia.org/wiki/Comma_operator