Ok. So here's my problem:
I want to create a new ambivalent verb which combines behaviors from two
existing verbs.
Requirements:
new verb will be called mynl
MONADIC CASE
mynl will permit monadic operation which accepts either
(1) a list of 1 or more items from the set {0, 1, 2, 3, 6}; or,
(2) the empty list (in which case it will default to the list 0 1 2 3)
According to its documentation: NB. Form: mynl y
For the monadic case, mynl will return the same results as is returned
from namelist_z_
DYADIC CASE
mynl will permit dyadic operation which accepts a right argument as
defined for the monadic case.
What's more, mynl will accept left arguments which will filter its
results, similarly to nl_z_.
According to its documentation: NB. Form: [x] mynl y
The right argument will be as defined for the monadic case. The left
argument will be used for filtering; and, if omitted, the left argument will
default to the empty list which will indicate that no filtering will be
performed on the results.
Since namelist_z_ is a simple wrapper around the foreign conjunction
(4!:1), we could simply state the problem definition for mynl thusly:
mynl will return the results as defined by 4!:1 while permitting
filtering as per nl_z_.
The most obvious change would be to modify the line from the nl_z_
definition that reads
nms=. (4!:1 y) -. ;: 'x y x. y.'
(This is code which prevents nl_z_ from being able to report x_mylocale_
and y_mylocale_ which have been defined in the current locale, mylocale.)
So, I have modified the line; I removed x and y from the list, while leaving
x. and y. in the list.
<------- CLIPBOARD ------------------------------------------->
[01] mynl =: 3 : 0
[02] '' mynl y
[03] :
[04] if. 0 e. #y do. y=. 0 1 2 3 end.
[05]
[06]
[07] NB. ANALYSIS
[08] NB. 1 4 8 e.~ 3!:0 y
[09] NB. => 1 4 8 e.~ 4 (because y= 0 1 2 3 AND 3!:0 y = 4)
[10] NB. => 4 e. 1 4 8
[11] NB. => 1
[12] if. 1 4 8 e.~ 3!:0 y do.
[13] NB. ANALYSIS
[14] NB. (4!:1 y) -. ;: 'x. y.'
[15] NB. => B|x|y| -. ;: 'x. y.'
[16] NB. => B|x|y| -. B|x.|y.|
[17] NB. => B|x|y|
[18] nms=. (4!:1 y) -. ;: 'x. y.'
[19] else.
[20] nms=. cutopen_z_ y
[21] end.
[22]
[23] if. 0 e. #nms do. return. end.
[24]
[25] if. #t=. x -. ' ' do.
[26] 'n s'=. '~*' e. t
[27] t=. t -. '~*'
[28] b=. t&E. &> nms
[29] if. s do. b=. +./"1 b
[30] else. b=. {."1 b end.
[31] nms=. nms #~ n ~: b
[32] end.
[33] )
<------- CLIPBOARD ------------------------------------------->
TESTING
-------
For testing, I use 3 cases:
To setup for testing, if I define 'x' and 'y' in 'mylocale' (which is the
specific issue that I am trying to address).
> cocurrent 'mylocale'
> ('mylib';'z') copath 'mylocale' NB. mylib is the locale into which I have
installed mynl
> x =: 5
> y =: 10
TEST 1: 'x' mynl '' NB. Result: Good; mynl reports x
'x' mynl 0 1 2 3 NB. Result: Good; mynl reports x
'y' mynl '' NB. Result: Good; mynl reports y
'y' mynl 0 1 2 3 NB. Result: Good; mynl reports y
TEST 2: '~x' mynl '' NB. Result: Good; mynl reports y
'~x' mynl 0 1 2 3 NB. Result: Good; mynl reports y
'~y' mynl '' NB. Result: Good; mynl reports x
'~y' mynl 0 1 2 3 NB. Result: Good; mynl reports x
TEST 3: mynl '' NB. Result: Good; mynl reports x and y
TEST 4: '' mynl '' NB. Result: Good; mynl reports x and y
'' mynl 0 1 2 3 NB. Result: Good; mynl reports x and y
TEST 5: erase 'x'
erase 'y'
mynl '' NB. Result: Fails; reports x and y (which
are local to mynl)
QUESTIONS:
(1) How can I cause mynl to only report (as does the 4!:1 foreign
conjunction) when
x or y is in the current locale? Is there a way for me to know which locale
was current when
mynl was invoked? Then I could test, indirectly, for x__current-locale and
y__current-locale. How do I determine the value for current-locale to do
this?
(2) Regarding line [20], isn't this unreachable? I have already verified
that y contains at least one item in line [04], no?
(3) Why am I enjoying my excursion with J so much? LOL
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm