Simple examples: Difference between revisions

Jump to navigation Jump to search
30 bytes added ,  12:29, 25 October 2019
no edit summary
Miraheze>Adám Brudzewsky
Miraheze>Marshall
No edit summary
Line 4: Line 4:
=== Indices of multiple elements ===
=== Indices of multiple elements ===
<code class="apl">∊</code> gives us a mask for elements (characters) in the left argument that are members of the right argument:
<code class="apl">∊</code> gives us a mask for elements (characters) in the left argument that are members of the right argument:
<pre class=apl>
<source lang=apl>
       'mississippi'∊'sp'
       'mississippi'∊'sp'
0 0 1 1 0 1 1 0 1 1 0
0 0 1 1 0 1 1 0 1 1 0
</pre>
</source>
<code class="apl">⍸</code> gives us the indices where true (1):
<code class="apl">⍸</code> gives us the indices where true (1):
<pre class=apl>
<source lang=apl>
       ⍸'mississippi'∊'sp'
       ⍸'mississippi'∊'sp'
3 4 6 7 9 10
3 4 6 7 9 10
</pre>
</source>
We can combine this into an anonymous infix (dyadic) function:
We can combine this into an anonymous infix (dyadic) function:
<pre class=apl>
<source lang=apl>
       'mississippi' (⍸∊) 'sp'
       'mississippi' (⍸∊) 'sp'
3 4 6 7 9 10
3 4 6 7 9 10
</pre>
</source>
{{Works in|[[Dyalog APL]], [[NARS2000]], [[dzaima/APL]]}}
{{Works in|[[Dyalog APL]], [[NARS2000]], [[dzaima/APL]]}}


=== Parenthesis nesting level ===
=== Parenthesis nesting level ===
First we compare all characters to the opening and closing characters;
First we compare all characters to the opening and closing characters;
<pre class=apl>
<source lang=apl>
       '()'∘.='plus(square(a),plus(square(b),times(2,plus(a,b)))'
       '()'∘.='plus(square(a),plus(square(b),times(2,plus(a,b)))'
0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1
</pre>
</source>
An opening increases the current level, while a closing decreases, so we convert this to ''changes'' (or ''deltas'') by subtracting the bottom row from the top row:
An opening increases the current level, while a closing decreases, so we convert this to ''changes'' (or ''deltas'') by subtracting the bottom row from the top row:
<pre class=apl>
<source lang=apl>
       -⌿'()'∘.='plus(square(a),plus(square(b),times(2,plus(a,b)))'
       -⌿'()'∘.='plus(square(a),plus(square(b),times(2,plus(a,b)))'
0 0 0 0 1 0 0 0 0 0 0 1 0 ¯1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 ¯1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 ¯1 ¯1 ¯1
0 0 0 0 1 0 0 0 0 0 0 1 0 ¯1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 ¯1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 ¯1 ¯1 ¯1
</pre>
</source>
The running sum is what we're looking for:
The running sum is what we're looking for:
<pre class=apl>
<source lang=apl>
       +\-⌿'()'∘.='plus(square(a),plus(square(b),times(2,plus(a,b)))'
       +\-⌿'()'∘.='plus(square(a),plus(square(b),times(2,plus(a,b)))'
0 0 0 0 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 2 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 3 2 1
0 0 0 0 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 2 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 3 2 1
</pre>
</source>
{{Works in|all APLs}}
{{Works in|all APLs}}
{{APL programming language}}
{{APL programming language}}
Anonymous user

Navigation menu