2
\$\begingroup\$

I'm writing a small utility operator which applies two functions to an argument pair and strands the results. The pair can either be given as two arguments or as a single argument with two elements. Either way, the left operand is applied to the left value and the right operand to the right value:

_n_←{⍺←⊢ ⋄ (a b)←⍺ ⍵ ⋄ (⍺⍺ a)(⍵⍵ b)}

Here is example usage for computing the factorial and the negation:

      4 !_n_- 10
16 ¯10
      !_n_- 4 10
16 ¯10

And here's the reverse and the unique:

      3 1 4 1 5⌽_n_∪2 7 1 8
┌─────────┬───────┐
│5 1 4 1 3│2 7 1 8│
└─────────┴───────┘
      ⌽_n_∪(3 1 4 1 5)(2 7 1 8)
┌─────────┬───────┐
│5 1 4 1 3│2 7 1 8│
└─────────┴───────┘

However, this seems like a lot of plumbing for what is essentially a trivial operator. Can it be done more elegantly?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

If you allow Behind, then _n_←{⍺←⊣ ⋄ ⍺ ⍺⍺⍛,∘⍵⍵/⍤,⍵} is arguably aesthetically nicer. However, this will just blindly concatenate left and right arguments, so if we need higher-ranked ones to stay separate, then the less pretty _n_←{⍺←⊣ ⋄ ⍺ ⍶⍛,⍥⊂∘⍹/⍤,⍥⊂⍵} variation is probably needed.

New contributor
B. Wilson is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
3
  • \$\begingroup\$ This is so clever! However, my example case was too simple. Now updated. You can fix your code by inserting ⍥⊂ to the right of each , \$\endgroup\$
    – Adám
    May 25 at 13:13
  • \$\begingroup\$ Btw, even without Behind, we can write _n_←{⍺←⊣ ⋄ ⊃⍺,⍨∘⍺⍺⍨⍥⊂∘⍵⍵/⍤,⍥⊂⍵} \$\endgroup\$
    – Adám
    May 25 at 13:31
  • \$\begingroup\$ @Adám Drats. The Enclose-less version is so nice, I was hoping you just needed scalar arguments. Added. \$\endgroup\$
    – B. Wilson
    May 26 at 1:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.