Live data from Hacker News

Javascript Arrays and Functional Programming

zabanaa.github.io

41–50 of 92 posts

Re: Javascript Arrays and Functional Programming

#41
post #17
post #6

Earlier quoted context omitted.

Wouldn't do it with variable, since it will make your function impure [1] 1 : http://www.nicoespeon.com/en/2015/01/pure-functions-javascri...

That's the GP's point though: it already is impure! If you rewrite as let acc = {...} footballPlayers.reduce(...,acc) It should become clear: acc is an external variable that got mutated by the reduce.

Indeed. A pure function should not depend on and / or mutate external variables.

I'd do it like this (yay for one-ish-liners!):

  const playersByCountry = footballPlayers.reduce((acc, player) => {
    return {...acc, [player.country]: [...(acc[player.country] || []), player]};
  }, {});

Re: Javascript Arrays and Functional Programming

#42
post #13

Earlier quoted context omitted.

The first is slow (O(n²) where O(n) will do), and the second is very likely to be slow (probably the same).

The good old question about sacrificing performance for readability. Anyway these days filter is not so bad. [1] https://jsperf.com/array-uniq-filter

And insertion sort is not so bad with 20 elements. It doesn't take much for it to start falling over, though.

[1]: https://jsperf.com/array-unique-filter-2/1

Re: Javascript Arrays and Functional Programming

#43
post #36

In his example Array.prototype.reduce, isn't this almost the same as just doing a foreach loop? I don't understand why doing this, it's pretty much the same number of lines.

In this example it is, but a nice thing about using map, reduce etc is that you can chain them together, ex:

  let out = ["a", "b", "c", 1, 2, 3]
  	// Turn letters to upper case
  	.map(i => {
  		return typeof i === "string"
  			? i.toUpperCase()
  			: i;
  	})
  	// Add one to numbers
  	.map(i => {
  		return typeof i === "number"
  			? i + 1
  			: i;
  	})
  	// split letters and numbers up
  	.reduce((all, i) => { 
  		typeof i === "string"
  			? all.letters.push(i)
  			: all.numbers.push(i);
  		return all;
  	}, { "letters": [], "numbers": [] });
  // => { letters: ["A", "B", "C"], numbers: [2, 3, 4] }

Re: Javascript Arrays and Functional Programming

#44
post #31

I don't know if I'm delirious but I remember a few years ago when articles like this were very common on HN. Then all of a sudden it felt like everyone had "learnt" it and all that got upvoted was very complicated articles explaining some esoteric JS thing. Now it seems we're back again with. Another example is the article explaining "this" which is on the front page again. It almost feels cyclic, like it's time for…

Maybe hacker news is starting to gain popularity with students, who are trying to follow the "pro hackers".

Re: Javascript Arrays and Functional Programming

#45
post #36

In his example Array.prototype.reduce, isn't this almost the same as just doing a foreach loop? I don't understand why doing this, it's pretty much the same number of lines.

That's the thing about functional programming. I personally don't remember the last time I had the need to write a foreach loop. Foreach loops even kind of feel _dirty_ to me.

All I really need now is Array.map, Array.reduce, Array.filter, Array.sort, Object.keys, Object.values.

Reduce is very powerful, you just have to get to know it.

Edit: this is how I'd use reduce in this case — https://news.ycombinator.com/item?id=14919636 — talk about line numbers

Re: Javascript Arrays and Functional Programming

#46

TL;DR author provides an example of map, filter and reduce functions in JS. I have been wondering lately what is the value of articles like this. They do not convey any meaningful idea, they do not offer any useful insight on underlying matters; why do people even write something like this, except to litter the Internet even more? Anyone, who is distinctly familiar with functional programming or even just with the co…

I'm not sure why you feel the need to bash this person. If you already know the concepts then go read something else. Instead you took the time to write this shitty comment.

This may not provide value to you and thats completely fine. This does however provide a lot of value to the author and to others.

Writing technical thoughts in a way that is simple and understandable is a skill and I think this author did a great job of that.

Also its the internet I can write posts for myself all day long and guess what? Ain't shit you can do about it. I'll keep littering the internet with these "random abrupt posts".

Re: Javascript Arrays and Functional Programming

#47

TL;DR author provides an example of map, filter and reduce functions in JS. I have been wondering lately what is the value of articles like this. They do not convey any meaningful idea, they do not offer any useful insight on underlying matters; why do people even write something like this, except to litter the Internet even more? Anyone, who is distinctly familiar with functional programming or even just with the co…

Man, I had just worked up the nerve to start a personal dev blog but you've just scared me straight off.

I have wanted to start a personal dev blog myself for quite a while, but each time I think of a suitable topic, I immediately recall an article or a post that is already better than anything I could write myself on the matter.

If you think you have something valuable or interesting or just funny to share, that either was never discussed or written about or you know you can do better, then you absolutely should blog about it and do not let haters like me stop you.

As for the original article:

* It should have been named just "JavaScript arrays and higher-order functions" (I get that using term functional programming is very catchy nowadays, but I am trying to talk about quality here, not clickbaitness)

* As it is aimed for people unfamiliar with the concept and coming from more imperative background, where first-class functions are either not present or not that popular or handy to work with, an introduction (even the most gentle one) would be very suitable

* Then the introduction of filter/map/reduce functions would be in place, ideally compared in place with imperative implementation of the same task

* The best way to make sure a reader understands how each of this functions works is to make them implement each one of them, even the naive implementation would suffice

* Finally, you could provide some reasonably unobvious usages of any of the functions, e.g. insertion sort done with fold-only.

Re: Javascript Arrays and Functional Programming

#48
post #33

Earlier quoted context omitted.

Sounds more like "coding with map, reduce and filter" to me. It beats complex for-loops where mapping, reducing and filtering are all mixed in together but I wouldn't call it functional programming.

As mentioned in one of the comments, this is nowhere near the definition of functional programming. > It beats complex for-loops Nvm. I still prefer for loops in most cases, in some way it's easier to understand for me if I read it some months later. For example the reduce function I would write something like this: getPlayersByCountry= ( footballPlayers= [], players= {} ) -> for player in footballPlayers if players.…

What I mean is when you have a 200 line for loop that includes other for loops, "continue" statements, if statements and modifying state it gets really confusing quickly. If it's short it's probably going to be OK to understand anyway but if you can decompose a long for loop into several map, reduces and filters it's much easy to understand what's going on because it's more linear.

For that example, you should really have a generic "group by" function to use anyway that plays nice with your map, filter and reduce functions.

Re: Javascript Arrays and Functional Programming

#49
post #5

Some more practical examples // Grab unique [1,1,2,3,4].filter( ( item, index, array ) => array.indexOf( item ) === index ) // => [1,2,3,4] // Flatten [[1,2],[3,4]].reduce( ( result, item ) => result.concat(item), [] ); // => [1,2,3,4] Not sure why the author missed `sort`. // Sort [1,2,4,3].sort( ( a, b ) => a - b )

Array.prototype.sort destructively modifies the array, which is typically not considered a part of the functional paradigm. You could make a case for pragmatism if the array were purely local to the function (i.e. not passed into it), so that nothing outside the function would notice anything being mutated.

Re: Javascript Arrays and Functional Programming

#50
post #36

In his example Array.prototype.reduce, isn't this almost the same as just doing a foreach loop? I don't understand why doing this, it's pretty much the same number of lines.

> isn't this almost the same as just doing a foreach loop

yes they are both ways of iterating over the array

> I don't understand why doing this

there's more than one way to skin a cat

Post reply on HN