Jump to content

Search a list: Difference between revisions

→‎{{header|J}}: correction
m (Added to text proc cat)
(→‎{{header|J}}: correction)
Line 87:
J has a general and optimized lookup function, <tt>i.</tt>. For example:
 
H =: ;:'Zig Zag Ronald. Bush Krusty Wally Charlie Bush Bozo' NB. Haystack
N =: ;:'Washington Bush' NB. Needles
NB. Haystack
N =: ;:'Washington Bush'
NB. Needles
H i. N
_19 3
 
Note that the arguments to <tt>i.</tt> can be '''anything''' (ie either or both may be scalars, lists, multidimensional arrays, etc).
 
To produceformat output similar to the other examples, one might write:
 
H ;:^:_1@(] (>@{.:@[ ]|."_1 (,. >@{:.)) i. ([{;{(~:_1+#) )1|.' is not in haystack' ;~ ":&.>@i.@#@[)~ N
Washington is not in haystack
3 Bush
 
To elaborate a bit: Array-oriented languages (like J) consume the input and produce the output ''in toto''.
Line 107:
And while it is both possible and simple to treat the input item by item, this is significantly slower and loses the great advantage of array processing.
 
Therefore these languages generally produce a special, but conforming, output for "bad" inputs (in this case, -1an index past the end of the list). Then the functions which consume these outputs may be left untouched (as the special outputs are already in their domain) or may be extended simply.
In this case, there is only one function which formats and prints the results, and its treatment of "good" and "bad" outputs is identical (it cannot distinguish the two). It is simply that the outputs of previous functions have been arranged such that the results are conformable.
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.