chars ← ##.ucase chars ⍝ Upper-casification.
Returns the upper-case equivalent of its simple character array argument. Note
that this function works with arguments of any rank. Note also, that the funct-
ion may easily be extended to work with "Dyalog ALT" or a font of our own.
Technical note:
The code for ucase looks like this:
ucase←{ ⍝ Upper-casification,
lc←'abcdefghijklmnopqrstuvwxyzåäöàæéñøü' ⍝ (lower case alphabet)
uc←'ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖÀÆÉÑØÜ' ⍝ (upper case alphabet)
(uc,⎕av)[(lc,⎕av)⍳⍵] ⍝ ... of simple array.
}
John Daintree points out that, as ⎕AV⍳ is an "idiom" in Dyalog Version 10.0 and
above, ⎕av⍳⍵ is considerably faster than (lc,⎕av)⍳⍵. To exploit this we inject,
rather than prefix, mapping characters into a copy of ⎕av. A further advantage
is that we can do this ahead of time and avoid the runtime construction of the
"from" and "to" vectors, by binding (deriving) a ucase function:
lc←'abcdefghijklmnopqrstuvwxyzåäöàæéñøü' ⍝ (lower case alphabet)
uc←'ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖÀÆÉÑØÜ' ⍝ (upper case alphabet)
av←⎕av ⍝ temp copy of ⎕av.
av[av⍳lc]←uc ⍝ subs upper for lower case.
ucase ← av∘{⍺[⎕av⍳⍵]} ⍝ derive new function.
⎕ex'av' ⍝ remove temp variable.
ucase 'Toto'
TOTO
Note that in the experimental "Function Results Edition" of Dyalog, we can en-
capsulate the construction of the function in a function- or "closure-" return-
ing function:
mapsto∆ ← { ⍝ returns function, which maps ⍺ chars to ⍵ chars.
av←⎕av ⍝ all chars.
av[av⍳⍺]←⍵ ⍝ subs ⍺ chars with ⍵ chars.
{av[⎕av⍳⍵]} ⍝ return mapping function.
}
ucase ← lc mapsto∆ uc ⍝ upper-casification closure.
lcase ← uc mapsto∆ lc ⍝ lower-casification closure.
⎕←lcase ⎕←ucase 'Toto' ⍝ test upper- and lower-case cases.
TOTO
toto
For more on closures, see: http://dfns.dyalog.com/downloads/fre.pdf
Finally, we could exploit "retained hash tables" and "squad indexing" to produce
this reasonably quick derived function. Notice that the two concatenations with
⎕av are evaluated at definition, rather than at function application time. This
is a good thing.
ucase ← ⌷∘(uc,⎕av)∘⊂∘((lc,⎕av)∘⍳) ⍝ (see →derive←)
An alternative coding, which uses the same techniques, might be:
ucase ← (lc,⎕av)∘⍳{⍵⍵[⍺⍺ ⍵]}(uc,⎕av) ⍝ upper-casification.
Again, in the above expression, hash table construction and the concatenations
are executed only once, irrespective of the number of times that the function is
subsequently applied.
Example:
ucase 2 8⍴'please, speak up'
PLEASE,
SPEAK UP
See also: lcase derive
Back to: contents
Back to: Workspaces
Trouble seeing APL font?