News for hackers. Stuff that matters.
Re:Generalizing 'jq' and Traversal Systems using optics and ... by mgraczyk on Wednesday October 07, 2020 @03:29PM (#24711747)
That's more difficult to type than something like

`.company.staff | .pets | flatten | filter .type == "cat" | log(...)`

Re:Generalizing 'jq' and Traversal Systems using optics and ... by vortico on Wednesday October 07, 2020 @03:33PM (#24711792)
Maybe so, but much easier to read. Code is read 10x more often than written, especially in teams, so it makes sense to optimize for legibility rather than elegance in some "higher" mindset, or less keys pressed (because most of the time will be spent staring at the line you wrote than actually typing if you write a functional line like that.)
Re:Generalizing 'jq' and Traversal Systems using optics and ... by kccqzy on Wednesday October 07, 2020 @03:37PM (#24711851)
That's a bad argument because you didn't specify who the audience is. Functional code is more readable to me (and many teams I've worked with), imperative code is more readable to you and that's fine. Go with your team's preference. And, most of time, what your team prefers can easily be changed with a few sessions to introduce them to a functional style.
Re:Generalizing 'jq' and Traversal Systems using optics and ... by elcritch on Wednesday October 07, 2020 @03:48PM (#24711959)
Perhaps its easier for you to read. It takes me 2-3 longer to read the loops since each loop is unique and I have to parse the semantics/context, whereas its quick for me to parse the behavior from the named functional operators even at a glance. Conversely I find writing the functional version a bit trickier for complex reduce operations.
Re:Generalizing 'jq' and Traversal Systems using optics and ... by akvadrako on Wednesday October 07, 2020 @03:39PM (#24711863)
I totally disagree. I can read the short version pretty quickly and get an idea what it does - every word matters.

The original version with for loops just looks like a block of text. Spacing would help, but I would still need to look carefully to pick out the important bits.

Re:Generalizing 'jq' and Traversal Systems using optics and ... by rrdharan on Wednesday October 07, 2020 @06:17PM (#24713444)
Forget it, Jake. It’s Chinatown.
Re:Generalizing 'jq' and Traversal Systems using optics and ... by MrBuddyCasino on Thursday October 08, 2020 @07:22AM (#24718055)
Underrated comment.
Re:Generalizing 'jq' and Traversal Systems using optics and ... by marta_morena_29 on Wednesday October 07, 2020 @03:42PM (#24711894)
It's a tradeoff. The problem is that academia often comes from the angle of abstracting and generalizing everything, which is even more harmful than copy & pasting and using the simplest code possible (mostly because copy & paste + simplest code is still readable and can be improved upon, while academic style code usually is inaccessible).

You don't want to listen to any specific camp. The best is always in the middle and you are on the right track:

"Pick what is easy to read."

However there is a caveat, which is that functional style is only hard to read at the beginning. Have a look at how modern languages integrate functional programming (see java, C#, C++, etc.).

Java streams are a common example. They make most code much easier to read ONCE you are used to it, because they provide standardized behaviour that is side-effect free. When you write everything in plain syntax, the reader needs to abstract your code in his head, while when using streams, you just know what `filter`, `map`, `reduce`, etc. do and you only need to look at the small customizations. It's easier to read once you are used to it.

However there is a limit and this limit get hit quickly. A lot of people like to make their code as complicated and fancy as possible and reduce every possible line. This is definitely wrong. Code must be easy to understand and change. If you fail that, you are doing it wrong.

This also depends on your environment. If your co-workers don't understand the code you write, you have failed, no matter if your code is "nicer" or not. You either need to write simpler code or educate your co-workers to create common ground.

Re:Generalizing 'jq' and Traversal Systems using optics and ... by vortico on Wednesday October 07, 2020 @03:44PM (#24711915)
Wait a second, you left out the most important part: What's in the `log(...)` function? How do you get the name of the pet and the name of the staff in that line if they have the same accessor `.name`?

That's my point. The fact that you didn't include it implies to me that it was too difficult to figure out, and a true implementation would be far more complicated, so it's not a fair comparison.

Re:Generalizing 'jq' and Traversal Systems using optics and ... by mgraczyk on Wednesday October 07, 2020 @05:11PM (#24712783)
I disagree, there are many ways to solve this problem. for example

   .company.staff | .pets | flatten | filter .type == "cat" | print(.name + " belongs to " + ..name)