1

I am wondering whether I should heavily use the each operator when using APL or should I try to find another solution to a given problem?

How is usage of this operator optimized in APL?

Lobachevsky
  • 1,222
  • 9
  • 17
syntagma
  • 23,346
  • 16
  • 78
  • 134
  • Unless you're using an open-source implementation of APL, the exact innards of each primitive is vendor-dependent and black-boxed. "Each" is considered a hidden loop but there are cases where it might be useful. If you apply a d-fn to each rather than an entire array, the program may run larger input sizes since d-fns will pop all local data off once they're done) – Chris Zhang Aug 18 '14 at 22:57

2 Answers2

1

Instead of writing something like

1 +" 2 x" 3|" (1 2 3) (4 5 6)    // sorry, no apl chars

I would write

{1 + 2 x 3 | omega}" (1 2 3) (4 5 6)

or

foo" (1 2 3) (4 5 6)

where foo contains the computation.

In other words, avoid chaining successive each operations when the functions can be combined. Dyalog DFNs {...} provide a great way to accomplish this.

But most of all, I would first try to solve the problem in the most natural way possible, without thinking about whether there is a workaround which happens to run faster. If the solution is good enough, that is, it performs adequately and you and your colleagues would be able to understand it some time in the future, I would leave it alone.

Lobachevsky
  • 1,222
  • 9
  • 17
  • In this particular example you don't need `¨` (each) as `+` `×` `|` will recursively apply to nested arrays anyway. – ngn Aug 12 '14 at 10:34
  • An explicit loop may be better for debugging when foo"data invokes a long running operation or iterative use of automation, .Net, or some external service. This depends on how the APL system handles interrupts. – Lobachevsky Oct 10 '14 at 10:50
0

In most cases, each will not have as good performance as an array-solution that does not use each, but sometimes it is the easiest and clearest way to code a complex expression. But if there are two ways to do the task of roughly equal clarity and complexity, and one does not use each, I would tend to choose that one. If it matters, of course, instrument and measure. Unless this is working with quite large data, it will quite likely not matter. The comment above on each used with calls to long running operations or external services is accurate.

David Siegel
  • 221
  • 2
  • 19