June 30, 2018
On Saturday, 30 June 2018 at 00:16:49 UTC, Basile B. wrote:
> On Friday, 29 June 2018 at 16:44:36 UTC, Robert M. Münch wrote:
>> I hope this is understandable... I have:
>>
>> class C {
>> 	void A();
>> 	void B();
>> 	void C();
>> }
>>
>> I'm iterating over a set of objects of class C like:
>>
>> foreach(obj; my_selected_objs){
>> 	...
>> }
>>
>> The iteration and code before/afterwards always looks the same, I need this iteration for many of the memember functions like C.A() and C.B(), etc.
>>
>> foreach(obj; my_selected_objs){
>> 	...
>> 	obj.A|B|C()
>> 	...
>> }
>>
>> So, how can I write a generic handler that does the iteration, where I can specify which member function to call?
>>
>> void do_A() {
>> 	handler(C.A()); ???
>> }
>>
>> void do_B() {
>> 	handler(C.B()); ???
>> }
>>
>> handler(???){
>> 	foreach(obj: my_selected_objs){
>> 		???
>> 	}
>> }
>>
>> Viele Grüsse.
>
> Using opDispatch we can manage to get a voldemort able to resolve the member func A, B or C etc.
>
> ---
> import std.stdio;
>
> class C
> {
>     void A(){writeln(__FUNCTION__);}
>     void B(int i){writeln(__FUNCTION__, " ", i);}
> }
>
> auto handler(T)(T t)
> {
>     struct Handler
>     {
>         auto opDispatch(string member, Args...)(Args args)
>         {
>             import std.algorithm.iteration : each;
>             mixin( `t.each!(a => a.` ~ member ~ `(args));` );
>         }
>     }
>     Handler h;
>     return h;
> }
>
> void main()
> {
>     auto cs = [new C(), new C()];
>     handler(cs).A();
>     cs.handler.B(42); // UFCS style
> }
> ---
>
> which results a very natural expression.

insert "in" at the right place.
June 30, 2018
On Friday, 29 June 2018 at 20:23:47 UTC, Timoses wrote:
> void handler(alias func, T)(T[] ts) {
>     ....
> }

Btw this is pretty much std.algorithm.each

import std.algorithm;

void main() {
  auto cs = [ new C(), new C() ];
  cs.each!(o => o.A());
}

https://dlang.org/phobos/std_algorithm_iteration.html#.each


July 01, 2018
On 2018-06-30 22:53:47 +0000, Jerry said:

> Btw this is pretty much std.algorithm.each
> 
> import std.algorithm;
> 
> void main() {
>    auto cs = [ new C(), new C() ];
>    cs.each!(o => o.A());
> }
> 
> https://dlang.org/phobos/std_algorithm_iteration.html#.each

The looping needs to be done in the handler because there are two loops running one after the other and the range to loop over is detected in the handler too. Otherwise a lot of code duplication would happen.

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster

July 01, 2018
On Sunday, 1 July 2018 at 06:55:35 UTC, Robert M. Münch wrote:
> On 2018-06-30 22:53:47 +0000, Jerry said:
>
>> Btw this is pretty much std.algorithm.each
>> 
>> import std.algorithm;
>> 
>> void main() {
>>    auto cs = [ new C(), new C() ];
>>    cs.each!(o => o.A());
>> }
>> 
>> https://dlang.org/phobos/std_algorithm_iteration.html#.each
>
> The looping needs to be done in the handler because there are two loops running one after the other

Look at my solution then:

https://forum.dlang.org/post/kmkckipiwlvwahifelnc@forum.dlang.org

> and the range to loop over is detected in the handler too. Otherwise a lot of code duplication would happen.


July 01, 2018
On Sunday, 1 July 2018 at 06:55:35 UTC, Robert M. Münch wrote:
>
> The looping needs to be done in the handler because there are two loops running one after the other and the range to loop over is detected in the handler too. Otherwise a lot of code duplication would happen.

Maybe an application for filter?

C[] cs = ...;
cs.filter!((c) => <predicate to be fulfilled>)
  .each!((c) => c.a())
1 2
Next ›   Last »