dlang-book/book/05-данные-и-функции-функциональный-стиль/src/chapter-5-9-1/app.d

21 lines
471 B
D

import std.stdio;
@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 .. $]; }
R find(R, T)(R haystack, T needle)
if (is(typeof(haystack.front != needle) == bool))
{
while (!haystack.empty && haystack.front != needle)
{
haystack.popFront();
}
return haystack;
}
unittest
{
writeln(find([1, 2, 3], 2.0));
assert(find([1, 2, 3], 1.0));
}