Phrases/Selection

From J Wiki
Jump to navigation Jump to search

Creating an Empty List

NB. y is anything; result is a list of 0 items, each like y
noneof =: ($0)&(]"0 _)

Culling

Given an array and a predicate, keep only those items for which the predicate is true

NB. Adverb.  If [x] u y is 1, keep that element of y
usedtocull =: 1 : 'u # ]'

Substring

NB. x is (starting position,length), y is list
NB. result is the selected portion
substr =: (];.0~ ,.)~"1 _

Open a Column

ocol =: >@:({"1)

Select Leading or Trailing Elements, but Not Running Off the End

NB. like {., but limited to the length of y
leading =: ((<. #) {. ])"0 _
NB. like - ux_vy {., but limited to the length of y
trailing =: (-@(<. #) {. ])"0 _

Insert Inside Multilevel Boxed Array

m gives the path, like that used by {::. x is put inside the box at that position in the path.

NB. Adverb.  Install x in the path given by m
store =: 1 : 0
:
if. #m do. (< x (}.m)store ({.m){::y) ({.m)} y else. x end.
)

Apply Verbs on Selected Items

The onitem conjunctions apply u to the cells of y selected my n . There are bivalent, monadic, and dyadic forms.

NB. Conjunction.  u is verb, n (or [x] v y) is arg to { to select s = desired portion of y
NB. The result of x u s (if dyad) or u s (if monad) replaces s
onitem =: 2 : '(u bivalent (n&{)) n} ]'
onitemm =: 2 : 'n}~ u@:(n&{)'
onitemd =: 2 : '(u (n&{)) n} ]'

applyintree applies u at the cell whose path is n .

NB. Conjunction.  Apply u at the cell indicated by n
applyintree =: 2 : 0
if. #n do. ((u applyintree (}.n)) L:_1 ({.n){y) ({.n)} y else. u y end.
:
NB. The rank is s,0 where s is the surplus of x-rank over y-rank.  This causes
NB. the cells of y to be matched up with the largest appropriate blocks x  This
NB. is necessary because it is impossible to change the shape of the values being modified
if. #n do. (x u applyintree (}.n) L:_ _1"(0 (,~ >.) x -&(#@$) a) (a =. ({.n){y)) ({.n)} y else. x u y end.
)