Skip to content

Commit

Permalink
Issue 9535: Add more examples to std.range.sequence.
Browse files Browse the repository at this point in the history
Demonstrate expected arguments for lambda / explicit function variants.

Demonstrate random access(!).
  • Loading branch information
H. S. Teoh committed Sep 23, 2014
1 parent e4e2739 commit 7914fa3
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion std/range.d
Expand Up @@ -5725,7 +5725,7 @@ auto sequence(alias fun, State...)(State args)
return Return(tuple(args));
}

///
/// Odd numbers, using function in string form:
unittest
{
auto odds = sequence!("a[0] + n * a[1]")(1, 2);
Expand All @@ -5736,6 +5736,42 @@ unittest
assert(odds.front == 5);
}

/// Triangular numbers, using function in lambda form:
unittest
{
auto tri = sequence!((a,n) => n*(n+1)/2)();

// Note random access
assert(tri[0] == 0);
assert(tri[3] == 6);
assert(tri[1] == 1);
assert(tri[4] == 10);
assert(tri[2] == 3);
}

/// Fibonacci numbers, using function in explicit form:
unittest
{
import std.math : pow, round, sqrt;
static ulong computeFib(S)(S state, size_t n)
{
// Binet's formula
return cast(ulong)(round((pow(state[0], n+1) - pow(state[1], n+1)) /
state[2]));
}
auto fib = sequence!computeFib(
(1.0 + sqrt(5.0)) / 2.0, // Golden Ratio
(1.0 - sqrt(5.0)) / 2.0, // Conjugate of Golden Ratio
sqrt(5.0));

// Note random access with [] operator
assert(fib[1] == 1);
assert(fib[4] == 5);
assert(fib[3] == 3);
assert(fib[2] == 2);
assert(fib[9] == 55);
}

unittest
{
import std.typecons : Tuple, tuple;
Expand Down

0 comments on commit 7914fa3

Please sign in to comment.