Each: Difference between revisions

Jump to navigation Jump to search
2,545 bytes added ,  09:39, 7 April 2022
no edit summary
(→‎Documentation: BQN link)
No edit summary
Line 1: Line 1:
{{Built-in|Each|¨}} is a [[primitive operator|primitive]] [[monadic operator]] which applies its [[operand]] to each [[element]] of the [[argument]]s, and returns an array whose elements are the results. If two arguments are given, their elements are matched using [[conformability]] rules.
{{Built-in|Each|¨}} is a [[primitive operator|primitive]] [[monadic operator]] which applies its [[operand]] to each [[element]] of the [[argument]]s, and returns an array whose elements are the results. If two arguments are given, their elements are matched using [[conformability]] rules.
 
== Definition ==
Each is defined only in [[Nested array model|nested]] APLs. Some [[Flat array model|flat]] APLs obtain analogous functionality by using an [[Under]] operator with [[close composition]] along with the [[Function rank|rank]]-0 function [[Disclose]] (or Unbox). In [[SHARP APL]] this is written <source lang=apl inline>f¨></source>. In [[J]] it is <source lang=j inline>f&.></source>.
Each is defined only in [[Nested array model|nested]] APLs. Some [[Flat array model|flat]] APLs obtain analogous functionality by using an [[Under]] operator with [[close composition]] along with the [[Function rank|rank]]-0 function [[Disclose]] (or Unbox). In [[SHARP APL]] this is written <source lang=apl inline>f¨></source>. In [[J]] it is <source lang=j inline>f&.></source>.


Each differs from the [[Rank operator]] with rank 0 in that the operand arguments and results are not [[enclose]]d. As the [[elements]] of a nested array they need not be [[scalar]].
Each differs from the [[Rank operator]] with rank 0 in that the operand arguments and results are not [[enclose]]d. As the [[elements]] of a nested array they need not be [[scalar]].


For example,
The Each operator has no effect on [[scalar function]]s, since these functions already map over each array element. For example, both expressions below have the same meaning, since <source lang=apl inline>+</source> is a scalar function.
<source lang=apl>
      1 + 1 2 3 4
2 3 4 5
      1 +¨ 1 2 3 4
2 3 4 5
</source>


== Examples ==
<source lang=apl>
<source lang=apl>
       1,1 2 3 ⍝ join 1 with 1 2 3
       1,1 2 3 ⍝ join 1 with 1 2 3
Line 26: Line 33:
</source>
</source>


The Each operator has no effect on [[scalar function]]s, since these functions already map over each array element.
== Notable uses ==
=== Mapping ===
It is very common to pair up an entire array with each element of a different array. There are two common ways to do this using Each. The first is to [[enclose]] the argument that is to be used as a whole for each element of the other array:
<source lang=apl>
      (⊂10 20 30),¨1 2 3  ⍝ Computes (10 20 30,1)(10 20 30,2)(10 20 30,3)
┌──────────┬──────────┬──────────┐
│10 20 30 1│10 20 30 2│10 20 30 3│
└──────────┴──────────┴──────────┘
      10 20 30,¨⊂1 2 3    ⍝ Computes (10,1 2 3)(20,1 2 3)(30,1 2 3)
┌────────┬────────┬────────┐
│10 1 2 3│20 1 2 3│30 1 2 3│
└────────┴────────┴────────┘
</source>
The other method is by [[bind]]ing the argument that is to be used as a whole to the function, deriving a monadic function, which is then applied using Each:
<source lang=apl>
      10 20 30∘,¨1 2 3  ⍝ Computes (10 20 30,1)(10 20 30,2)(10 20 30,3)
┌──────────┬──────────┬──────────┐
│10 20 30 1│10 20 30 2│10 20 30 3│
└──────────┴──────────┴──────────┘
      ,∘1 2 3¨10 20 30  ⍝ Computes (10,1 2 3)(20,1 2 3)(30,1 2 3)
┌────────┬────────┬────────┐
│10 1 2 3│20 1 2 3│30 1 2 3│
└────────┴────────┴────────┘
</source>
Note how binding a right argument derives a monadic function which still takes its single argument on the right.
=== Selecting ===
An enclosed array is a [[scalar]], which is subject to [[scalar extension]]. This can be used to simulate [[Outer Product|outer product]] by a one-sided Each (pair the entire right argument with each element of the left argument, or vice versa). A notable application of this behavior is the "chipmunk idiom" <source lang=apl inline>X⊃¨⊂Y</source>, which simulates <source lang=apl inline>Y[X]</source> for (possibly nested) [[vector]] Y and [[simple]] X:


For example, both expressions below have the same meaning, since <source lang=apl inline>+</source> is a scalar function.
<source lang=apl>
<source lang=apl>
       1 + 1 2 3 4
       (2 2⍴1 2 2 1)⊃¨⊂(1 2)(3 4)(5 6)  ⍝ Computes (1 2)(3 4)(5 6)[2 2⍴1 2 2 1]
2 3 4 5
┌───┬───┐
      1 +¨ 1 2 3 4
│1 2│3 4│
2 3 4 5
├───┼───┤
│3 4│1 2│
└───┴───┘
</source>
</source>


Navigation menu