sortUniq
zeljkog via Digitalmars-d
digitalmars-d at puremagic.com
Sat Jan 24 07:17:22 PST 2015
On 24.01.15 15:53, Andrei Alexandrescu wrote:
> On 1/24/15 4:13 AM, zeljkog wrote:
>> On 24.01.15 12:50, Peter Alexander wrote:
>>> auto f = unique();
>>> [1, 5, 5, 2, 1, 5, 6, 6].filter!(f).writeln; // [1, 5, 2, 6]
>>>
>>> Filter needs an alias, and you cannot alias an R-value (it has no
>>> symbol).
>>
>> Yes, I see :)
>>
>> But I think this should be supported by std.algorithm:
>>
>> import std.stdio, std.algorithm;
>>
>> struct Uniq{
>> bool[int] c;
>> bool opCall(int a){
>> if (a in c)
>> return false;
>> else{
>> c[a] = true;
>> return true;
>> }
>> }
>> }
>>
>> void main()
>> {
>> [1, 5, 5, 2, 1, 5, 6, 6].filter!Uniq.writeln;
>> }
>
> I can't make sense of this - where is Uniq supposed to be instantiated?
> -- Andrei
>
Require following changes in std.algorithm.filter.
original:
template filter(alias predicate) if (is(typeof(unaryFun!predicate)))
{
auto filter(Range)(Range range) if (isInputRange!(Unqual!Range))
{
return FilterResult!(unaryFun!predicate, Range)(range);
}
}
private struct FilterResult(alias pred, Range)
...
changed (to run this case):
template filter(alias predicate) if (is(typeof(unaryFun!predicate)))
{
auto filter(Range)(Range range) if (isInputRange!(Unqual!Range))
{
static if (is(predicate == struct))
return FilterResult!(predicate, Range)(range);
else
return FilterResult!(unaryFun!predicate, Range)(range);
}
}
private struct FilterResult(alias predicate, Range)
{
static if (is(predicate == struct))
predicate pred;
else
alias pred = predicate;
...
More information about the Digitalmars-d
mailing list