Phrases/Names

From J Wiki
Jump to navigation Jump to search

Is a Name Defined?

NB. y is a variable name
NB. result is 1 if name is defined
ifdefined =: 13 : '0 <: 4!:0 < y'

Which Script Defined This Name?

NB. Indicate which script defined name y (may be boxed or unboxed)
whichscript =: 13 : '((''Not from script''"_)`({ 4!:3@(0&$)))@.(0&<:) (4!:4) < > y'

Which Locales Contain This Name?

NB.*whichlocales v returns list of locales where given name is defined
NB. To work properly whichlocales must be in the copath
NB. Optional left argument limits search to given name classes
whichlocales=:((i.4)&$:) : (4 : 0)
  r=.0$a:
  ls=.(coname ''),(copath coname '')
  ls=.ls,(nl 6)-.ls
  test=.'(4!:1 ]',(":x),') (#@[ > i.) <''',(>y),''''
  for_l. ls do.
    if. do__l test  do.
      r=.r,l
    end.
  end.
  r
)

List Names Starting With a Prefix

NB. y is a string; result is vector of boxed names beginning with y
NB. If x is present, only those name classes are searched (default 0 1 2 3)
NB. a: ,  required so S: doesn't think type is unboxed
listnameswithprefix =: (0 1 2 3&$:) : (13 : '(a: , ({.y) 4!:1 x) ( (] -: ({.~ #))S:0 _ # [ ) y ')

Copy List of Names from Current Locale into Specified Locale

NB.*exportnames v exports listed names from current locale into locale
NB. specified by x by creating stubs
exportnames_z_=: ('z'&$:) : (4 : 0 cutopen)
  c=.>coname''
  for_nm. y do.
    z=. (>nm),'_',x,'_=:',(>nm),'_',c,'_'
    echo z
    do z
  end.
  empty ''
)