-
Notifications
You must be signed in to change notification settings - Fork 13
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
Switch from static functions to namespaced functions #7
Comments
It is unfortunate that there isn't something like |
Oh I see the concern... Personally I would prefer typing multiple use function Pramda\map;
// ...
class P {
public static function __callStatic($name, $arguments)
{
if (in_array($name, ['map', /* ... */])) {
return call_user_func_array($name, $arguments);
}
}
} |
I'm sorry, I am not sure what the above snippet intends. |
I was just wondering if you could provide both the static methods and the standalone functions in the same version of Pramda (by making use of P::map(function($num) { return $num * 2; }, [1,2,3,4,5]);
// or
use function Pramda\map;
map(function($num) { return $num * 2; }, [1,2,3,4,5]); |
Ah, I see - you mean having the functions defined in the namespace and then creating a stub Since we already have the class with the static functions and there are a lot of // pramda.ns.php
namespace Pramda;
use \P; /* The library, still defined outside of the namespace */
/* Stub */
function map() {
return call_user_func_array(['P', 'map'], func_get_args());
} // added in pramda.php
require ("pramda.ns.php"); Each function would need a stub, but that's ok - it can be automated with PHP-Parser if it ever gets tedious. Regardless of the way, I like it a lot, thank you very much for the suggestion! 🍻 I will test it and include it in the next release. |
Excellent - you're welcome! That's great that it will keep working for 5.5, nice revision 👍. I'll be looking forward to the next release then, and thanks for porting Ramda to PHP! 🍻 |
The feature has been implemented in branch 0.10.0 In PHP 5.6+, the first example in the Readme can be now also written as: use function Pramda\file;
use function Pramda\map;
use function Pramda\compose;
use function Pramda\countBy;
use function Pramda\identity;
use function Pramda\negate;
use function Pramda\sort;
use function Pramda\unary;
use function Pramda\flatten;
use function Pramda\toArray;
use function Pramda\take;
use function Pramda\each;
// The above could be replaced in PHP 7+ with
// use function Pramda\{file, map, compose, countBy, identity, negate, sort, unary, flatten, toArray, take, each}
$text = file('test.php'); // Lazy read line by line with generators
$onlyWords = function ($txt) {
return preg_split("/[^A-Za-z]/", $txt, NULL, PREG_SPLIT_NO_EMPTY);
};
$wordsPerLine = map(compose($onlyWords, unary('strtolower')));
$getFreq = countBy('identity');
$sortDesc = sort('negate');
$getWordsFrequencyDesc = compose(
$sortDesc,
$getFreq,
'flatten',
$wordsPerLine
);
$topFiveFreq = compose('toArray', take(5), $getWordsFrequencyDesc); // Just the top 5, for fun
$printEm = each(function($value, $key) {
echo $key . ": " . $value . "\n";
});
// And now, we apply to the data
$results = $printEm($topFiveFreq($text)); Also, Another feature is that now this is also possible: I will close this issue once |
As of 7.0 one can do this: http://php.net/manual/en/language.namespaces.importing.php#language.namespaces.importing.group (if that helps :) |
Thank you, certainly 🍻 (I had mentioned it as well in the comment on the snippet above below the use function lines). In my opinion, it is slightly better but still a bit of a hassle, since you are just expanding the text horizontally instead of vertically. I think a wildcard would have been much more practical. I don't mean to sound like complaining, I find PHP 7 great - I would just love few more extras like this (and "fat arrow" closures). |
@kapolos Right, right. I see. |
It would be awesome if pramda used namespaced functions (
use function Pramda\map
) instead of static functions (P::map
). Is this sort of API possible without requiring PHP 5.6+?The text was updated successfully, but these errors were encountered: