~ (Reflex) Adverb

Back to: Vocabulary

The reflexive adverb has its verb evaluate as a dyad with its own argument on left and right. Look for an otherwise monadic verb followed by ~ to identify reflexive.

   NB. Let verb f implement the algebraic function "x to the power of x"

   0 1 2 3 4 ^ 0 1 2 3 4
1 1 4 27 256
   
   ^~ 0 1 2 3 4
1 1 4 27 256

   f=: ^~

   f i.5
1 1 4 27 256

Common uses

To avoid having to specify the same noun as both left and right arguments to a given verb.

1. Produce a multiplication table

   */~ i.10   NB. instead of: (i.10) */ (i.10)
0 0  0  0  0  0  0  0  0  0
0 1  2  3  4  5  6  7  8  9
0 2  4  6  8 10 12 14 16 18
0 3  6  9 12 15 18 21 24 27
0 4  8 12 16 20 24 28 32 36
0 5 10 15 20 25 30 35 40 45
0 6 12 18 24 30 36 42 48 54
0 7 14 21 28 35 42 49 56 63
0 8 16 24 32 40 48 56 64 72
0 9 18 27 36 45 54 63 72 81

2. Sort items into descending order

   \: 'the' ; 'quick' ; 'brown' ; 'fox'
0 1 3 2
   \:~ 'the' ; 'quick' ; 'brown' ; 'fox'
+---+-----+---+-----+
|the|quick|fox|brown|
+---+-----+---+-----+

3. Key with self. (See explanation following code)

   #/.~ items   NB. tally of each item.
   NB. the items are in the same order as the nub of items

   14 #/.~@:([ smoutput)@:?.@:$ 4
2 0 0 2 1 0 2 0 0 3 1 0 3 1
3 6 3 2

   NB. ?. generates same data as before, and show the nub.
   14 ~.@:([ smoutput)@:?.@:$ 4
2 0 0 2 1 0 2 0 0 3 1 0 3 1
2 0 1 3

How to read 14 #/.~@:([ smoutput)@:?.@:$ 4

The two nouns 14 and 4 are separated by a blob of primitive verbs, conjunctions, adverbs, and a hook.

The blob forms a verb, making the sentence pattern noun verb noun.

Therefore, the verbal blob has dyadic context.

All of the conjunctions are @: .

The compound verb f@:g has infinite rank, and as a dyad X f@:g Y boils down to "do all of dyadic X g Y , and then do monadic f to that result".

There being several at conjunctions, we also need to know that f@:g Y means "do f to all g Y . So, $ the rightmost primitive, is the dyad. 14 shape 4 happens first, giving fourteen fours. Fixed seed roll ( ?. ) next computes 14 pseudo-random integers. Working leftward, we find the hook ([ smoutput) . Hook (f g) Y evaluates as Y (f g) Y . (Which is the same as reflexive (f g)~ Y .) Here, g is smoutput and f is "left". ([ smoutput) displays the the argument and passes it onward. Show without change is a useful debugging tool. The hook displays the random data.

Finally, the verb formed from # /. ~ executes. How do we know that #/. is dyadic key instead of monadic oblique? The reflexive adverb evaluates f~ Y as Y f Y . Key groups like items and applies its verb to each group. # tallies each group. The interpreter displays the result. The data contains 3 twos, 6 zeros, 3 ones, and 2 threes. How do we know the correspondence of the vector output? Replace the first verb of the compound verb with nub. The result of key is in the same order as the nub of the keys.

See Also

~ (Passive) Adverb

Back to: Vocabulary

Swaps the arguments to the verb preceding it. Thus, X f~ Y is the same as Y f X .

Passive (~) can reduce the number of parentheses a sentence needs, and it can help to write j in the "control_noun verb data" style.

   (% i.) 7x    NB. whoops!  Division is "backward".
_ 7 7r2 7r3 7r4 7r5 7r6
   
   intervals=: %~ i.
   intervals 7x
0 1r7 2r7 3r7 4r7 5r7 6r7

This hook pattern X (f~ g)~ Y arises. Wow! Rather than spinning your head around counting what might be even or odd instances of passive, it may help to think thusly: A hook, (f g) , is dyadic f with its right argument modified by g . The pattern (f~ g)~ is dyadic f with its left argument modified by g . These constructs can be written (f g) as the fork ([ f g@:]) , and (f~ g)~ as the fork (g@:[ f ]) which are longer, but perhaps straightforward.

~ (Evoke) Adverb

Back to: Vocabulary

If you happen to have a background in which dereference makes sense to you, it may help to understand evoke by thinking of it as dereferencing in name space. Furthermore, evoke can appear on either side of copula. Therefor, evoke can use or modify a variable named at run time. In a j sentence, word~ is reflexive if word is a verb in monadic context; is passive if word is a verb in dyadic context; evoke if word names a pronoun or proverb. (I haven't tried all possibilites.) Otherwise, there is an error condition.

   a=: i.3
   'a'~
0 1 2

Common uses

Evoke demonstration implementing queue and stack. A non-zero optional x argument invokes display to show the transitions.

display=: smoutput@:([,' ',":)  NB. a custom verb to display items.  Dyad, x names the global variable.  y are itemized items.

Display=: adverb define
:
m display^:(0 -.@:-: x)y
)

NB. Queue, Stack, Pop: m literal name of vector to use.  verbose unless x is 0.
Queue=: adverb define                   NB. enqueue y
0 m Queue y
:
x (m,' queue')Display y
('m'~)=: y ,~ (m~)
EMPTY
)

Stack=: adverb define                   NB. Stack y
0 m Stack y
:
x (m,' stack')Display y
('m'~)=: (|.y) , (m~)
EMPTY
)

Pop=: adverb define                     NB. Pop y items
0 m Pop y
:
rv=. y {. (m~)
('m'~)=: y }. (m~)
x (m,' pop') Display rv
rv
)


NB. test code
'Q S'=: ;: 'QUEUE STACK'  NB. unboxing occurs at assignment
('Q'~)=:('S'~)=: ''       NB. initialize empty vectors to store data
1 Q Queue'abc'
'QUEUE'Queue'de'
assert 'ab'-:Q Pop 2
assert'cde'-:Q Pop 3
S Stack'abc'
S Stack'de'
assert'edc'-:1 S Pop 3
assert 'ba'-:'STACK'Pop 2

The items must share a common data type. Can you modify the code to handle arbitrary data types?

See Also


CategoryVocTemplate CategoryVoc CategoryWorkInProgress

Vocabulary/tilde (last edited 2012-02-26 14:48:57 by IanClark)