Search code examples
apl

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


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


Solution

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