Skip to content

indexable range support #101

Description

@crazymonkyyy
opend -unittest -main -run foo.d
--- foo.d
import std;
auto indexs(T,int N)(ref T[N] a)=>iota(0,N);
auto iteratebyindex(A)(ref A a){
	alias R=typeof(a.indexs);
	struct range{
		A* a;
		R r;
		auto ref front()=>(*a)[r.front];
		auto ref index()=>r.front;
		void popFront()()=>r.popFront;
		bool empty()=>r.empty;
	}
	return range(&a,a.indexs);
}
auto map(alias F,R)(R r){
	struct Map{
		R r;
		auto ref front()=>F(r.front);
		auto ref index()=>r.index;
		void popFront()()=>r.popFront;
		bool empty()=>r.empty;
	}
	return Map(r);
}
unittest{
	int[5] foo=[1,2,4,3,5];
	foreach(i,e;foo.iteratebyindex.map!(a=>a*2)){
		writeln(i,':',e);
}}

error: expected 1 argument

foreach of a range with 2 arguments should attempt to match i to r.index and e to r.front

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions