Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic function maps? #334

Open
Beakerboy opened this issue Aug 13, 2019 · 3 comments
Open

Generic function maps? #334

Beakerboy opened this issue Aug 13, 2019 · 3 comments

Comments

@Beakerboy
Copy link
Contributor

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
        );
    }
}
@markrogoyski
Copy link
Owner

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.

@Beakerboy
Copy link
Contributor Author

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.

@markrogoyski
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants