Перенос страниц
This commit is contained in:
parent
4d57446057
commit
4c954c9186
129 changed files with 14 additions and 15 deletions
|
@ -0,0 +1,40 @@
|
|||
// import std.range;
|
||||
|
||||
// V reduce(alias fun, V, R)(V x, R range)
|
||||
// if (isInputRange!R && is(typeof(x = fun(x, range.front))))
|
||||
// {
|
||||
// for (; !range.empty; range.popFront())
|
||||
// {
|
||||
// x = fun(x, range.front);
|
||||
// }
|
||||
|
||||
// return x;
|
||||
// }
|
||||
|
||||
@property bool empty(T)(T[] a) { return a.length == 0; }
|
||||
@property ref T front(T)(T[] a) { return a[0]; }
|
||||
void popFront(T)(ref T[] a) { a = a[1 .. $]; }
|
||||
|
||||
V reduce(alias fun, V, R)(V x, R range)
|
||||
if (is(typeof(x = fun(x, range.front)))
|
||||
&& is(typeof(range.empty) == bool)
|
||||
&& is(typeof(range.popFront())))
|
||||
{
|
||||
for (; !range.empty; range.popFront())
|
||||
{
|
||||
x = fun(x, range.front);
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
unittest
|
||||
{
|
||||
int[] r = [ 10, 14, 3, 5, 23 ];
|
||||
// Вычислить сумму всех элементов
|
||||
int sum = reduce!((a, b) { return a + b; })(0, r);
|
||||
assert(sum == 55);
|
||||
// Вычислить минимум
|
||||
int min = reduce!((a, b) { return a < b ? a : b; })(r[0], r);
|
||||
assert(min == 3);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue