Comments (4)
Wow, this is a strange coincidence: I was just the other day thinking about how great it would be if PHP had currying at its core. It would even be mostly backwards compatible, save for code relying on ArgumentCountError
s being thrown and such 😄.
Thinking on this, I noticed that currying is probably what I am missing. I have been working around sprinkling partial
everywhere by using the with
function, like so:
<?php
private function calculateAverageRating(): float
{
if (!count($this->ratings)) {
return 0.0;
}
with($this->ratings)
->map(methodCaller('getScore')),
->reduce(sum) / count($this->ratings),
);
}
The problem with this is, is that it will not work with the lazy variants, as I have now moved the division out of the function composition and thus required that the result be immediately evaluated, which does work nicely in your currying and original example.
I think your rpartial(div, count($this->ratings))
could possibly be rewritten to flipped(div)(count($this->ratings))
to benefit from currying, though flipped
would probably need to be rewritten, as it blindly tries to immediately call the flipped function, which will then still pass the first argument as actual first argument if you don't supply all arguments immediately.
I wanted to raise the concern that there already is a separate version of most functions to make them lazy. Adding a separate curried version of all functions would probably explode into a whopping four versions of all functions. Though I must say that I have been wondering if it would be feasible to just drop the non-lazy variants of the library in a new major release altogether. They could then also be made curried and it would reduce maintenance costs considerably.
Evaluating lazy functions is automatically done by looping over them anyway and, if you really need arrays as output, iterator_to_array
already exists (which, takes a Traversable
, which Generator
implements). I'm also skeptical of any major performance difference between generators and arrays, but I don't think it will make much of a difference unless you have huge collections, which I doubt will happen often.
Most functions are also wrapped in the ds.php
classes for convenience. Though, if we get currying, it's pretty much as short as using the with
keyword, and makes staying inside the function bubble less awkward, I'm wondering if it may not just be a better idea to drop ds.php
as well.
Now if only we had operator overloading so I can overload the dot operator to do function composition and the bitwise or operator to pipe functions 😄.
from nspl.
Evaluating lazy functions is automatically done by looping over them anyway and, if you really need arrays as output, iterator_to_array already exists (which, takes a Traversable, which Generator implements).
There are other libraries of similar purpose using iterators everywhere by default and they are really painful to work with in real-life scenarios. iterator_to_array
calls all around the code, because most standard library functions don't accept iterators.
from nspl.
Though I must say that I have been wondering if it would be feasible to just drop the non-lazy variants of the library in a new major release altogether.
You don't have any larger application using this library, do you?
from nspl.
As for currying by default: if you like Ramda, there are already enough PHP libraries going the Ramda way (see my list). I think NSPL has a clear profile, is quite good at what it is, and shouldn't try to become something else with the next major release.
from nspl.
Related Issues (7)
- Strict mode warning HOT 1
- Allow functions to work with strings HOT 2
- Best way to achieve partial application with operator functions HOT 4
- update doc HOT 2
- Strict error HOT 1
- Head and tail HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nspl.