Jx Extensions Guide
Primitives
·
=.. Is (Local) _ _
=:: Is (Global) _ _
The copula verbs =.. and =:: are similar to their corresponding copulas but are syntactically dyadic verbs and may be embedded in tacit expressions.
test=. verb define
=::~ 'XX'
echo XX
=..~ 'XXX'
echo XXX
)
test ''
XX
XXX
assert 'XX' -: ". 'XX'
assert '' -: ". 'XXX'
· $. Sparse _ _ _
Sparse arrays can be boxed and retrieved from a boxed array.
]A=. 'sparse';7;$.3=7&|i.30
┌──────┬─┬──────┐
│sparse│7│ 3 │ 1│
│ │ │10 │ 1│
│ │ │17 │ 1│
│ │ │24 │ 1│
└──────┴─┴──────┘
2{::A
3 │ 1
10 │ 1
17 │ 1
24 │ 1
· $:: Self reference without implicit context _ _ _
Similar to $: but without implicit
context apart from apart from O.0 or O.1
NB. Tail recursive
(continuation-style) form of the Ackermann function...
'adv
conj ver'=. _1 _2 _3 <@?: 0
o=. @:
c=. "_
e=.&.>
train=.
".. NB. `:6 as a (monadic) verb
an=. "..inv NB. Atoming
nouns (or words)
cv=. (an f.adv) (c
(train f.@:)) NB. Constant verb (word)
at=. ?: <'@:' NB. Verbing
@:
Train=. ]: ((train cv at ]) o (train o
(train o ([ ; ;cv ; ])/)) f.)adv
'amper knot'=. ?:e
;:'& `.' NB. Verbing
&and
'v M N'=. (] < o amper"0 {::cv) 0 1
2
'vv MM NN'=. 0 1 2
case=. #. o * o (M , N)
c0=. [:
NB. It is never active
c1=. v train o knot (N + 1:)
c2=. $::@:((<:@:M ; 1:) (MM,NN)} ])
next=. < o (($:: cv) at [: (an o v) ;cv
((an o <: o M) ) ;cv ]cv Train) NB.
Next verb
c3=. $::@:((next ; N - 1:) (vv,NN)} ])
ack=. (c0`c1`c2`c3
@. case)O.1
Ack=. ack o (((] <adv)c ; ;)/) M.f.
3 Ack 2
29
· ~ Evoke
X~ for literal X is the result of executing X as a sentence. u~ first applies u to its argument(s) and then executes the resulting sentence.
'u v'=. [: +/ *: ]:
'uv=. u o v'~
uv
u@:v
· [. Trigger _ ]. Trap mu lu ru
Trap is an adverb such that u ]. is the same as u , except that if the monadic verb trigger [. is executed then u returns immediately with the result being the argument of [. .
([.@:(+:&.>)(^:_)]. 1 ; 2 ; 3)
┌─┬─┬─┐
│2│4│6│
└─┴─┴─┘
· ]: Strand to Box _ _ _
The Curried adverb ]: collects its arguments until a closing [: is encountered, returning them as a boxed list. If the train of an argument is valid then the product of the train is collected instead of the argument. If the train (`:6) of an argument produces an entity then this entity becomes the boxed item.
[:]: -: 0$a:
1
[: +/ *: (<'"') 0 (i.3 4) ]:
┌──┬──┬─┬─┬─────────┐
│+/│*:│"│0│0
1 2
3│
│
│ │ │ │4
5 6
7│
│
│ │ │ │8
9 10 11│
└──┴──┴─┴─┴─────────┘
[: [:`'' ]:
┌──┐
│[:│
└──┘
·
" Rank with cyclical gerunds (m"n u"n m"v u"v)
It is similar to the official rank " but accepts a gerund left
argument whose verbs are executed on the computed cells of the input cyclically
except when the right argument is _ .
([:+ - * %]: " 0 )0 1 2 3 4 j. 1
0j_1 _1j_1 0.894427191j0.447213595 0.3j_0.1
4j_1
1j1([:+ - * %]: " 0 )0 1 2 3 4 j. 1
1j2 0 1j3 0.4j0.2 5j2
([:+ - * %]: " _ ) _
┌─┬─┬─┬─┐
│+│-│*│%│
└─┴─┴─┴─┘
_ ([:+ - * %]: " _ ) _
┌─┬─┬─┬─┐
│+│-│*│%│
└─┴─┴─┴─┘
+:`|:"::2 i.
2 2 3
0 2 4
6 8 10
0 0 0
6 9 0
7 10 0
8 11 0
· ".. Train / Do · Apply _ 1 _
The sentence ".. Y for gerund (or equivalent) Y is equivalent to Y`:6 ; for literal Y is similar to ". Y except that verbs, adverbs and conjunctions are valid results. X ".. Y is X 128!:2 Y (apply).
".. (+`(;:'/'))
+/
+".. '/'
+/
'+/@:*:' ".. 1 2 3
14
·
`.
Knot (Gerund)
The conjunction knot is similar
to the J primitive ` (tie) except that a non-boxed noun argument is first
replaced by its atomic representation.
".. (+/@:*:)`.1 2 3
14
·
&:.
Cumulative Reduce _ _
In x u &:. v y , the left argument
x is an array whose items are "transient inputs" and the right
argument y is an initial state. The verb u ("state transition")
computes a new state from a transient input and the current state, and the verb
v ("historical map") computes a value from a state. For i=0,1,2,...,n-1 where n=. #x
s0
=. y
si+1=. (i{x) u si
ri =. v si
The result is the pair of the final
state sn and the ri values;
that is, x u &:. v y is sn;r if ri are not boxed and sn;<r if
boxed, where r is the result of assembling the ri
according to the rules in section IIB of the J Dictionary. In other words, x u
&:. v y is
sn ;
> r0;r1;...; rn
if ri are
not boxed
sn ; < >
r0;r1;...;<rn
if ri are
boxed
For example, in the Fibonacci
sequence, the state are consecutive elements of the sequence fi,fi+1 . The verb
u=. ({:,+/)@] computes a new state (fi+1,fi+2) from the current state, and the
verb v=. {. computes a value of interest (fi) from the current state. Thus:
(10$0) ({:,+/)@] &:. {. 0 1
┌─────┬─────────────────────────┐
│55 89│0 1 1 2 3 5 8 13 21 34 55│
└─────┴─────────────────────────┘
In the result, 55 89 is the final
state and 0 1 1 2 3 5 8 13 21 34 55 are the values computed from successive
states. In this example, the left argument 10$0 serves only to indicate how
many terms to compute, and does not otherwise participate in the computation
(due to the @] in the left verb argument to &:.). The computation can be
resumed by using the final state as the new initial state:
(7$0) ({:,+/)@] &:. {. 55 89
┌─────────┬──────────────────────────────┐
│1597 2584│55 89 144 233 377 610
987 1597│
└─────────┴──────────────────────────────┘
That is, the space used by 256!:0 is a
small amount beyond the space needed for its result, with fraction of the small
amount decreasing with the number of iterations (depending on the verbs u and
v). For example,
u1=. [: |."1 [: |. -
v1=. [: +/ ,
B=. i.25 35
spa=. 3 : '7!:5 <''y'''
xxx=. 'X u1 &:. v1 B'
(spa ". xxx) % 7!:2 xxx [ X=. i.5000
25
0.793672627
(spa ". xxx) % 7!:2 xxx [ X=.
i.10000 25
0.878934625
(spa ". xxx) % 7!:2 xxx [ X=.
i.20000 25
0.933716306
(spa ". xxx) % 7!:2 xxx [ X=.
i.40000 25
0.965205289
As a practical matter, &:.
computations should be arranged so that v si would
give results that are uniform in type and shape; non-uniform results incur a
penalty in time and space. For example:
((i.1000) + &:. ] -~2) -: (i.1000) +
&:. ] 0
1
ts=. 6!:2 ,
7!:2@]
ts '(i.1000) +
&:. ] -~2'
8.21251947e_5 18560
ts '(i.1000) +
&:. ] 0'
0.000103023124 157312
In the first example, with initial
state -~2 (an integer), v si are all scalar integers.
In the second example, with initial state 0 (a boolean),
v 0 is boolean and all the other v si are integers. The non-uniformity forced the
implementation to use a less efficient technique.
· ?: Cloak (as verb) •
Cloak 0 0 0
The sentence X ?: y produces the entity whose atomic representation is Y as a verb (3), adverb (1) or conjunction (2) depending on the return code X. For positive X . ?: Y is equivalent to 3 ?: Y and it is also equivalent to (<(<,'4'),<(<(<,'4'),<;:'0:`'),<(<,'4'),<;:',^:') (0:`)(,^:) . For negative X ?: generates the following entities:
_3 ?: 0 is a verb that converts its argument into a verb.
_2 ?: 0 is an adverb that converts its argument into a conjunction.
_1 ?: 0 is an adverb that converts its argument into an adverb.
((;:'ver conj adv')=. _3 _2 _1<@?: 0)
┌───────┬───────┬───────┐
│(_3?:0)│(_2?:0)│(_1?:0)│
└───────┴───────┴───────┘
· i.. Complex Integers 0
The sentence i..
X j. Y is |. |: (i. X) j." 0 1"0 i. Y
i.. 2j3
0j2 1j2
0j1 1j1
0
1
i.. _2j3
1j2 0j2
1j1 0j1
1
0
i.. 2j_3
0
1
0j1 1j1
0j2 1j2
i.. _2j_2
1
0
1j1 0j1
(i.. -:
|.@:|:@:(([ + j.@:])"0/&:i.&>/)@:+.)3j4
1
· O. 0/1 Optimize mu lu ru / _ _ _
The verb v O.0 (with direct scope) is similar to v except any $: or $:: appearing in v applies to v and not the verb containing v if any. The verb v O.1 is similar to v , if v is tail-recursive, except that recursive calls are optimized in the sense that they do not grow the function call stack. If v is not tail recursive then v O. 1 will cause the interpreter to crash.
NB. Recursive Scope (1 +
!)5 ...
(1 + 1:`( * $:@:<:)@. * O.0) 5
121
NB.
Recursive Scope TCO 3
(1 + !) 5 ...
3 (1 + 1:`(%~ *
$:&: <:)@.(*@:[)O.0) 5
11
Foreign
Conjunction
· 104!:5
Unnamed Execution
104!:5 v
calls verb v on a temporary name for X.
assert ((i.2 3)
((104!:5) ((5!:1)@<))) -: ("..^:_1 i.2 3)
(i.2 3) (rc ((5!:1)@<)) NB. Atomic representation
┌─────────┐
│┌─┬─────┐│
││0│0 1 2││
││ │3 4 5││
│└─┴─────┘│
└─────────┘
(rc
type)rc
┌───────────┐
│conjunction│
└───────────┘
type rc type
┌────┐
│verb│
└────┘
· 102!:0/1
In-place Amend/Append
Jx cannot be certain that the original value of the array being modified will not be needed later. It is up to the user to know by other means that the original value really is no longer needed.
Facility J Primitive Model
Amend In-Place 102!:0 AMIP=. 4 :
'(< (i{::y) (j{y)} k{::y) k}y [ ''i j k''=. x'
Append In-Place 102!:1 APIP=. 4 : '(< (i{::y),j{::y
) i}y [ ''i j ''=. x'
] V=. (- 11 22 33 ,:
44 55 66) ; (1 2;3 4 5) ; i.5 7
┌───────────┬───────────┬────────────────────┐
│_11 _22 _33│┌───┬─────┐│
0 1 2
3 4 5 6│
│_44 _55 _66││1 2│3
4 5││ 7 8 9 10 11 12 13│
│ │└───┴─────┘│14
15 16 17 18 19 20│
│ │ │21 22 23 24 25 26 27│
│ │ │28 29 30 31 32 33 34│
└───────────┴───────────┴────────────────────┘
0 1 2 (102!:0) V
┌───────────┬───────────┬───────────────────────┐
│_11 _22 _33│┌───┬─────┐│
0 1 2
3 4 5 6│
│_44 _55 _66││1 2│3
4 5││ 7 8 9 _11 _22 _33 13│
│ │└───┴─────┘│14
15 16 _44 _55 _66 20│
│ │ │21 22 23 24
25 26 27│
│ │ │28 29 30 31
32 33 34│
└───────────┴───────────┴───────────────────────┘
] W=. (i.3 4) ;
-11 22 33 44
┌─────────┬───────────────┐
│0 1 2
3│_11 _22 _33 _44│
│4 5 6
7│ │
│8 9 10 11│ │
└─────────┴───────────────┘
0 1 (102!:1) W
┌───────────────┬───────────────┐
│ 0
1 2 3│_11 _22 _33 _44│
│ 4
5 6 7│ │
│ 8
9 10 11│ │
│_11 _22 _33 _44│ │
└───────────────┴───────────────┘
Syntax
·
Adverb – Verb
a v ↔ v(a)
The verb is executed with the adverb as its argument, e.g.,
/< NB. Boxing /
┌─┐
│/│
└─┘
·
Adverb – Adverb
x (a a) ↔ (x a) a
The product of (x a) is not required
to be a noun or verb and ((x a) a) produces a train if (x a) produces an adverb
or conjunction.
+ (/\) NB. (x
a) producing a verb (the official case)
+/\
(/<) (>(adv=. _1?:0) \) NB. (x a) producing
an adverb
/\
(<adv@:) (>adv \) NB. (x a)
producing a conjunction
@: \
u (<adv@:) (>adv \) v
u@:v\
·
Adverb Conjunction
a c ↔
(c)a
The adverb is executed with the conjunction as its argument.
]:&
(<&)]:
<adv@: NB. Boxing @:
┌──┐
│@:│
└──┘
·
Conjunction Adverb
x (c a) y ↔ x c y a
The product of (x c y) is not required
to be a noun or verb and produces a train.
*: (@ /) -1 NB. (x c y) producing a verb
*:@-/
; ("/) 1
;"1/
(; ("/) 1) i.3 2
┌───┬───┬───┐
│0 1│2 3│4 5│
└───┴───┴───┘
(<adv@:) ("..@:;(conj=. _2?:0)/)
< NB. (x c
y) producing an adverb
(@:<)/
u (<adv@:) ("..@:;conj/)
<
u@:</
(/<) "..@:([
, (<'@:') , ])conj (\<) NB. (x
c y) producing a conjunction
/ @: \
u(/<)
"..@:([ , (<'@:') , ])conj
(\<)v
u/@:(v\)
·
Adverb
Conjunction Adverb
x (a c a) y ↔ (x a) c
(y a)
The products of (x a) and (y a) are not required to be a noun or verb and produces a train.
u /@:\ v NB. (x a) and (y a) producing verbs
u/@:(v\)
(/<) ((>adv)
@: (>adv)) (/<) NB. (x a) and
(y a) producing adverbs
/ @: /
(<adv<)
((>adv) @: (>adv))
(<adv")
NB. (x a) producing an adverb and (y a) a
conjunction
<@:"
_ ((<adv<)
((>adv)@:(>adv)) (<adv")) NB. boxed @: as a constant verb
(<@:)"_
_ ((<adv<)
((>adv)@:(>adv)) (<adv")) ''
┌──┐
│@:│
└──┘
Unicode Names
Unicode characters are legal inside names.
'λ λ0
λ1' =. 1 2 3
λ , λ0
, λ1
1 2 3
Δ=. +/\inv
Δ*: 1 2 3 4 5 6 7
1 3 5 7 9 11 13