Generic function maps?
As this library gets larger, the selection of native php functions that are needed as Functions\Map\Single::foo() will increase. What do you think about generating a generic one:
class Single
{
public static function generic(string $function, array $xs, ...$params): array
{
return array_map(
function ($x) use ($params) {
return $function($x, ...$params);
},
$xs
);
}
}
You can always just use array_map. The Map\Single classes make it more convenient to use array_map.
This:
$sqrts = Map\Single::sqrt($xs);
Is nicer to read than
$sqrts = array_map(
function ($x) {
return sqrt($x);
},
$xs
);
To make a generic one that will work with most of the built-in PHP functions, then the interface goes from something like this:
$sqrts = Map\Single::sqrt($xs);
To this:
$sqrts = Map\Single::generic('sqrt', $xs);
It is less work for library writers, but more work for library users. I think a library should provide as simple an interface as possible to make the lives of library users easier at the expense of the library writers having to do more work. Kind of like Spock's the needs of the many outweigh the needs of the few.
My intention was more for one-off use cases. I’m mapping the signum function in one PCA test, and was considering adding a single map for it, but then thought that a generic one that I proposed here could serve more purposes. This isn’t to replace the more heavily used Single Maps.
Sure. Go ahead and add it. It sounds more like something that would be used internally in places like testing and probably not something to document for end users to use, which is fine. Not every useful utility class or method needs to be documented.