www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Recurrence

reply n00b <n00b nospam.com> writes:
Hello.

------
MyType increment(MyType previous)
{
	//snip
}

auto myRec = std.range.recurrence!increment(initialMyType);
-----

... doesn't work. What am I doing wrong? The documentation doesn't seem 
to imply the function has to be passed as a string, if that's the issue.
Feb 19 2013
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Tuesday, 19 February 2013 at 10:37:31 UTC, n00b wrote:
 Hello.

 ------
 MyType increment(MyType previous)
 {
 	//snip
 }

 auto myRec = std.range.recurrence!increment(initialMyType);
 -----

 ... doesn't work. What am I doing wrong? The documentation 
 doesn't seem to imply the function has to be passed as a 
 string, if that's the issue.
It is not very well documented (or should I say, documented at all...), but the signature of a recurrence function must take the form: "auto recurrence(R)(R r, size_t n);" Where: n is the current index being calculated and: r is a range that holds the values of your recurrence, and is valid for the indices "n - 1" to "n - numberOfInitialArgs". The type of R is implementation defined. (It's a "Cycle!(T[Args.length])" for efficiency reasons, fyi, but this is an implementation detail). For example, fibs and ! would be implemented as such: //---- import std.stdio; import std.range; int fibFun(R)(R r, size_t n) { return r[n - 1] + r[n - 2]; } int expFun(R)(R r, size_t n) { return n * r[n - 1]; } void main() { //auto myRec = std.range.recurrence!(increment)('a', 'b'); auto fib = std.range.recurrence!fibFun(1, 1); auto exp = std.range.recurrence!expFun(1); writeln(fib.take(10)); writeln(exp.take(10)); } //---- [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880] //----
Feb 19 2013
parent n00b <n00b nospam.com> writes:
Le 19/02/2013 07:09, monarch_dodra a écrit :
 On Tuesday, 19 February 2013 at 10:37:31 UTC, n00b wrote:
 Hello.

 ------
 MyType increment(MyType previous)
 {
 //snip
 }

 auto myRec = std.range.recurrence!increment(initialMyType);
 -----

 ... doesn't work. What am I doing wrong? The documentation doesn't
 seem to imply the function has to be passed as a string, if that's the
 issue.
It is not very well documented (or should I say, documented at all...), but the signature of a recurrence function must take the form: "auto recurrence(R)(R r, size_t n);" Where: n is the current index being calculated and: r is a range that holds the values of your recurrence, and is valid for the indices "n - 1" to "n - numberOfInitialArgs". The type of R is implementation defined. (It's a "Cycle!(T[Args.length])" for efficiency reasons, fyi, but this is an implementation detail). For example, fibs and ! would be implemented as such: //---- import std.stdio; import std.range; int fibFun(R)(R r, size_t n) { return r[n - 1] + r[n - 2]; } int expFun(R)(R r, size_t n) { return n * r[n - 1]; } void main() { //auto myRec = std.range.recurrence!(increment)('a', 'b'); auto fib = std.range.recurrence!fibFun(1, 1); auto exp = std.range.recurrence!expFun(1); writeln(fib.take(10)); writeln(exp.take(10)); } //---- [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880] //----
Thanks a lot. I reported the lack of documentation to bugzilla, I hope I did it right...^^
Feb 19 2013