random stuff you might find useful

Downs default_357-line at yahoo.de
Tue May 22 03:21:07 PDT 2007


Small improvement (is it actually? not sure ..) to the map function. This totally breaks the one-on-one mappingness and may also contain other subtle bugs, nevermind possibly be slower, so use it with care.
Works for me. No warranty. :p

ArrayOf!(U) map(T, U, V=Tuple!())(T[] array, U delegate(T, size_t) dg, bool delegate(V) valid=null) {
   static if (!is(U==void)) static assert(is(Tuple!(V)==Tuple!(U)), "Error: "~Tuple!(V).stringof~" is not "~Tuple!(U).stringof);
   static if (is(U==void)) {
     foreach (id, inout v; array) dg(v, id);
   } else {
     auto ret=new U[array.length];
     size_t i=0;
     foreach (id, inout v; array) { auto res=dg(v, id); static if (Tuple!(V).length) { if (!valid||valid(res)) ret[i++]=res; } else ret[i++]=res; }
     return ret[0..i];
   }
}

ArrayOf!(U) map(T, U, V=Tuple!(), Bogus=void)(T[] array, U delegate(T) dg, bool delegate(V) valid=null) {
   static if (is(U==void)) map!(T, U, V)(array, (T t, size_t bogus) { dg(t); }, valid);
   else return map!(T, U, V)(array, (T t, size_t bogus) { return dg(t); }, valid);
}



More information about the Digitalmars-d mailing list