Range Construction Pattern

Philippe Sigaud philippe.sigaud at gmail.com
Sat Feb 22 07:55:54 PST 2014


per.nordlow:
>
> My try so far:
>
> import std.traits: isCallable, ReturnType, arity, ParameterTypeTuple;
>
> enum arityMin0(alias fun) = __traits(compiles, fun()); // new syntax in
2.064
>
> auto repeat(alias fun)(size_t n) if (isCallable!fun &&
>                                      arityMin0!fun &&
>
>                                      !is(ReturnType!fun == void))
> {
>     import std.range: iota, map;
>     return n.iota.map!(n => fun);
> }

For a nullary function, this solution is certainly OK: short and
understandable.
I found a way to parse an entire function param list, including default
args, but I don't remember right now how I did that. Maybe by parsing
'fun.stringof'
Maybe this exists in Phobos now?

btw, I'd call this template 'apply', because repeat already exists in
Phobos with a different use.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20140222/db9249e2/attachment-0001.html>


More information about the Digitalmars-d-learn mailing list