add sources

This commit is contained in:
Alexander Zhirov 2023-02-26 01:19:12 +03:00
parent fcd25eea52
commit 7ce631a648
21 changed files with 548 additions and 2 deletions

View file

@ -0,0 +1,34 @@
import std.stdio;
T[] find(alias pred, T)(T[] input)
if (is(typeof(pred(input[0])) == bool))
{
for (; input.length > 0; input = input[1 .. $])
{
if (pred(input[0])) break;
}
return input;
}
unittest
{
int zero = 0;
bool isTypeLow(int x)
{
return x < zero;
}
static bool isTypeBig(int x)
{
// Из-за static zero не будет видно внутри функции
return x > zero;
}
int[] a = [ 1, 2, 3, 4, -5, 3, -4 ];
// Найти первое отрицательное число
auto b = find!(isTypeLow)(a).dup;
writeln(b);
auto c = find!(isTypeBig)(b);
writeln(c);
}