Readability: Difference between revisions
No edit summary |
m (→Examples) |
||
Line 2: | Line 2: | ||
== Examples == | == Examples == | ||
=== Gilman & Rose === | === Gilman & Rose === | ||
In [[Books#APL_.E2.80.95_An_Interactive_Approach|APL ― | In [[Books#APL_.E2.80.95_An_Interactive_Approach|APL ― An Interactive Approach]], the authors describe the following code, which computes the correlation coefficient, as “almost pornographic”:<!--- For the effect, this is intentionally not syntax coloured ---> | ||
:<code>r←(+/x×y)÷((+/(x←x-(+/x)÷⍴x)*2)×+/(y←y-(+/y)÷⍴y)*2)×.5</code> | :<code>r←(+/x×y)÷((+/(x←x-(+/x)÷⍴x)*2)×+/(y←y-(+/y)÷⍴y)*2)×.5</code> | ||
By splitting the expression intro even a moderate number of pieces, a symmetry is revealed: | By splitting the expression intro even a moderate number of pieces, a symmetry is revealed: | ||
Line 35: | Line 35: | ||
</source> | </source> | ||
A new [[namespace]], with the original value of <source lang=apl inline>ns</source> as name, is created inside <source lang=apl inline>container</source> and the character representation <source lang=apl inline>'#.container.ns'</source> is returned from <source lang=apl inline>⎕NS</source> to <source lang=apl inline>⍎</source> which evaluates the name to a reference, that in turn replaces the previous value of <source lang=apl inline>ns</source>. | A new [[namespace]], with the original value of <source lang=apl inline>ns</source> as name, is created inside <source lang=apl inline>container</source> and the character representation <source lang=apl inline>'#.container.ns'</source> is returned from <source lang=apl inline>⎕NS</source> to <source lang=apl inline>⍎</source> which evaluates the name to a reference, that in turn replaces the previous value of <source lang=apl inline>ns</source>. | ||
== See also == | == See also == | ||
* [[Semantic density]] | * [[Semantic density]] | ||
* [[Function-operator overloading]] | * [[Function-operator overloading]] | ||
{{APL syntax}} | {{APL syntax}} |
Revision as of 07:34, 27 September 2020
Maintaining readability of APL can take a special effort. It is easy to write very dense code, and the mathematical look of APL can encourage usage of single-letter names. Traditionally, APLers use the term pornography to describe code that is hard to read, or uses unusual constructs. Code golf often results in pornographic code.
Examples
Gilman & Rose
In APL ― An Interactive Approach, the authors describe the following code, which computes the correlation coefficient, as “almost pornographic”:
r←(+/x×y)÷((+/(x←x-(+/x)÷⍴x)*2)×+/(y←y-(+/y)÷⍴y)*2)×.5
By splitting the expression intro even a moderate number of pieces, a symmetry is revealed:
yVar←+/(y-(+/y)÷⍴y)*2 xVar←+/(x-(+/x)÷⍴x)*2 r←(+/x×y)÷(xVar×yVar)×0.5
This also avoids reusing variable names, and thus ensures that the code can be rerun from at any point. The chosen additional variable names are still short, but quite indicative of what they signify (variance). Finally, the .5
is expanded to 0.5
which helps to clarify that this is a decimal number and not an inner product.
IBM
The APL2 Idiom list includes the following entry:
X←'line1',0⍴Y←'line2'
⍝ Pornography. Combining two lines into one.
This was a common technique before Left was added to the language:
X←'line1' ⊣ Y←'line2'
The Diamond statement separator (⋄
) provides an alternative means of inlining multiple statements:
Y←'line2' ⋄ X←'line1'
Note that in all these cases, Y
is assigned first.
Morten Kromberg
Morten Kromberg asked one of his colleagues to “Please avoid this kind of pornography:”
ns(⍎container.⎕NS)←⍬
Avoiding the unusual modified assignment (using the 2-train ⍎⎕NS
as modifying function) helps:
ns←ns container.(⍎⎕NS) ⍬
Finally, splitting the 2-train apart makes it even clearer:
ns←⍎ns container.⎕NS ⍬
A new namespace, with the original value of ns
as name, is created inside container
and the character representation '#.container.ns'
is returned from ⎕NS
to ⍎
which evaluates the name to a reference, that in turn replaces the previous value of ns
.