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

Exercise 19 #82

Open
Lee182 opened this issue Jul 21, 2015 · 2 comments
Open

Exercise 19 #82

Lee182 opened this issue Jul 21, 2015 · 2 comments
Labels

Comments

@Lee182
Copy link

Lee182 commented Jul 21, 2015

Hi wouldn't this code for reduce

videos.reduce(function(acc, video, index) {
  acc[video.id] = video.title;
  return acc;
}, {});

be better than using Object.create in your example

        videos.reduce(function(accumulatedMap, video) {

            // Object.create() makes a fast copy of the accumulatedMap by
            // creating a new object and setting the accumulatedMap to be the
            // new object's prototype.
            // Initially the new object is empty and has no members of its own,
            // except a pointer to the object on which it was based. If an
            // attempt to find a member on the new object fails, the new object
            // silently attempts to find the member on its prototype. This
            // process continues recursively, with each object checking its
            // prototype until the member is found or we reach the first object
            // we created.
            // If we set a member value on the new object, it is stored
            // directly on that object, leaving the prototype unchanged.
            // Object.create() is perfect for functional programming because it
            // makes creating a new object with a different member value almost
            // as cheap as changing the member on the original object!

            var copyOfAccumulatedMap = Object.create(accumulatedMap);

            copyOfAccumulatedMap[video.id] = video.title;

            return copyOfAccumulatedMap;
        },
        // Use an empty map as the initial value instead of the first item in
        // the list.
        {});
@gabrielkunkel
Copy link
Contributor

So I think the conclusion on this issue, from this conversation, was that we want exercise 19 to work with immutable data. As one person said, "mutating the original copy through out the call stack would be considered a 'side effect' and not very 'functional.'"

Your solution is more performant, testable, and simple. In an interest to achieve the most value I submitted this pull request which uses Object.assign() instead of Object.create().

@Lee182
Copy link
Author

Lee182 commented Jan 28, 2016

Yes thanks, I'm sure a lesson in Object.assign would be quite useful. Anyways I submitted the issue ages ago (in 2015) so no worries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants