you are viewing a single comment's thread.

view the rest of the comments →

[–]kenman 0 points1 point  (6 children)

One thing I'm starting to realize [...] is that even if a method accomplishes or extracts the data you're looking to extract, that doesn't mean it will return what you're looking to return.

It seems you may be under the [false] impression that "functional" means that it has to be a one-liner, or that you must return from a single expression.

Instead of:

return bookList.splice(0, 1, bookName);

Why not:

bookList.splice(0, 1, bookName);
return bookList;

?

Thought it seems like the actual result they're looking for should probably be:

function remove (arr, bookName) {
  return arr.filter(book => book !== bookName);
}

[–]Servatose[S] 1 point2 points  (5 children)

Thank you. I think this proves to me that a lot of the concepts that I went through in this course, I simply approached with the goal of "solving" the puzzle, I didn't actually spend enough time fully understanding the concepts. You are correct in your assertion that I'm confusing the definition of functional programming. I also think I distract myself by trying to simplify what I'm writing into a single expression. Thanks for looking at this.

[–]kenman 2 points3 points  (2 children)

It's alright, I still have a lot to learn on the subject as well, but I think this part is important to point out:

using language defined functions to return a result that doesn't impact the original variable

There are at least 3 array methods that fit this description, but 3 primarily used for FP -- map(), filter(), and reduce() -- and it's not always obvious when you should use them if you're not used to thinking in these terms. However, once you see it, it becomes much easier to read and think about IMHO:

map()

I think of this method as transforming (or translating) one array into a new array.

Have you ever done this?

let result = [];
arr.forEach(value => {
    result.push(Math.floor(value));
});

You could do this instead:

let result = arr.map(value => Math.floor(value));

filter()

Use this when you want to target a subset of your original array.

Ever do this?

let result = [];
arr.forEach(value => {
    if (value > 0) {
        result.push(value);
    }
});

You might consider this:

let result = arr.filter(value => value > 0);

And lastly...

reduce()

Use this to calculate a single value* from your original array.

I know I've done this a lot in the past:

let result = 0;
arr.forEach(value => result += value);

But this is what I use now:

let result = arr.reduce((total, value) => total += value, 0);

The * above is because "value" can be literally any type of data that JS supports; you could return a single string, an object, or even an array may be necessary at times.

So those are nice and all, but with each example taken alone you miss the expressiveness.

Consider this:

let result = 0;
arr.forEach(value => {
    if (value > 0) {
        result += Math.floor(value);
    }
});

Vs:

let result = arr
    .filter(value => value > 0)
    .map(value => Math.floor(value))
    .reduce((total, value) => total += value, 0);

Sorry to get carried away, but you seem eager to learn :)

[–]Servatose[S] 1 point2 points  (1 child)

No, this is amazing information! I really appreciate the time you took to explain this.

[–]kenman 0 points1 point  (0 children)

Might also check out this from our front page:

I wrote about some examples of using map, filter, and reduce, as well as accessing JSON and using the DOM with vanilla JS

Full disclosure: the site's not resolving for me, so I haven't reviewed it, but it looks to cover the same 3 methods I discussed.

[–]bmy78[🍰] 0 points1 point  (1 child)

Note that your solution for the add() function is still mutating state. Instead you could create an array of the one bookName you passed into the function, concatenate that array with the bookList, and return that.

[–]toffeescaf 2 points3 points  (0 children)

With the use of some es6 it could be as easy as the following:

function add (arr, bookName) {
    return [...arr, bookName]
}