Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

foreache

Name: Anonymous 2014-01-20 18:58

what is you're opinion on foreache loops?

i don't like em cuz they ache in the fore

Name: Anonymous 2014-01-20 19:22

Pure syntactic sugar*. Use them when there is a need, don't bitch if they aren't there. Not to be used as a blanket replacement for for unless you've got a damn good reason.

* I guess they could be an efficiency gain for a language that doesn't have pointer arithmetic exposed, but does have C-array-like-structures.

Name: Anonymous 2014-01-20 21:05

I like foreach.

Name: Anonymous 2014-01-20 21:19

Almost all the times they do what they're supposed to do and I like that.

Name: Anonymous 2014-01-20 21:38

I foreach when I want to Map.

Name: Anonymous 2014-01-20 21:58

I made a FOREACH macro in C for a linked list data type I implemented. It's useful.

Name: Anonymous 2014-01-20 21:59

foreach is a control construct map is a function.

Control constructs do not return values, functions do. Operational vs denotational etc. etc.

Name: Anonymous 2014-01-20 22:23

>>7
Control constructs mutate state; map returns a value. I use one for imperative programming, one for functional.

They both comb over some collection.

Name: Anonymous 2014-01-20 23:30

>>8
You can use ``function'' calls in imperative programs too.

Name: Anonymous 2014-01-21 2:38

Take aspirins for all those foreaches

Name: Anonymous 2014-01-21 20:57

>>2

not even close. what we are really talking about is iteration by enumeration (i.e. internal iteration) vs iteration by stream (i.e. external iteration). the two are provably isomorphic, but differ a lot in semantics and implementation

