Either returning a map with an anonymous function or using it as you do. I gather the Map template in std.algo is unable to have access to your closure literal, as it is inside foo.
A possible workaround is having the anonymous function as a standard, named, free function and use this inside foo. But in that case, why have anonymous functions in D?
Another is to use 'string functions', I think. I can test right now, but something like might work:
void foo(string str)
{
str =
std.algorithm.map!q{
inPattern(a, [digits, letters])? a : '_';
}
(str);
}