CamelCase in J

Example for versions j602

alpha is the set of upper and lower case alphabetic characters.

camel is an explicit solution to the problem, meaning that the variables to the function(y) are mentioned in the function body. The strategy used is to calculate the indices(i) of the alphabetic characters from the bit vector(b) which represents whether or not a character is alphabetic. The function then takes the leading index of each interval of these alphabetic characters and uses that to replace them by their capitalized form. Finally the string is sieved using b so that it contains only the alphabetic characters.

camel2 is a tacit definition, it cuts the input string at the delimiters(non-alphabetic characters), leaving a collection of boxed strings. The leading character of each of these boxed strings is then capitalized and they are all concatenated together forming the final result.

camel3 takes advantage of the built-in libraries, a regular expression is used to split the string into boxed strings, each of these is then capitalized and concatenated.

In all three functions the input is first converted to lower case.

check can be used to ensure that all the functions produce equivalent output.

alpha=: a.{~,65 97+/i.26


camel=: 3 : 0
y=. tolower y
i=. I. (> 0,}:) b=. y e. alpha
(b # toupper@(i&{) i} ]) y
)


isalpha=: e.&alpha
capital=: (toupper@{.,}.)^:(1<:#)
format =: [: ,&' ' tolower
case   =:  [: ; [: capital&.> ] <;._2~ [: -. isalpha
camel2 =: case@format


require'regex text'
camel3=: [: ; [: capitalize&.> [: '[a-zA-Z]+'&rxall tolower


check=: [: ,. camel;camel2;camel3