Comments (9)
I have to work on it... Expect some changes during the weekend.
from collection.
@drupol let me know if you can think of other operations that we might want to include in this.
Even though they are not reduction operations, it feels like a good opportunity to revisit: first
, last
, head
, get
. From the usage you've seen, are they actually used in instances other than calling current
immediately after? E.g. are they used to apply further transformations to a single element?
from collection.
Hi,
I think I know why I chose to do that.
It's because a "reducing" operation such as those listed in here are also able to, instead of "reducing" the input data into one single value, "expand" the input data into a bigger set.
from collection.
Hi,
I think I know why I chose to do that. It's because a "reducing" operation such as those listed in here are also able to, instead of "reducing" the input data into one single value, "expand" the input data into a bigger set.
Hey, could you pls give some examples of this?
from collection.
<?php
namespace Test;
use loophp\collection\Collection;
include __DIR__ . '/vendor/autoload.php';
$input = [
'a' => [
'b', 'c'
],
'd' => [
'e', 'f', 'g'
],
];
$c = Collection::fromIterable($input)
->foldLeft(
static function (array $carry, array $value, string $key) {
$carry[] = $key;
$carry = array_merge($carry, $value);
return $carry;
},
[]
);
from collection.
I see, I feel like this is only a thing because in PHP we can use arrays as both lists and maps 😄. You'd have some type restrictions in other languages that might prevent doing this.
Anyway, I see a few options to move forward here:
-
Ignore this usage and make all reductions return a single value - the simplest case, might be a bit inconvenient for the usage you described above; would be interesting to know if this usage is common
-
Pick and choose which operations to modify - for example, we could modify only
compare, min, max
and leave the other one as they are - I'm not the biggest fan of this because it makes things a bit inconsistent -
Have some logic to determine whether to keep returning a Collection or not - for example, we could check the
$carry
parameter: if it's anarray
, we keep things in a collection. If not, return a single value
None of these are perfect, they all have some compromises. Can you think of any others? Or which one do you find more appealing?
from collection.
I have to think about that.
I would definitely go for the first option.
Indeed, if the intended result is to expand the input set, flatMap should be used:
$c = Collection::fromIterable($input)
->flatMap(
static function (array $value, string $key) {
yield $key;
yield from $value;
}
);
from collection.
Found a few more that could be included in this:
The purpose of these is to always result in a single value
from collection.
+1 on this, I did have few workaround in my projects but having this in core would be awesome
from collection.
Related Issues (20)
- `Partition` Operation - Awkward to use? HOT 18
- Add find/search/where/single/firstWhere method HOT 23
- Modify `all` operation to prevent data loss HOT 6
- PHPStan 1.0 upgrade HOT 1
- Typed collection support HOT 17
- API oddities HOT 6
- Issue with cache and fromCallable HOT 13
- PHPStan reporting an error for missing optional parameters HOT 6
- [Question] Rename Collection interface to CollectionInterface HOT 1
- Dependency Dashboard HOT 51
- Collection interface doesn't extend Countable HOT 9
- Weird interplay between Collection and PDO result set HOT 13
- Plus operation RFC HOT 10
- Palm cannot infer types when using some operations HOT 4
- Unexpected behavior of pair operation over empty collection HOT 1
- [Feature request] Implement stable sorting HOT 12
- Memory size exhausted for large collections HOT 15
- Issue with distinct HOT 3
- Distinct is slow HOT 2
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 collection.