How to get type returned by e.g. std.algorithm.iteration.filter
Christian Köstlin
christian.koestlin at gmail.com
Sun May 19 20:40:16 UTC 2019
On 19.05.19 20:38, Jacob Carlborg wrote:
> On 2019-05-19 15:36, Christian Köstlin wrote:
>
>> Unfortunately I have no idea how to even store the result of this
>> search in an attribute of ByMinimum, as I cannot writeout its type.
>
> In general you can use `typeof(<expression>)`, where `<expression>` is
> the expression you want to get the type of.
>
Thanks for the hint. The best I could come up with this is:
#!/usr/bin/env rdmd
import std.stdio;
import std.algorithm;
import std.typecons;
import std.array;
import std.range;
import std.traits;
auto byMinimum(Ranges)(Ranges ranges)
{
auto getNonEmpty()
{
return ranges.filter!("!a.empty");
}
auto minimumOfRanges(Ranges)(Ranges ranges) {
// dfmt off
return ranges
.map!(range => tuple!("range", "line")(range, range.front))
.minElement!("a.line");
// dfmt on
}
auto nonEmpty = getNonEmpty;
ReturnType!(minimumOfRanges!(typeof(nonEmpty))) minRangeAndLine;
struct ByMinimum(Ranges)
{
bool empty()
{
return nonEmpty.empty;
}
auto front()
{
minRangeAndLine = minimumOfRanges(nonEmpty);
return minRangeAndLine;
}
void popFront()
{
minRangeAndLine.range.popFront;
nonEmpty = getNonEmpty;
}
}
return ByMinimum!(Ranges)();
}
void main(string[] files)
{
foreach (n; files[1 .. $].map!(name =>
File(name).byLine(No.keepTerminator)).array.byMinimum)
{
writeln(n.line);
}
}
Still it looks a little clumsy. Any ideas?
--
Christian
More information about the Digitalmars-d-learn
mailing list