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

Possible improvements #149

Closed
MortalFlesh opened this issue Aug 6, 2017 · 3 comments
Closed

Possible improvements #149

MortalFlesh opened this issue Aug 6, 2017 · 3 comments

Comments

@MortalFlesh
Copy link
Contributor

Hey I really like this library. I'm quite interesting in functional programming and this is much help in PHP :) But since I quite know php and its "bad parts" I came up with few possible improvements, if you were interested.

For example, if you need to pass one of your functions (or any other) as a parameter, you need to do it by its fully qualified name (in string) which sucks. So I tried to use constants to keep my fully qualified name in reusable value: https://github.com/MortalFlesh/php-query-builder-composer/blob/master/src/functions.php#L9-L17 - this saves me a lot of pain (typos, remembering all names) it would be much nicer, if such constants could be camel case and not upper case (by PSR2) but this is still quite big help. So I though about a single final class, which would hold all function names in constants and could be used as:

final class Functions {
    const LAST = 'Functional\\last';
}

// elsewhere
use function Functional\curry_n;
use Functional\Functions as f;

$splitHostAndPort = curry_n(2, 'explode')(':');
$parsePort = compose($splitHostAndPort, f::LAST);

$port = $parsePort('host:123'); // 123

Constant with fully qualified name could also be right in file where function is declared. And it would be my pleasure to contribute by this idea to your library if you finds it helpful.


And even more to callbacks, I have this little library https://github.com/MortalFlesh/CallbackParser which allows to have callbacks in strings and saves some awful function syntax for small and easy callbacks. Such as:

use function Functional\map;

$add1map = map('($value) => $value + 1', [1, 2, 3]); // 2, 3, 4
@3amprogrammer
Copy link
Contributor

3amprogrammer commented May 29, 2018

I love the idea of constants. To be honest, this is exactly why I am here, as I wanted to create a similar issue.

I can make a PR just let me know what are your thoughts @lstrojny?

$getRegion = compose(
    $selectAdministrativeBoundary,
    $sortByPlaceRank,
    'Functional\first' // <--- This is ugly as hell :(
);

$getRegion = compose(
    $selectAdministrativeBoundary,
    $sortByPlaceRank,
    Functional::FIRST // <--- This is art, java-like syntax
);

@lstrojny
Copy link
Owner

I very much like the idea to use constants for convenient indirection, that indeed looks very nice. I would love to see a PR. Bonus points if the constants table is either automatically generated or introspected for completeness in a test.

@lstrojny
Copy link
Owner

On MortalFlesh/CallbackParser: one of my goals (which should probably be explicitly stated) with this library is to support functional programming in PHP using standard PHP functionality. In DSL terms: an internal DSL if you like, not an external DSL. MortalFlesh/CallbackParser is cute but it goes to much into external DSL and I still have high hopes that we get short array functions in PHP eventually 😄

Closing this issue since the constants table has been introduced, thank you for that.

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