In APL, are one-liners preferred to more traditional variable-heavy code?

StackOverflow https://stackoverflow.com/questions/17873885

  •  04-06-2022
  •  | 
  •  

문제

Coming from a programmer of Java and Python, the syntax of the elegant and powerful APL solutions to problems often are confusingly lengthy. The code I have written could look equally formidable but I prefer to variable chunks with good variable names for my understanding. Which is the more accepted developmental process? Is there a down-side to having more lines of code even if the computation is identical (albeit with a tad more memory usage due to variables).

도움이 되었습니까?

해결책

No. Not by me, and not if you value maintainabilty, clarity, and even efficiency (one liners can in fact be quite inefficient.) There is no downside to having more lines and less lengthy one-liners.

The question of course hinges on what is a one-liner? In APL much can be done in a short one-liner that may require many lines in a conventional language. Nothing is wrong with expressing a single concept in one line of code. For example, there is little to be gained by breaking an expression for deleting the leading blanks from a string into its component parts (except perhaps in an introductory classroom):

(∨\' '≠a)/a 

But just like English, a sentence can get too long, and even though it is grammatically correct, it is improved by breaking it into smaller parts.

In other words, short one-liners are good. Long one-liners are bad. Obviously the difference between the two is more art than science and a matter of taste as well.

A very good example of code that I think exhibits the right balance is the dfns collection from Dyalog APL.

다른 팁

The tendency for dense one-line APL solutions probably came from the fact that in the early days (1970s), legacy editing of functions on teleprinter terminals was very tedious. The default editor supplied with APL\360 was brutally primitive even compared to EDLIN from early IBM PC-DOS. Also, earlier versions of APL, such as APL/1130, ran on slow hardware which was said to require a disk read per line of code. Only in the 1980s did decent full screen editors finally arrive which made it far easier to author decent-looking code.

That was then, this is now.

Today, there is no reason, other than perhaps entertainment, to pack too much in a single line. The tendency is to go for readability as it is far more important than any miniscule CPU or space saving (a non-issue on modern hardware) one may realise coding this way.

What is accepted is code which you and your co-workers can read days, months, or years after you've written it.

I have been a professional APL application developer for over 20 years. I think I can probably count the number of one-liners I have written on my fingers and toes over that period. For small utility functions, a 1-line implementation can be useful and appropriate, otherwise it is not. Some APLers do write overly dense and hard-to-maintain code (although spaghetti C can be as bad or worse). They don't in any shop where I have any say. This is one of the things that marks an amateur and inexperienced coder off from an experienced professional developer. APL is at least as much in need of comments, meaningful variable names, and other aspects of clear, documented or self-documenting code as any other coding tool, and good APLers use it. One-liners can be useful exercises in understanding the language, but are rarely good in production or serious code.

I have been an APL developer for about three years, and I find that I like to work out problems as one long line, and then refine it. When it is one long line, it is easy (for me)to see if there are similar 'chunks' of code that can be abstracted to simple one-liners as named lambdas.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top