Idiom: Difference between revisions
(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...") |
|||
Line 27: | Line 27: | ||
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: | 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: | ||
* <syntaxhighlight lang=apl inline>↓⍉↑Y</syntaxhighlight> (in Dyalog APL or similar) or <syntaxhighlight lang=apl inline>↓⍉⊃Y</syntaxhighlight> (in APL2 or similar) to "[[transpose]]" a vector of vectors, also known as [[wikipedia:zipping (computer science)|zipping]]. Applying [[Mix]] (<syntaxhighlight lang=apl inline>↑</syntaxhighlight> or <syntaxhighlight lang=apl inline>⊃</syntaxhighlight>) 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. | * <syntaxhighlight lang=apl inline>↓⍉↑Y</syntaxhighlight> (in Dyalog APL or similar) or <syntaxhighlight lang=apl inline>↓⍉⊃Y</syntaxhighlight> (in APL2 or similar) to "[[transpose]]" a vector of vectors, also known as [[wikipedia:zipping (computer science)|zipping]]. Applying [[Mix]] (<syntaxhighlight lang=apl inline>↑</syntaxhighlight> or <syntaxhighlight lang=apl inline>⊃</syntaxhighlight>) 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. | ||
* <syntaxhighlight lang=apl inline>{⍵[⍋⍵]}</syntaxhighlight> to sort: It is often faster to sort than [[grade]], but most implementations do not include sorting [[primitive]]s. Therefore, recognising this and similar constructs, allows the interpreter to speed up sorting. The idiom has, however, the downside of only working with [[vector]]s, while higher-[[rank]] arrays would need trailing semicolons inside the [[bracket indexing | * <syntaxhighlight lang=apl inline>{⍵[⍋⍵]}</syntaxhighlight> to sort: It is often faster to sort than [[grade]], but most implementations do not include sorting [[primitive]]s. Therefore, recognising this and similar constructs, allows the interpreter to speed up sorting. The idiom has, however, the downside of only working with [[vector]]s, while higher-[[rank]] arrays would need trailing semicolons inside the [[bracket indexing|square bracket]]. [[Dyalog APL]] therefore added <syntaxhighlight lang=apl inline>{(⊂⍋⍵)⌷⍵}</syntaxhighlight> as an alternative, rank-agnostic idiom. | ||
== See also == | == See also == |
Latest revision as of 14:35, 6 March 2024
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 vectorY
on a separator characterX
.((⍳⍴Y)=Y⍳Y)/Y
(or similar), which for several decades was used to find the unique elements ofY
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
- APLcart — a maintained collection of short phrases, including idioms
- FinnAPL idiom library — the traditional list of idioms
External links
- Dyalog: Documentation, idiom list
References
- ↑ Wikipedia. Idiom. Retrieved 2023-08-06.
- ↑ Including:
- Macklin, D., The APL Handbook of Techniques, 1978, page 32.
- Iverson, K.E., Operators and Functions, 1978, section 16.
- Iverson, K.E., Programming Style in APL, 1978, section 3.
- Berry, P.C., SHARP APL Reference Manual, 1979, page 154.
- Cheney, C.M., APL*PLUS Nested Arrays System, 1981, page 10.
- Iverson, K.E., Rationalized APL, 1983, section J.
- Gilman, L., and A. Rose, APL An Interactive Approach, third edition, 1984, page 119.
- Hui, R.K.W., Some Uses of { and }, 1987, section A.4.
- Brown, J.A., S. Pakin, and R. Polivka, APL2 at a Glance, 1988, page 222.
- FinnAPL idiom library, #205.
- Legrand, B., Mastering Dyalog APL, 2009, page 131.
- ↑ Hui, Roger. D07: Index-Of on Multiple Floats. Dyalog '17.
- ↑ Hui, Roger. D10: Tolerant Unique. Dyalog '17.
APL development [edit] | |
---|---|
Interface | Session ∙ Typing glyphs (on Linux) ∙ Fonts ∙ Text editors |
Publications | Introductions ∙ Learning resources ∙ Simple examples ∙ Advanced examples ∙ Mnemonics ∙ ISO 8485:1989 ∙ ISO/IEC 13751:2001 ∙ A Dictionary of APL ∙ Case studies ∙ Documentation suites ∙ Books ∙ Papers ∙ Videos ∙ APL Quote Quad ∙ Vector journal ∙ Terminology (Chinese, German) ∙ Neural networks ∙ Error trapping with Dyalog APL (in forms) |
Sharing code | Backwards compatibility ∙ APLcart ∙ APLTree ∙ APL-Cation ∙ Dfns workspace ∙ Tatin ∙ Cider |
Implementation | Resources ∙ Open-source ∙ Magic function ∙ Performance ∙ APL hardware |
Developers | Timeline of corporations ∙ APL2000 ∙ Dyalog ∙ IBM ∙ IPSA ∙ STSC |