Unique: Difference between revisions

Jump to navigation Jump to search
7 bytes removed ,  11:51, 25 October 2019
class= → lang=
Miraheze>Adám Brudzewsky
No edit summary
Miraheze>Adám Brudzewsky
(class= → lang=)
Line 4: Line 4:


When called on the [[string]] "Mississippi", Unique keeps the first three letters and a later "p", but removes all subsequent occurrences.
When called on the [[string]] "Mississippi", Unique keeps the first three letters and a later "p", but removes all subsequent occurrences.
<source class=apl>
<source lang=apl>
       ∪ 'Mississippi'
       ∪ 'Mississippi'
Misp
Misp
</source>
</source>
It works on nested arrays as well:
It works on nested arrays as well:
<source class=apl>
<source lang=apl>
       ∪ v←'CAT' 'DOG' 'CAT' 'DUCK' 'DOG' 'DUCK'
       ∪ v←'CAT' 'DOG' 'CAT' 'DUCK' 'DOG' 'DUCK'
┌───┬───┬────┐
┌───┬───┬────┐
Line 16: Line 16:
</source>
</source>
In some languages, such as [[Dyalog APL]] and [[J]], Unique applies to the [[major cells]] of an array, including those with rank greater than 1:
In some languages, such as [[Dyalog APL]] and [[J]], Unique applies to the [[major cells]] of an array, including those with rank greater than 1:
<source class=apl>
<source lang=apl>
       ⊢x ← (⍳10)∘.∨2 3 6
       ⊢x ← (⍳10)∘.∨2 3 6
1 1 1
1 1 1
Line 46: Line 46:


The following example shows why we must be careful about how cells are matched when tolerant comparison is involved. Here we produce a vector in which the first and second elements match, as do the second and third, but the first and third elements do not! Unique produces an array which contains an element matching every element of the original array, but a less careful definition (for example, excluding every element which matches an earlier one) would fail to satisfy this property.
The following example shows why we must be careful about how cells are matched when tolerant comparison is involved. Here we produce a vector in which the first and second elements match, as do the second and third, but the first and third elements do not! Unique produces an array which contains an element matching every element of the original array, but a less careful definition (for example, excluding every element which matches an earlier one) would fail to satisfy this property.
<source class=apl>
<source lang=apl>
       ⎕CT←1e¯14
       ⎕CT←1e¯14
       ⎕PP←18 ⍝ Print all digits
       ⎕PP←18 ⍝ Print all digits
Line 65: Line 65:


For [[Vector|vectors]] the following implementation based on [[Union]] can be used. It repeatedly adds cells of the argument to an accumulated unique vector <code>u</code>, using Union so that duplicate cells are never added.
For [[Vector|vectors]] the following implementation based on [[Union]] can be used. It repeatedly adds cells of the argument to an accumulated unique vector <code>u</code>, using Union so that duplicate cells are never added.
<source class=apl>
<source lang=apl>
VecUnique ← {
VecUnique ← {
     u ← 0↑⍵
     u ← 0↑⍵
Line 73: Line 73:
{{Works in|[[Dyalog APL]]}}
{{Works in|[[Dyalog APL]]}}
The accumulation above resembles a [[reduction]]—in fact, it is a reverse [[insertion]]. The following implementation written with Reduce works for [[simple]] vectors in [[Nested array model|nested]] APLs because reduction is equivalent to insertion followed by [[Enclose]] on such vectors:
The accumulation above resembles a [[reduction]]—in fact, it is a reverse [[insertion]]. The following implementation written with Reduce works for [[simple]] vectors in [[Nested array model|nested]] APLs because reduction is equivalent to insertion followed by [[Enclose]] on such vectors:
<source class=apl>
<source lang=apl>
VecUnique ← {⊃∪⍨/⌽⍵}
VecUnique ← {⊃∪⍨/⌽⍵}
</source>
</source>
{{Works in|[[Dyalog APL]],[[ngn/apl]]}}
{{Works in|[[Dyalog APL]],[[ngn/apl]]}}
It can be modified to obtain a full model for Unique by enclosing enough times, and converting scalars to vectors with [[Replicate]] at the end:
It can be modified to obtain a full model for Unique by enclosing enough times, and converting scalars to vectors with [[Replicate]] at the end:
<source class=apl>
<source lang=apl>
Unique ← {1/↑↑⌽∪/⌽⊂∘⊂⍤¯1⊢⍵}
Unique ← {1/↑↑⌽∪/⌽⊂∘⊂⍤¯1⊢⍵}
</source>
</source>

Navigation menu