4

I want to select elements from an array based on some test. Currently, I am trying to do that with a compression, and I would like to write it as a tacit function. (I'm very new to APL, so feel free to suggest other options.) Below is a minimal (not-)working example.

The third line below shows that I can use the testing function f on vec and then do the compression, and the fifth line shows I can apply the identity function to vec (as expected). So based on my understanding of the train documentation, I should be able to make a fork from f and with / as the center prong. Below shows that this does not work, and I presume it is because Dyalog is interpreting the sixth and eighth lines as doing an f-reduce. Is there a way to indicate that I want a compression train and not a reduce? (and/or is there a better way to do this altogether?)

      vec ← 10⍴⍳3
      f ← {⍵≤2}
      (f vec) / vec
1 2 1 2 1 2 1
      (f vec) / (⊢ vec)
1 2 1 2 1 2 1
      (f/⊢) vec
1
      (f(/)⊢) vec
1
j_v_wow_d
  • 511
  • 2
  • 11
  • 1
    I've added [a section](https://aplwiki.com/wiki/Tacit_programming#Problems_caused_by_function-operator_overloading) addressing this to the wiki article. Thank you for bringing the issue up! – Adám Feb 18 '22 at 12:22

1 Answers1

4

Yes, by making / an operand, it is forced to behave as a function. As per APL Wiki, applying atop the result of / solves the problem:

      vec ← 10⍴⍳3
      f ← {⍵≤2}
      (f⊢⍤/⊢) vec
1 2 1 2 1 2 1
Adám
  • 6,573
  • 20
  • 37
  • For anyone else that comes across this question, this can also be accomplished with the over operator (which was not mentioned on the linked wiki page), but this also requires extra parentheses: `(f(/⍥⊢)⊢) vec` – j_v_wow_d Feb 18 '22 at 08:01
  • 1
    @j_v_wow_d Yes, any forced use of `/` as operand will do. So too `f(/⍨⍨)⊢` or `⊢(/⍨)f` but using Atop is the most concise, and the only way to avoid the extra parenthesis. An entirely different method is dfn-wrapping: `{⍺/⍵}` which you can optionally give a name. – Adám Feb 18 '22 at 08:03
  • If we were to reinvent APL from scratch, would we ever use the same symbol (here `/`) for both a function and an operator? – Jeppe Stig Nielsen Mar 07 '22 at 09:22
  • @JeppeStigNielsen [No](https://mlochbaum.github.io/BQN/commentary/why.html#apl). – Adám Mar 07 '22 at 09:24