3

I have

dummytxt←'abcdefghijk'
texttoadd←'down'
rfikv←20 30 50

and need following output

 defghijk20down  defghijk30down  defghijk50down 

I can do it with:

scenv←(¯10↑¨(⊂dummytxt),¨⍕¨rfikv),¨⊂texttoadd

but please help me to write without each operator but using rank

I use Dyalog APL, but please do not use trains.

Thank you

  • `↓(dummytxt{¯10↑⍺,⍕⍵}⍤1 0⊢rfikv),⍤1⊢texttoadd` ? – ngn Oct 05 '19 at 11:30
  • Rank is used for orthotopic (rectangular in the N dimension sense) data. If your data is ragged (uneven lengths), and therefore not orthotopic/rectangular, as yours is. you want each, as you have it. Right tool for the right job and all that. – Dan Bron Oct 05 '19 at 18:33
  • @DanBron OP seeks to increase her understanding of Rank by translating a very Each'y expression. Btw, the data *is* rectangular since it only consists of simple scalars and vectors, which obviously cannot be ragged. – Adám Oct 05 '19 at 20:59

1 Answers1

1

Expressions using Each, like f¨x, can be expressed in terms of Rank as {⊂f⊃⍵}⍤0⊢x (note that is to separate the array right operand, 0 from the array right argument x). In other words, on the scalars of the argument we:

  1. disclose the scalar: ⊃⍵
  2. apply the function: f⊃⍵
  3. enclose the result: ⊂f⊃⍵

A similar expression applies for the dyadic case, x f¨y, but we need to:

  1. disclose both scalars: (⊃⍺)(⊃⍵)
  2. apply the function: (⊃⍺)f(⊃⍵)
  3. enclose the result: ⊂(⊃⍺)f(⊃⍵)

This gives us x{⊂(⊃⍺)f(⊃⍵)}⍤0⊢y. We can thus use Rank to build our own Each operator which allows both monadic and dyadic application of the derived function:

      Each←{⍺←⊢ ⋄ ⍺ ⍺⍺{×⎕NC'⍺':⊂(⊃⍺)⍺⍺(⊃⍵) ⋄ ⊂⍺⍺⊃⍵}⍤0⊢⍵}
      (¯10↑Each(⊂dummytxt),Each⍕Each rfikv),Each⊂texttoadd
 defghijk20down  defghijk30down  defghijk50down 

Alternatively, we can substitute the two simpler equivalences into your expression:

      (¯10{⊂(⊃⍺)↑(⊃⍵)}⍤0⊢(⊂dummytxt){⊂(⊃⍺),(⊃⍵)}⍤0{⊂⍕⊃⍵}⍤0⊢rfikv){⊂(⊃⍺),(⊃⍵)}⍤0⊂texttoadd
 defghijk20down  defghijk30down  defghijk50down 

Notice that we are enclosing texttoadd so it becomes scalar, and then we use ⍤0 to address that entire scalar, only to disclose it again. Instead, we can use ⍤0 1 to say that want to use the entire vector right argument when applying the function, which in turn doesn't need to disclose its right argument:

      (¯10{⊂(⊃⍺)↑(⊃⍵)}⍤0⊢(⊂dummytxt){⊂(⊃⍺),(⊃⍵)}⍤0{⊂⍕⊃⍵}⍤0⊢rfikv){⊂(⊃⍺),⍵}⍤0 1⊢texttoadd
 defghijk20down  defghijk30down  defghijk50down 

rfikv and ¯10 are a simple scalars, so disclosing them has no effect:

      (¯10{⊂⍺↑(⊃⍵)}⍤0⊢(⊂dummytxt){⊂(⊃⍺),(⊃⍵)}⍤0{⊂⍕⍵}⍤0⊢rfikv){⊂(⊃⍺),⍵}⍤0 1⊢texttoadd
 defghijk20down  defghijk30down  defghijk50down 

dummytxt is in the same situation as texttoadd above, but as left argument, so we can skip the enclose-disclose and ask Rank to use the entire vector left argument; ⍤1 0:

      (¯10{⊂⍺↑(⊃⍵)}⍤0⊢dummytxt{⊂⍺,(⊃⍵)}⍤1 0{⊂⍕⍵}⍤0⊢rfikv){⊂(⊃⍺),⍵}⍤0 1⊢texttoadd
 defghijk20down  defghijk30down  defghijk50down 

This is about as simple as it gets using a general method. However, if we instead observe that the only non-scalar is rfikv, we can treat dummytxt and texttoadd as global constants and express the entire thing as a single ⍤0 function application on rfikv:

      {⊂(¯10↑dummytxt,⍕⍵),texttoadd}⍤0⊢rfikv
 defghijk20down  defghijk30down  defghijk50down 

Of course, Each can do this too:

      {(¯10↑dummytxt,⍕⍵),texttoadd}¨rfikv
 defghijk20down  defghijk30down  defghijk50down 
Adám
  • 6,573
  • 20
  • 37
  • Adám, thank you for very detailed answer (and all you guys) 1. In last answers, you forgot about ``¯10``, that is why I assume final answer is ``{⊂(¯10↑dummytxt,(⍕⍵)),texttoadd}⍤0⊢rfikv`` – Yuliia Serhiienko Oct 07 '19 at 08:07
  • @YuliiaSerhiienko You're welcome. Thanks for pointing that out — fixed. And sorry for messing up your post. – Adám Oct 07 '19 at 08:11
  • Could you please explain what means ``⍺⍺`` here ``Each←{⍺←⊢ ⋄ ⍺ ⍺⍺{×⎕NC'⍺':⊂(⊃⍺)⍺⍺(⊃⍵) ⋄ ⊂⍺⍺⊃⍵}⍤0⊢⍵}`` – Yuliia Serhiienko Oct 07 '19 at 08:13
  • @YuliiaSerhiienko To avoid clutter, maybe ask a separate question: *I saw the following code in [this](https://stackoverflow.com/a/58252281/5306507) answer: `Each←{⍺←⊢ ⋄ ⍺ ⍺⍺{×⎕NC'⍺':⊂(⊃⍺)⍺⍺(⊃⍵) ⋄ ⊂⍺⍺⊃⍵}⍤0⊢⍵}`…* – Adám Oct 07 '19 at 08:16
  • Adám, thank you, created separate [question](https://stackoverflow.com/questions/58299517/how-to-build-own-each-operator-using-rank-operator-in-dyalog-apl) – Yuliia Serhiienko Oct 09 '19 at 08:01