Git Product home page Git Product logo

Comments (9)

drupol avatar drupol commented on August 11, 2024 1

I have to work on it... Expect some changes during the weekend.

from collection.

AlexandruGG avatar AlexandruGG commented on August 11, 2024

@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.

drupol avatar drupol commented on August 11, 2024

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.

AlexandruGG avatar AlexandruGG commented on August 11, 2024

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.

drupol avatar drupol commented on August 11, 2024
<?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.

AlexandruGG avatar AlexandruGG commented on August 11, 2024

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:

  1. 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

  2. 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

  3. Have some logic to determine whether to keep returning a Collection or not - for example, we could check the $carry parameter: if it's an array, 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.

drupol avatar drupol commented on August 11, 2024

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.

AlexandruGG avatar AlexandruGG commented on August 11, 2024

Found a few more that could be included in this:

The purpose of these is to always result in a single value

from collection.

ipranjal avatar ipranjal commented on August 11, 2024

+1 on this, I did have few workaround in my projects but having this in core would be awesome

from collection.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.