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

filter/select by property #120

Closed
Erikvv opened this issue Feb 13, 2017 · 4 comments
Closed

filter/select by property #120

Erikvv opened this issue Feb 13, 2017 · 4 comments

Comments

@Erikvv
Copy link
Contributor

Erikvv commented Feb 13, 2017

$persons = ....
$lars = 'Lars'
$larses = selectByProperty($persons, ['name' => $lars]);

much less verbose than:

$persons = ....
$lars = 'Lars'
$larses = select($persons, function ($person) use ($lars) {
    return $person->name == $lars;
});

It could work with multiple properties too, plus nesting. Like graphQL.

The name, alternatively, could be 'where'.

Good idea?

Ive created an implementation here: https://github.com/Erikvv/array-utils-php/blob/master/src/Ve/Where.php

It's a bit too large to be contained in one function.

@lstrojny
Copy link
Owner

I think a where() function would be an awesome addition. Wanna work on a pull request?

@cullylarson
Copy link

cullylarson commented Feb 22, 2017

If you add it, I'd suggest changing the order of your parameters so that your 'where' properties are the first param, and your data param is second; and also, curry it. This would let you do something like:

$selectByLars = selectByProperty(['name' => 'Lars']);

$larsPeople = $selectByLars($people);

OR

$notEmpty = function($x) { return !is_empty($x); };
$haveLars = compose(
  $notEmpty,
  selectByProperty(['name' => 'Lars'])
);

if($haveLars($people)) { ... }

@Erikvv
Copy link
Contributor Author

Erikvv commented Feb 23, 2017

Switching the argument order would be inconsistent with the other functions.

The same for currying by default: none of the other functions do this. You can still use a curry function.

@lstrojny
Copy link
Owner

lstrojny commented May 9, 2017

Closing because of inactivity

@lstrojny lstrojny closed this as completed May 9, 2017
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

3 participants