Live data from Hacker News

Loopless Programming

code.jsoftware.com

21–30 of 129 posts

Re: Loopless Programming

#21

This reminds me of regex, and the struggle 2 months later to remember wtf I was trying to do there. Easy to write, hard to maintain, so thank you but no thank you, I prefer the loops instead even if it means going down at individual elements in a list/matrix. For any meaningful project maintenance is the threshold that will make it or break it.

There are cases where I very much prefer a declarative style. Multiplying matrices for example. Depending on the context, I will know what it does conceptually, but don't want to see the implementation, as it probably won't matter. My rule of thumb is: are you doing something unexpected ? show the implementation : show the concept

Re: Loopless Programming

#23
post #15

Loops of known size is the one of the best hints you can give to your compiler though. If the computation inside the loop isn't too complicated, the compiler will unroll these, often resulting in amazing speedups. In fact if you know that your array is pretty long but don't know its size, it's often worthwhile to structure the computation as a nested loop, where the inner loop processes chunks of known length (which…

Good modern compilers do all of that for you, even for unknown sizes. For example, see how clang unloops (and vectorizes) a simple loop: https://godbolt.org/z/NPfnhb

Re: Loopless Programming

#24
post #11

> Looping - performing a computation repeatedly - is what programs do. In most computer languages, all loops are expressed by one of two statements: > Do While - repeat a code block until a condition is met > For - repeat a code block, with a loop index indicating how many times the block has been repeated. > Programmers trained on scalar languages have spent many years internalizing the Do-While and For paradigms. D…

> It also doesn't take 'many years' to internalise for/while loops, these are fairly basic constructs which are learned by most novices in the beginning of an introductory programming course. The comment is referring to the patterns becoming ingrained in the mind through years of use, not that learning how to produce a while loop for the first time takes years.

There's not much to be ingrained after you learn how for/while loops work, and there's not much in the way of learning how map/filter, or overloaded functions work. I wouldn't even consider the overloaded '+' operator as a loop, you can think of it as a single operation, it's an implementation detail.

Re: Loopless Programming

#25
post #17
post #11

> Looping - performing a computation repeatedly - is what programs do. In most computer languages, all loops are expressed by one of two statements: > Do While - repeat a code block until a condition is met > For - repeat a code block, with a loop index indicating how many times the block has been repeated. > Programmers trained on scalar languages have spent many years internalizing the Do-While and For paradigms. D…

> (x + y) is going to be an expression in almost any language... But the equivalent for loop is not.

It is wherever '+'/(equivalent function) overloading is a thing - C++, Haskell, Rust, etc. There isn't even a good reason to think of it as a loop, because for different types it may not be a loop at all.

Re: Loopless Programming

#26
In C#/.Net there's language constructs like LINQ, and framework methods on generic collections like Select() and Where() and SkipWhile(), etc.

For example, if you want to write code that returns the first X items in an array that satisfy a predicate P, it will look a lot neater and readable using code like that, than if you were to write it with for loops and if statements.

But it's still just syntactic sugar, the neater code just masks the underlying code that contains the actual loops and conditionals.

As always, it's a tool, and can be misused like all tools. It's a balance, the neater code might be more readable, but the for/if code might be easier to change or optimize down the line if conditions change. It all depends. There are no silver bullets, just tools, and trying to minimize the amount of tools in your toolbox is just dumb.

Re: Loopless Programming

#27

This reminds me of regex, and the struggle 2 months later to remember wtf I was trying to do there. Easy to write, hard to maintain, so thank you but no thank you, I prefer the loops instead even if it means going down at individual elements in a list/matrix. For any meaningful project maintenance is the threshold that will make it or break it.

It really depends on the situation. For regex, certain things are much more understandable than a long set of loops for it, as long as the regex isn't too complicated. Ditto for implicit for-loops for addition in say numpy or fortran 90. I've seen scientific codes in C that have like 5 to 6 order nested loops that end up being thousand+ of lines, after which the length requires much much more time reinternalizing after coming back months later, or worse, as someone who didn't write it, all due to the fact that the whole damn thing can't fit on a single page of emacs and be still legible.

Re: Loopless Programming

#28
C++ devs are encouraged to do loopless programming by using algorithms in the standard library; plus C++20 ranges should improve the ease of loopless programming.

Where J shines is array based programming. The C++ algorithms are concerned with vectors, but in J you can combine matrices, vectors and scalars in concise expressions.

Re: Loopless Programming

#29
> add each number in a list x to each number in the corresponding row of a two-dimensional array y

I mean, I could write a function `add(x, y)` that does that in any language. You could even inspect the data or type to make it polymorphic. I believe NumPy does this, for example.

Skimming the rest of the article, I don't get how this is different from any other language. The primary difference seems to be the function names are all one or two characters long for some reason. I must be missing something...

Re: Loopless Programming

#30
post #11

> Looping - performing a computation repeatedly - is what programs do. In most computer languages, all loops are expressed by one of two statements: > Do While - repeat a code block until a condition is met > For - repeat a code block, with a loop index indicating how many times the block has been repeated. > Programmers trained on scalar languages have spent many years internalizing the Do-While and For paradigms. D…

Just FYI, the code-formatted block quotes in your comment need a lot of horizontal scrolling both on mobile and desktop because of the very long lines. Unfortunately HN doesn't provide any kind of nice formatting for block quotes, so it's understandable that people try to use code formatting for this.

Sometimes commenters will use code formatting with manually entered line breaks, e.g. keeping lines to 72 characters or less. But that is still hard to read on mobile devices.

Instead of code formatting using leading spaces, here's a good alternative for block quotes (the '>' goes in the first column, no leading spaces):

  > *A paragraph of block quoted text.*
This renders as:

> A paragraph of block quoted text.

If the quote has * characters within it, don't try to italicize it, just do this:

  > A paragraph of block quoted text.
which renders as:

> A paragraph of block quoted text.

Either way the quoted text will be wrapped correctly on any device.

Give each paragraph its own '>' indicator and a blank line between them so they don't get wrapped together.

In case you don't get a chance to edit your comment, here's a more readable copy:

- - - - - - -

> Looping - performing a computation repeatedly - is what programs do. In most computer languages, all loops are expressed by one of two statements:

> Do While - repeat a code block until a condition is met

> For - repeat a code block, with a loop index indicating how many times the block has been repeated.

> Programmers trained on scalar languages have spent many years internalizing the Do-While and For paradigms. Discarding these paradigms is the biggest re-think they need to make when they learn J.

A lot of languages have these kinds of semantics and arguably in a more streamlined / better organised way - Haskell typeclasses for example. I don't know if it's true that 'most languages' only support for/while loops, but probably not. It also doesn't take 'many years' to internalise for/while loops, these are fairly basic constructs which are learned by most novices in the beginning of an introductory programming course.

> (x + y) is an expression rather than a statement. The J programmer can embed (x + y) in a larger expression, perhaps a matrix multiplication (w +/ . * (x + y)) which adds the equivalent of three more nested loops, but is still a single expression. Expressions can be combined; statements cannot.

(x + y) is going to be an expression in almost any language...

> Many modern languages have iterators, which slightly streamline the For loop, but without addressing its fundamental deficiencies.

What are the 'fundamental deficiencies'?

This seems like a pretty low-effort write-up.

- - - - - - -

(end of reformatted version of NOGDP's parent comment, please direct any replies to the original)

Post reply on HN