Idiom

From APL Wiki
Revision as of 08:53, 7 August 2023 by Adám Brudzewsky (talk | contribs) (Created page with "The term '''idioms''' is used in the community to mean either of two distinct (but overlapping) groups of expressions: * Idiomatic expression, that is, a construct that is often employed by experienced APLers and that these can therefore spot and understand without thinking about their constituent parts. * Optimised phrase, that is, a string of tokens that the interpreter recognises as a whole, directly computing a result rat...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The term idioms is used in the community to mean either of two distinct (but overlapping) groups of expressions:

  • Idiomatic expression, that is, a construct that is often employed by experienced APLers and that these can therefore spot and understand without thinking about their constituent parts.
  • Optimised phrase, that is, a string of tokens that the interpreter recognises as a whole, directly computing a result rather than evaluating each in turn.

These constructs are not necessarily optimal, and on occasion, they are later found to have flaws.

Ironically, the most common meaning of idiom, "a phrase or expression that typically presents a figurative, non-literal meaning attached to the phrase",[1] is never used in the context of APL, effectively rendering the word idiom an actual idiom in the most common sense of the word.

Idiomatic expressions

Some constructs tend to be needed often, and either lend themselves well to one particular phrasing, or is generally taught with a single spelling such that this spelling propagates throughout the community. Examples include:

  • X(≠⊆⊢)Y (using Dyalog APL or similar) or (X≠Y)⊂Y (using APL2 or similar) is often employed to split a character vector Y on a separator character X.
  • ((⍳⍴Y)=Y⍳Y)/Y (or similar), which for several decades was used to find the unique elements of Y in dialects without the primitive, including in many well-regarded works.[2] However, at Dyalog '17, Roger Hui presented his findings that this historical expression for Unique (which was used internally for the primitive) was was wrong on floating point numbers, due to some false assumptions about Index of,[3] and suggested a more involved model.[4]

Optimised phrases

A technique for increasing the performance of an interpreter is executing select idiomatic expressions as single units, instead of going through their constituent parts. This allows using alternate algorithms for achieving the result, which might be faster and/or use less memory. Examples include:

  • ↓⍉↑Y (in Dyalog APL or similar) or ↓⍉⊃Y (in APL2 or similar) to "transpose" a vector of vectors, also known as zipping. Applying Mix ( or ) would temporarily create a matrix containing all the elements, thus requiring a large contiguous memory allocation, whereas the result goes back to only requiring multiple smaller allocations. Computing the final result directly can be the difference between being able to perform the operation and exceeding memory limits.
  • {⍵[⍋⍵]} to sort: It is often faster to sort than grade, but most implementations do not include sorting primitives. Therefore, recognising this and similar constructs, allows the interpreter to speed up sorting. The idiom has, however, the downside of only working with vectors, while higher-rank arrays would need trailing semicolons inside the |square bracket. Dyalog APL therefore added {(⊂⍋⍵)⌷⍵} as an alternative, rank-agnostic idiom.

See also

External links

References

  1. Wikipedia. Idiom. Retrieved 2023-08-06.
  2. Including:
    1. Macklin, D., The APL Handbook of Techniques, 1978, page 32.
    2. Iverson, K.E., Operators and Functions, 1978, section 16.
    3. Iverson, K.E., Programming Style in APL, 1978, section 3.
    4. Berry, P.C., SHARP APL Reference Manual, 1979, page 154.
    5. Cheney, C.M., APL*PLUS Nested Arrays System, 1981, page 10.
    6. Iverson, K.E., Rationalized APL, 1983, section J.
    7. Gilman, L., and A. Rose, APL An Interactive Approach, third edition, 1984, page 119.
    8. Hui, R.K.W., Some Uses of { and }, 1987, section A.4.
    9. Brown, J.A., S. Pakin, and R. Polivka, APL2 at a Glance, 1988, page 222.
    10. FinnAPL idiom library, #205.
    11. Legrand, B., Mastering Dyalog APL, 2009, page 131.
  3. Hui, Roger. D07: Index-Of on Multiple Floats. Dyalog '17.
  4. Hui, Roger. D10: Tolerant Unique. Dyalog '17.


APL development [edit]
Interface SessionTyping glyphs (on Linux) ∙ FontsText editors
Publications IntroductionsLearning resourcesSimple examplesAdvanced examplesMnemonicsISO 8485:1989ISO/IEC 13751:2001A Dictionary of APLCase studiesDocumentation suitesBooksPapersVideosAPL Quote QuadVector journalTerminology (Chinese, German) ∙ Neural networksError trapping with Dyalog APL (in forms)
Sharing code Backwards compatibilityAPLcartAPLTreeAPL-CationDfns workspaceTatinCider
Implementation ResourcesOpen-sourceMagic functionPerformanceAPL hardware
Developers Timeline of corporationsAPL2000DyalogIBMIPSASTSC