Just one time

Andrea Fontana via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Oct 21 00:59:46 PDT 2015


On Tuesday, 20 October 2015 at 18:08:33 UTC, Ali Çehreli wrote:
> On 10/20/2015 08:48 AM, Andrea Fontana wrote:
>> It happens I need to perform an operation just one time 
>> (inside a
>> function, a loop...)
>
> An idea that uses a function pointer where the first step does 
> its task and then sets the stage for the following steps:
>
> import std.stdio;
> import std.range;
> import std.algorithm;
>
> void firstStep() {
>     writeln("First call!");
>     takeAStep = &followingSteps;
> }
>
> void followingSteps() {
>     writeln("Just another call");
> }
>
> auto takeAStep = &firstStep;
>
> void main() {
>     3.iota.each!(_ => takeAStep());
> }
>
> First call!
> Just another call
> Just another call
>
> Ali

Nice idea, but I can't "reuse" it :)


More information about the Digitalmars-d-learn mailing list