internal iteration is imo the superior choice - it is easier to read, easier to reason about (for data structures that aren't lists), requires less state (i.e. obtaining and maintaining an iterator, deallocating resources), and has predictable resource utilization (when iterating over items requires file/db access)

it is a lot harder (but not impossible) to stop iteration before exhaustion and do things like interleave iterators, but i find the other stuff outweighs this

Name: Anonymous 2014-01-21 21:29

>>11
Iteration by stream? What are you talking about? How does:

int pointless;
for (int i = 0; i % 13 != 0; i += 3) {
pointless = i;
}

have anything to do with streams? It's a control construct.

And why are you talking about exhaustion? and interleaving iterators? You can have variations of foreach with something like break and return or a "Knuth style" mutli-body foreach construct with by clauses.

Name: Anonymous 2014-01-21 21:33

>>12

For each (or foreach) is a computer language idiom for traversing items in a collection. Foreach is usually used in place of a standard for statement. Unlike other for loop constructs, however, foreach loops [1] usually maintain no explicit counter: they essentially say "do this to everything in this set", rather than "do this x times". This avoids potential off-by-one errors and makes code simpler to read. In object-oriented languages an iterator, even if implicit, is often used as the means of traversal.

foreach is a way of iterating over a collection. obviously i am not comparing this with loops, but loops that also iterate over a collection, which are streams

Name: Anonymous 2014-01-21 21:39

also, foreach doesn't have to be a language primitive - you're right that things like break and return create a sort of hybrid iterator, but in some languages foreach (or equivalent) just takes a collection and a function.

Name: Anonymous 2014-01-21 21:43

>>13
Fascinating. Give me an example of a ``loop'' which is also a ``stream''.

You can combine a loop with another control construct (say yield) and you'll get a generator, which is a stream, but not a loop.

Name: Anonymous 2014-01-21 21:50


for (it = begin(collection); it.has_next(); it.advance())


obviously the for loop itself isn't the stream, the iterator is the stream, the for loop is just the consumer of the stream - but this is typically the one and only consumer for it so it's just useful to think of them together

Name: Anonymous 2014-01-21 21:59

also i hope this forum has some kind of filter to transform "quotes" into ``quotes'' and you idiots aren't just doing this intentionally

Name: Anonymous 2014-01-21 21:59

>>16
obviously the for loop itself isn't the stream, the iterator is the stream, the for loop is just the consumer of the stream
Yes. obviously.

Now think about how terribly your first post characterizes differences between foreach and other control constructs.

Name: Anonymous 2014-01-21 22:02

>>17
Write an infix language which expresses grouping with ( and ( instead of ) and ).

Name: Anonymous 2014-01-21 22:04

>>19

Doh! I meant ``( and ( instead of ( and ).''

) (I just can't stand unbalanced parentheses!).

Name: Anonymous 2014-01-21 22:07

>>18

i characterized the differences between internal and external iterators, you should read it again

Name: Anonymous 2014-01-21 22:27

People who make foreach macros in C should be shot.

Name: Anonymous 2014-01-21 22:28

>>22
Why?

Name: Anonymous 2014-01-22 4:02

>>22,23
#define foreach(e, i, a) \
for (i = 0; e = (a)[i], i < (sizeof(a) / sizeof(*(a))); ++i)

Name: Anonymous 2014-01-22 18:47

>>23
You are imitating features of non-C languages and hacking them into C. Chances are youre also going to want to use that macro more often and so youll use linked lists where they dont belong and the whole thing is such a small macro that doesnt show up often and it begs the question why is it necessary when the real way of writing will be clearer?

Name: Anonymous 2014-01-23 0:28

>>25 ``begs the question'' doesn't mean what you think it means.

Name: Anonymous 2014-01-23 9:47

>>25
How do you feel about http://sglib.sourceforge.net/?

Name: Anonymous 2014-01-23 22:06

>>27
I'm not him, but I'm having feelings of mild disgust. It's only mild, so it could be just an unfounded instinctual reaction, but still - a reaction. And all I did was read half of their intro page.

Name: Anonymous 2014-01-23 22:31

>>27

As ugly as sin.

Name: Anonymous 2014-01-23 22:43

>>28,29
Why? I'm genuinely curios why you think a fairly conservative addition to the expressiveness of a really unexpressive language from within that language is ``ugly'' or ``digusting''?

Name: Anonymous 2014-01-23 23:49

>>30

(>>28 here)
To begin with, I absolutely detest your use of the term ``expressiveness'', since its definition is so approximate and situation-dependent that it might as well be ``prettiness''. Clearly it is not the case that one is unable to write a Red-Black tree in C, so adding a library for Red-Black trees doesn't add any features to C. It's just that you don't have to look at rbtree.c when you scan your source directory. To reduce ad absurdum, you might as well just write your entire program, turn it into a library, and then state that you have improved upon the expressive power of C because now
#include <super/special.h>
int main(void)
{
return yay();
}

is a fully-featured word processor in something like 75 bytes!

No. You have not changed the fundamental nature of a language, you are including some extra header files and linking against a library.

Anyway:

Firstly (and admittedly pettily), one thing I noticed as I haphazardly scanned Sglib's description was a statement that all of Sglib's variables begin and end with an underscore, so it's okay for them to use variables in their macros. For a library with the intent of being truly general-purpose, that's a [i]bad[/i] idea (N1124, which I have on hand, brings this up in section 7.1.3, but I know the point is repeated in other standards), and the author[s] should know this.

Secondly, the writer of this page came off as a bit of a crank. See
In opposition to many cake eaters from my university, I believe that ideas behind Sglib are good because of my former research on modern (and generic) programming languages and because of my current work on a refactoring browser for C.
A page about a data structure ``library''* isn't the place to put snide comments about former classmates, well-deserved though that comment may be. That's not a condemnation of the code, but given the quality of other writings/projects I've seen from authors who made such comments, I was reluctant to trust the vague technical claims that this author makes. Still, this would not be significant in my overall choice to use the library, were I to make one.

I would say ``Thirdly, the lack of a proper license is a bit worrying - if it's so simply licensed, the author should just put it under MIT/BSD/etc. and be done with it.'' if I cared about that, but I really don't. Somebody else might say it, though.

Thirdly, and this is the only reason that I would really end up making a decision based on (and note that I did not come to this conclusion from the initial overview page), the performance seems bizarre. There's an admirable focus on performance by offloading as much as possible to the preprocessor, but consider SGLIB_LIST_LEN. Linear time? What the fuck? If that's the price I have to pay for not linking against anything, no thank you.

* I use those quotes because I don't consider Sglib a library on technical grounds, and I'm not sure what to call a piece of off-the-shelf code that is not linked against.

Name: Anonymous 2014-01-23 23:56

>>31
SGLIB_LIST_LEN. Linear time? What the fuck? If that's the price I have to pay for not linking against anything, no thank you.

How can you have a linked list length function that isn't linear time?

Name: Anonymous 2014-01-24 0:22

>>32
Is this a trick question? Store the length and update it when add/delete occurs.

Name: Anonymous 2014-01-24 0:40

>>33
Where do you store it?

What happens when e.g. I assign the nth tail to some variable, and then append another list?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List