Phrases/Locales

From J Wiki
Jump to navigation Jump to search

Execute a Verb in a List of Locales

NB. Conjunction.  u is applied in each locale v
inlocales =: 2 : 0
i =. 18!:5 ''
for_l. n do.
  cocurrent l
  u y
end.
cocurrent i
''
:
i =. 18!:5 ''
for_l. n do.
  cocurrent l
  x u y
end.
cocurrent i
''
)

NB. same, but return concatenated results
inlocalesr =: 2 : 0
i =. 18!:5 ''
r =. 0$a:
for_l. n do.
  cocurrent l
  r =. r,u y
end.
cocurrent i
r
:
i =. 18!:5 ''
r =. 0$a:
for_l. n do.
  cocurrent l
  r =. r,x u y
end.
cocurrent i
r
)

See if Locale Exists

NB. y is boxed locale name
NB. Result is 1 if locale exists
localeexists =: e.   (4!:1)@:6:

Create Variable Name in the Current Locale

Useful if you have to pass a callback to the current locale.

NB. y is a character string; we add on the current locale name (if there is no locale already)
NB. Result is string.  Ex: inthislocale 'abc'  produces 'abc_base_'
inthislocale =: (, '_'&([,],[)@>@(18!:5)@(0&$))^:('_'&~:@{:)

Automating Multi-Locale Initialization

Include these lines early in your project. For each locale with run-time initialization, create a verb initialize to perform the initialization. Then, include a line registerinit 0 outside of any explicit definition. That will add the locale of the file to the initialization list. When the project is executed, issue initialize_z_ 0 which will run all the initialize routines.

NB. Initialization automation.
NB. Initialization routines are always named 'initialize' in
NB. the locale in which they reside.  They are registered
NB. here, along with a level at which they should be applied.
NB. (the level is system-dependent but generally 0=one-time
NB. initialization).  A call to initialize_z_ y runs all
NB. the initializations at y or above.
initialize_list_z_ =: 0 2$a:
registerinit_z_ =: 3 : 0
'initialize' registerinit y
:
initialize_list_z_ =: initialize_list_z_ , y ; x , '_' , (>18!:5'') , '_'
0 2$''
)
initialize_z_ =: 3 : 0
128!:2&y@>^:(*@#@])(y&<:@:(>@:(0&{"1)) # 1&{"1) initialize_list
0 2$''
)

Creating Global Names for Verbs

Example: publishentrypoints 'method1 method2'

NB. y is character string list of entry-point names
NB. x is the level number at which we should publish the entry points (default _1, 'z')
NB. we publish these names in the locale at position x in the path
publishentrypoints =: 3 : 0
_1 publishentrypoints y
:
NB. The rhs of the assigment below interprets the names as gerunds
path =. '_' (,,[) x {:: (<,'z') ,~^:(-.@*@#@]) 18!:2 current =. 18!:5 ''
l =. ,&path^:('_'&~:@{:)&.> ;: y
r =. ,&('_' (,,[) > current)@(({.~ i:&'_')@}:^:('_'&=@{:))&.> ;: y
NB. The gerund assignment requires more than one name, so duplicate the last:
('`' , ;:^:_1 (, {:) l) =: (, {:) r
)