Forwarding arguments through a std.algorithm.map
ag0aep6g
anonymous at example.com
Sat Mar 10 21:31:41 UTC 2018
On 03/10/2018 09:48 PM, Nordlöw wrote:
> If I have a function
>
> bool f(Rs...)(Rs rs)
>
> is it somehow possible to map and forward all its arguments `rs` to
> another function
>
> bool g(Rs...)(Rs rs);
>
> through a call to some map-and-forward-like-function `forwardMap` in
> something like
>
> bool f(Rs...)(Rs rs)
> {
> alias someArbitraryFun = _ => _;
> return g(forwardMap!(someArbitraryFun)(rs));
> }
>
> ?
Not with that syntax, as far as I know. A function can't return an alias
seq like that.
But it can return a std.typecons.Tuple. You'd have to add `.expand` then:
return g(forwardMap!someArbitraryFun(rs).expand);
> What should the definition of forwardMap look like?
Not tested beyond `f(1, 2.3, "foo")`:
----
auto forwardMap(alias fun, Ts ...)(Ts things)
{
import std.meta: aliasSeqOf, staticMap;
import std.range: iota;
import std.typecons: Tuple;
alias NewType(size_t i) = typeof(fun(things[i]));
alias NewTypes = staticMap!(NewType,
aliasSeqOf!(iota(things.length)));
Tuple!NewTypes results;
static foreach (i, thing; things) results[i] = fun(thing);
return results;
}
----
I wouldn't call it "forwardMap". "tupleMap" or "seqMap" maybe?
> Does Phobos contain something like this already?
Not that I know of.
More information about the Digitalmars-d-learn
mailing list