Jack Andrews asked:
> are there better ways?
There's the old assembler standby:
cup =: 22 b.&.(a.&i.)~ (' ',~{.a.) {~ e.&'abcdefghijklmnopqrstuvwxyz'
cdn =: 22 b.&.(a.&i.)~ (' ',~{.a.) {~ e.&'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
(if you like it better, you can replace ' ',~{.a. with 0 32{a. )
These are kind of neat because their definitions each include only one case set
(in most J solutions, both case sets must be mentioned).
These are probably not as efficient as the standard solution. Even though m
b.&.(a.&.i) is fast now (see PS), the e. and { each generate a vector as
long as the input, so you have to malloc and fill more than double the input
size. I don't see a good way to avoid this.
> how could i define a word 'cmap' so that
> lower2=.cmap A2Z a2z
> upper2=.cmap a2z A2Z
>
You can't quite do it this way. The Jish way to do this would be to have cmap
be a conjunction, and your mapping sets as its arguments: Like so:
cmap =. 2 : '((n{a.) m }a.) {~ a.i.]'
lower=.A2Z cmap a2z
upper=.a2z cmap A2Z
For a little fun, you could also write an adverb which derives an adverb, to
more closely match the syntax you wrote above (i.e. that the nouns a2z and A2z
abut, but in this case, they'll appear to the left of the word cmap ).
Cmap =. 1 : ' 1 : (''(('',(":m),''{a.) m}a.) {~ a.i.] '' )'
lower2 =. A2Z a2z Cmap
upper2 =. a2z A2Z Cmap
(if you don't mind a couple of parens, you could also use the original cmap ,
as in A2Z (a2z cmap) ). A tacit version is left as an exercise to the
reader (hint: start with the half-tacit, i.e. Cmap is explicit, but it derives
a tacit adverb).
-Dan
PS: Roger made m b.&.(a&i.) fast. See his Essay at
http://www.jsoftware.com/jwiki/Essays/Bitwise_Functions_on_Characters
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm