std.algorithm missing for_each?

Jens Mueller jens.k.mueller at gmx.de
Wed Feb 5 01:37:58 PST 2014


Dear lovely D community,

recently I refactored some code into component style (see Component
Programming in D by Walter
http://www.drdobbs.com/architecture-and-design/component-programming-in-d/240008321)

It looks about like this

someInputRange
.filter!()
.map!()

Next I want to discard all elements but perform some last operation on
each element. The problem is that map forces me to pass a function that
returns. Of course I could return a fake value. But that doesn't look
proper. Another option is to iterate using the foreach loop. Does not
look better either, does it?

This makes me believe that std.algorithm misses an algorithm. The
for_each algorithm (see for_each in STL
http://www.cplusplus.com/reference/algorithm/for_each/).

To rephrase the problem more general (because maybe I'm just not seeing
how to fit the pieces together): How do you perform some arbitrary
operation on the elements of a range?

myRange
.each!((e)
{
	e.someOperation();
});

Jens


More information about the Digitalmars-d mailing list