Idiom: Difference between revisions

Jump to navigation Jump to search
m
(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||square bracket]]. [[Dyalog APL]] therefore added <syntaxhighlight lang=apl inline>{(⊂⍋⍵)⌷⍵}</syntaxhighlight> as an alternative, rank-agnostic idiom.
* <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 ==

Navigation menu