www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Just one time

reply Andrea Fontana <nospam example.com> writes:
It happens I need to perform an operation just one time (inside a 
function, a loop...)

I wonder if doing this it's a good idea or not.

bool isFirstTime(alias T)()
{
	static val = true;

	if (val)
	{
		val = false;
		return true;
	}

	return false;
}

So:

void my_func()
{
	if (isFirstTime!my_func) writeln("First call!");
	else writeln("Just another call");
}

myfunc(); // Print First call!
myfunc(); // Print Just another call;

Or (for example inside a for loop):

assert(isFirstTime!"blah" == true);
assert(isFirstTime!"blah" == false);

Does it work as expected?
Oct 20 2015
next sibling parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Tuesday, 20 October 2015 at 15:48:47 UTC, Andrea Fontana wrote:
 It happens I need to perform an operation just one time (inside 
 a function, a loop...)

 I wonder if doing this it's a good idea or not.

 bool isFirstTime(alias T)()
 {
 	static val = true;

 	if (val)
 	{
 		val = false;
 		return true;
 	}

 	return false;
 }

 So:

 void my_func()
 {
 	if (isFirstTime!my_func) writeln("First call!");
 	else writeln("Just another call");
 }

 myfunc(); // Print First call!
 myfunc(); // Print Just another call;

 Or (for example inside a for loop):

 assert(isFirstTime!"blah" == true);
 assert(isFirstTime!"blah" == false);

 Does it work as expected?
I think that's ok, because each unique (i.e. with a different parameter) instantiation of isFirstTime will have its own static val. Be aware that there will be one instance of val per thread, so you are detecting the first run in each thread, not in the program overall.
Oct 20 2015
parent reply Andrea Fontana <nospam example.com> writes:
On Tuesday, 20 October 2015 at 15:55:47 UTC, John Colvin wrote:
 Be aware that there will be one instance of val per thread, so 
 you are detecting the first run in each thread, not in the 
 program overall.
This is the kind of thing I'm interested in. What happens if I call isFirstTime!"test" across different modules? Does instatiation works in the same way for all compilers/platform? Other things to consider?
Oct 20 2015
parent John Colvin <john.loughran.colvin gmail.com> writes:
On Tuesday, 20 October 2015 at 16:01:58 UTC, Andrea Fontana wrote:
 On Tuesday, 20 October 2015 at 15:55:47 UTC, John Colvin wrote:
 Be aware that there will be one instance of val per thread, so 
 you are detecting the first run in each thread, not in the 
 program overall.
This is the kind of thing I'm interested in. What happens if I call isFirstTime!"test" across different modules? Does instatiation works in the same way for all compilers/platform? Other things to consider?
It should work exactly the same across modules, just one instance per thread. Same on all platforms and compilers.
Oct 20 2015
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Oct 20 2015
parent Andrea Fontana <nospam example.com> writes:
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 :)
Oct 21 2015