Live data from Hacker News

Loopless Programming

code.jsoftware.com

61–70 of 129 posts

Re: Loopless Programming

#61
post #7

As far as explicit loops, I probably use write out a loop once a month and maybe not even that often. Using map/filter/reduce [as well as sugar funcs until/any/all] solves virtually all the common cases of working with lists. Granting it's not sufficient if you're writing specialized code like sorting arrays efficiently, but for general development, going higher-order is the way to go.

As the song says:

    All I ever wanted
    All I ever needed
    Is here
    In ML
    Loops are very
    Unnecessary
    map will work just as well
But the function you pass to map or filter is called once for every sequence element, and reduce is nothing more than a foreach loop with explicit dataflow. So, although I agree that these are often better than a for loop, they are not the same thing as making the looping implicit.

Re: Loopless Programming

#62
post #60
post #58

Earlier quoted context omitted.

It's disappointing to see someone aggressively defending their ignorance like this. It's okay if you don't understand what the article is saying, but it's unfortunate that you are blaming that on the article. Maybe your time would be better spent solving some coding challenges in J or another array language like Dyalog APL, Kona, or A+, then comparing your solutions with others’ using the same language, rather than p…

Are you trying to address anything I've said, or just ranting for another reason?

Bless your heart. The former.

Re: Loopless Programming

#63
post #18

Tl;dr : APL has implicit loops because almost everything is an array, and mentioning an array implies a loop to process it. In C++, nowadays, people are encouraged to use Standard Library algorithms in preference to most loops; and to make an algorithm out of any loop that can't be cleanly replaced with a Standard one, and call that. The reasoning is similar to that explained in the article, except that saying which…

From what I've read, your last paragraph is true, but only because the naming often isn't necessary. The importance of abstraction in a codebase that is 60 pages of code is high, but it fades away if the equivalent J program can be 5 pages of expressions. Instead of defining some numerical operation that is only 5 characters long, just use the 5 characters in the 2 to 3 places where it is needed in your 5 pages of code. This way everything is explicit and you don't have to reference back to the definition. This would be an awful idea in Java, but it works great in J and APL. Aaron Hsu talks about this with his parallel APL compiler and refers to this as working at the macro level. Note that he has plenty of named items that are passed around in a data flow fashion, but he doesn't need to name every little function.

Re: Loopless Programming

#65
I still remember as a kid back in the 80s when a friend and I were making BASIC games on our Tandy 1000s with just the reference manual that came with the computer, and he was trying to explain to me what a FOR loop was, and I was not getting it. "Why would you want to do the same thing twice?"

Just this morning my own 10-year-old was looking over my shoulder while I was debugging some C, and I explained for loops to him. Since he plays violin I said it was like a repeat in music, and he seemed to grok that right away.

My other memory of those days is, after years of dismissing GOSUB as useless ("Why would I want to go back to the same place I just left?"), finally having a flash of enlightenment and getting the point. It's a function call! (Not that I knew what those were....)

Sorry this has nothing to do with the wiki page. :-) Except maybe that to at least one kid loopy programming was unnatural.

Re: Loopless Programming

#66

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.

My favorite example is not the commonly seen average function in the J library (avg:= +/ % $), but the one that Dyalog APL has with their interactive tutorial that looked a little weird to me until I figured out it didn't just work over lists, but arbitrary dimensions like an array of 2x2 matrices. I just laughed to myself...oh what magic the gods have wrought.

Unfortunately, my programming needs are high performance scientific computing where APL just doesn't have enough horsepower in most cases (J has some ability to call out to BLAS/LAPACK, but Julia makes it so easy to work with sparse matrices that it has become my go-to Lang for that work) and I also do scripting (Python, Bash, Powershell, and a little Perl6) and J/APL are a little awkward in these areas IMO.

Re: Loopless Programming

#69

Most programmers have worked in a largely loopless programming language: SQL. IT lets you easily build the same sort of ‘Boolean state for every item’ as this discusses, but it requires you to be much more explicit about which items you want to line up next to one another if you’re joining two lists together. In modern languages we usually just use mapreduce like functional approaches to handle the same sort of thing…

From someone who has experience in mostly SQL, Python, Linux commands, and a smattering of other languages that I've played with (Haskell, Common Lisp, F#, Ada, Prolog, Julia, Forth, Fortran...etc) I can say that J and modern APL systems are completely different in a lot of ways. Yes functional languages have a lot built around map/fold/filter that is very similar to J's tacit programming, but the implementation make…

It seems to me like it is terse because this is a DSL for dealing with vectors and arrays of numbers.

Sure the equivalent C for loops are a lot more wordy for the problems that this language solves, but you'll drive yourself crazy trying to write a simple event loop for your GUI in J.

Re: Loopless Programming

#70
post #62

Earlier quoted context omitted.

Bless your heart. The former.

In that case, maybe you might like to make an attempt at addressing his points.

There's no point in trying to explain things to someone who's aggressively defending their ignorance. (I have lots of experience being the aggressively ignorant guy.) Step one is realizing other people might know something you don't. After that it becomes useful to talk to you. Someone who's determined to dismiss what they're hearing can always find an excuse—especially in programming, where everything is Turing-complete anyway.
Post reply on HN