digitalmars.D.learn - Good intuitive list patterns
```d
import std;
struct intutisive(alias Array,discrim...){
static Array array; alias array this;
//this(){
// array~=typeof(array[0]);
//}
auto opIndex(I)(I i){
static struct handle{
I i;
ref get()=>array[i]; alias get this;
string toString()=>get.to!string;
auto children(){
static struct kidhandle{
I i;
ref front()=>array[i];
void popFront(){i=front.sib;}
bool empty()=>i==0;
//void opOpAssign(string op:"~")(handle a){//I dont think
this works without a parent pointer
// a.sib=front.sib;
// front.sib=a.i;
//}
}
return kidhandle(get.kid);
}
void addchild(handle a){
a.sib=get.kid;
get.kid=cast(typeof(get.kid))a.i;
}
}
return handle(i);
}
auto length()=>array.length-1;
auto opSlice()=>iota(1,array.length).map!(i=>opIndex(i));
}
struct data{
string name;
ubyte kid;
ubyte sib;
}
intutisive!(data[]) foo;
unittest{
foo~=data("");
foo~=data("hi");
assert(foo[1].children.empty);
foo~=data("bye");
foo[1].kid=2;
assert(foo[1].children.front.name=="bye");
foo[].writeln;
//---
foo~=data("cow");
foo~=data("moo");
foo[].filter!(a=>a.name=="cow").front.addchild(
foo[].filter!(a=>a.name=="moo").front );
foo[].writeln;
}
```
https://www.youtube.com/watch?v=ShSGHb65f3M
I feel like im missing something. Maybe it cant fit cleanly into
ranges but idk.
Feb 21
On Saturday, 21 February 2026 at 09:25:20 UTC, monkyyy wrote:there are three datatype familys: handles, elements, datastructures Handles are a int-ish index + a mixin of a datastructure. Elements are structs that need to contain a handle. Datastructures are lists of elements, importantly []'s default operations must be worked around. I see 2 problems to solve: a) this is naively recursive so you must break the recursion(handles only get strings and may only contain lazy templates) b) some data structures maybe naive that its part of an intrusive.. typethoery(?) assume naive datastructures act like []'s: 1. &[0] .. &[$-1] is owned by the datastructure and is entirely continuous 2. ~= then [$-1] creates an element and gets you the element knowledgeable datastuctures implement: 1. add(element constructor arguments) returns a handle (~= is broken by slices) 2. (enum) isvalidtype(T) returns if T can even exist in the datastructure 3. getindex(ref T) returns a valid index for T, if you want safety do it here elements implement: 1. alias handle 2. maybe uses the keywords: kid,sib,mom if you want my ranges handles impliment: 1. this(ref T) 2. this(I) 3. opAssign(ref T) 4. opAssign(handle) --- is that all expandable and the easiest thing to implement?
Feb 25








monkyyy <crazymonkyyy gmail.com>