DEV Community

loading...

Discussion on: Javascript Sock Merchant Challenge - Solution 2

Collapse
theodesp profile image
Theofanis Despoudis

Looks complicated. Take a look at my comment.

Collapse
johnboy5358 profile image
John

Thanks, I did and borrowing your concept I found a reducing callback that works, but I think it is going to show what Ady said about functional code in JS with reference execution speed. Array.prototype.reduce is slow compared to other JS imperative looping constructs. However, I am getting somewhere around 30ms for the code below using console.time on a 10k set.

When put in perspective; a blink of an eye is approx 300 - 400ms, roughly a third of a second, so it's still pretty quick.


const sockMerchant = (n, ar) =>
  ar.reduce((acc, colr) => {
    return (colr in acc && acc[colr])
      ? {...acc, [colr]:0, 'pairs':acc['pairs']+1}
      : {...acc, [colr]:1}
  }, {'pairs': 0} ).pairs

Thread Thread
adyngom profile image
Ady Ngom Author

Hey John awesome to have you back and yet coming up with other ways of enriching the discussion. I really, really like what you are proposing here.

It really boils down to having an imperative or a declarative style in solving those problems. Your solution here is kind a mix of both. For those who are used to reduce this might look declarative even though the ternary operation is leaning more towards imperative.

If those concepts are new or not clear, I can't recommend enough Tyle McGinnis article on the subject: Imperative vs Declarative Programming

Regarding performance, I will quote what I think is a real gem from the above article:

Many (if not all) declarative approaches have some sort of underlying imperative abstraction.

-- Tyler McGinnis

That should settle the performance discussion since declarative (map, reduce, filter, etc...) will always include and abstract one or more imperative layers such as a for loop. I can live with 30ms for a dataset of 10,000 items though if I have the understanding that the for loop might cut in half, it is an informed decision.

The last part that is left from my perspective would be readability. I think it is highly readable for the trained eye but might still be hard to digest for the enthused and upcoming at first glance. With time though, one might learn to grow and appreciate the details in it.

Cheers

Thread Thread
johnboy5358 profile image
John

Thanks Ady, Tyler's article is interesting and he also says in that same article...

"Imperative: C, C++, Java

Declarative: SQL, HTML

(Can Be) Mix: JavaScript, C#, Python"

JavaScript, as he points out, is a mixed bag and I think we should take every opportunity to use that wherever it works best. Yes, I have been having fun trying out different solutions.

But, I think, that's all from me on the sock merchant. I had a thought that I might go and learn a functional programming language like Elm or Elixir.

Thanks again for the Sock Merchant problem... it has been fun!

Thread Thread
adyngom profile image
Ady Ngom Author

You will be missed Sir John, thank you and happy coding :)