SList/DList ranges

Jonathan M Davis jmdavisProg at gmx.com
Mon Nov 26 23:50:21 PST 2012


On Monday, November 26, 2012 19:49:51 Zhenya wrote:
> Hi!
> I read the spec,but I didn't find any function,that removes
> concrete element from
> list.I am not familiar with D's ranges,so could you help me
> please?

What do you mean by removing concrete elements? How is that any different from 
any other element in the list? If you want to remove elements from the list, 
then use one of the *remove functions. [] on the list to get a range over all 
of the elements in the list, find to find the element that you want, and then 
take to get a range with just the elements from the front of the range that 
you want to remove. Hopefully this will enlighten you somewhat:

import std.algorithm;
import std.container;
import std.range;
import std.stdio;

void main()
{
    auto list = make!(SList!int)(4, 5, 6, 7, 22, 9, 5, 4);
    assert(equal(list[], [4, 5, 6, 7, 22, 9, 5, 4]));
    auto found = find(list[], 6);
    assert(equal(found.save, [6, 7, 22, 9, 5, 4]));
    list.linearRemove(take(found, 3));
    assert(equal(list[], [4, 5, 9, 5, 4]));
    list.linearRemove(take(find(list[], 5), 1));
    assert(equal(list[], [4, 9, 5, 4]));
}

Unfortunately, std.container needs a fair bit of work in terms of some of the 
details - particularly with its functions which accept and/or return ranges, 
so it doesn't always work like it should yet. It's very much a work in 
progress right now. But the above should give you the basic idea.

As for ranges in general, the best resource on them at this point would be 
this chapter from an online book on D:

http://ddili.org/ders/d.en/ranges.html

If you're going to be using Phobos much (which is likely if you're using D), 
then you're going to need to understand ranges, because Phobos uses them quite 
heavily.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list