Live data from Hacker News

Loopless Programming

code.jsoftware.com

41–50 of 129 posts

Re: Loopless Programming

#41
post #25
post #17

Earlier quoted context omitted.

> (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.

You seem to have missed the point of the original remark that (x + y) is a statement, and you're missing the point of my clarification.

> "(x + y) is an expression rather than a statement".

The point isn't that (x + y) wouldn't be a statement in other languages, rather than point is that in languages the reader of the article is most likely to be familiar with, you would use a for loop to solve the problem, and obviously a for loop is a statement, not an expression. You can't compose statements as you can expressions. That was the only point the article was making there.

You clearly missed it, because you went on saying that (x + y) is an expression in almost any language, which while true, is irrelevant for languages where (x + y) isn't the way you would express the operation in question. Which, by the way, includes idiomatic C++, Haskell, and Rust, in all of which overloading (+) to do something so specific would at best be seen in a purpose-specific library (like numpy) only, but isn't automatically present in the language. That means it doesn't force you to think in that way, which J does, which is the point the article is making.

In most languages that most programmers use, for loops are common, and if all of those are replaced by expressions, you get a more expressive language (for dealing with arrays). That's the point that the article is making, and you haven't addressed it at all.

Re: Loopless Programming

#42
post #3

I recall another language that was trying to do something similar. In most languages you have to decide about x:1 versus x:* relationships very early and it’s a painful refactor to go back and fix it. This one tried to fix it as well so that much of the time you only changed the variable declaration and the code just worked. jQuery made a similar observation, and many of its functions are list comprehensions. If they…

What's the inverse behavior?

Re: Loopless Programming

#43
If you really want to play with loopless programming, I recommend Python’s numpy. It implements many of the same constructs, but using regular English words instead of punctuation.

Re: Loopless Programming

#44

> In J we do this by creating a Boolean list, one value (0 or 1) per item of z Does the word "creating" here mean you can't do a relatively simple check across array elements in J without allocating memory? E.g., soft realtime scheduling for such a simply operation is essentially just not possible in J?

I don't recall offhand the answer to this specific question, but many common operations in J are highly optimized under the hood. So even though you are doing "conceptually expensive" things, J will often implement them efficiently under the hood.

J is quite fast, shockingly so for an interpreted language.

Re: Loopless Programming

#45
post #41
post #25

Earlier quoted context omitted.

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.

You seem to have missed the point of the original remark that (x + y) is a statement, and you're missing the point of my clarification. > "(x + y) is an expression rather than a statement". The point isn't that (x + y) wouldn't be a statement in other languages, rather than point is that in languages the reader of the article is most likely to be familiar with, you would use a for loop to solve the problem, and obvio…

J has a big focus on array manipulation, the libraries in Rust for arrays provide implementations of the Add trait, so I’d disagreed that it is not “idiomatic” https://docs.rs/ndarray/0.13.0/ndarray/struct.ArrayBase.html...

Re: Loopless Programming

#46
post #41
post #25

Earlier quoted context omitted.

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.

You seem to have missed the point of the original remark that (x + y) is a statement, and you're missing the point of my clarification. > "(x + y) is an expression rather than a statement". The point isn't that (x + y) wouldn't be a statement in other languages, rather than point is that in languages the reader of the article is most likely to be familiar with, you would use a for loop to solve the problem, and obvio…

I can tell you that at least in Python, for loops are not common for math. In fact, if I saw a C-style for loop counting indices, I’d defect it in any code I review.

For simple things, you have list comprehensions and things like “sum”, while for complex things one should reach for numpy.

Re: Loopless Programming

#47
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

Yes, in trivial cases like this one, the compiler will generate assembly that's hard to improve upon. But if there were more meat on the bones there (as there is, most of the time, if you aren't just summing ints), this technique could help. As always, benchmark before and after and keep the best variant.

Re: Loopless Programming

#48
Almost any functional programming language has the same power and quality of life in programming, except with the added bonus that things have names.

If you come up with a new looping construct (which you probably won't), just create a type class for it and you're done; any data structure you want now has that construct, if you give it a sensible implementation.

It would be very unusual to write a Haskell program with explicit loops in it. The closest you ever get would be something like `forM_` and even that doesn't really count. I suppose a more direct example would be a recursive function call, but imperative programmers may be surprised at just how few of these actually crop up in production code.

Re: Loopless Programming

#49
post #45
post #41

Earlier quoted context omitted.

You seem to have missed the point of the original remark that (x + y) is a statement, and you're missing the point of my clarification. > "(x + y) is an expression rather than a statement". The point isn't that (x + y) wouldn't be a statement in other languages, rather than point is that in languages the reader of the article is most likely to be familiar with, you would use a for loop to solve the problem, and obvio…

J has a big focus on array manipulation, the libraries in Rust for arrays provide implementations of the Add trait, so I’d disagreed that it is not “idiomatic” https://docs.rs/ndarray/0.13.0/ndarray/struct.ArrayBase.html...

Well yes, but ndarray is a library, which is what I said. You certainly don't get this "for free" in the language, which means you aren't forced to think this way as a beginner in the language, which is why libraries like ndarray generally have a learning curve, in whatever language you find them. In J you have to get over that learning curve much earlier, because, as you said, the language is focused on it. (Which is the point the article is making, about how J changes the way you think. If you were writing an article about how Rust changes the way you think, I don't think you'd mention ndarray.)

Re: Loopless Programming

#50
post #43

If you really want to play with loopless programming, I recommend Python’s numpy. It implements many of the same constructs, but using regular English words instead of punctuation.

Well numpy doesn't totally obviate the need for loops though, although it is a good mental exercise to try to write as few loops as possible.
Post reply on HN