Here is an updated version of my code.

[1] It includes your bugfixes, so it actually works for 1p1 and 1x1.
[2] It includes support for signed arbitrary base numbers.

That said, I noticed another area where the native implementation
is more functional than my code:  8b1.1

And, fixing this will not be completely trivial, since my current approach
would have the part that implements the fraction be unaware of
the base being used.  And I am almost tempted to rewrite the handling
of . and r (which I currently have at the same level of hierarchy), however
the handling of assertions about legal digit characters winds up being
enough of an issue that I do not think I would really gain anything if
I refactored the code in that fashion.

NB. core numeric implementation
alfnum=: '0123456789abcdefghijklmnopqrstuvwxyz'
enquote=: '''',,&''''
refdig=: alfnum enquote@:{.~ >./@:>:@i.~&alfnum
digits=: refdig,'i.',enquote
isb10=: ass...@e.&'0123456789'
isb36=: ass...@e.&alfnum
decimal=:  (isb10 ] '10#.',digits )@;@{.
extend=:   (isb10 ] '10x#.',digits)@;@{.
arbitrary=:(isb36 ] digits        )@;@{:  NB. note:
{: instead of {.

NB. parsing
split=:1 :0
 (0;(0 10#:<.10*".;._2]0 :0);< <;._1 m)&;:@;
)

NO=: assert bind 0

sel=:2 :0
 'ndx opts'=.y
 x`''@.((;:opts) i. ndx&{)
)

NB. fractions
tkn1=: ' .r' split
 0.6 1.1
 2.2 1
 3.2 3.2
 3   3
)
numb1=: no`decimal`decimal`num...@.#@tkn1
numb1e=: numb1d`numb1r sel (1;'.r')
numb1d=: '(', decimal,')+(%10)#.|.', (isb10 ] digits)@,~&'0'@>@{:
numb1r=: '(', extend, ')%',ext...@{:

NB. negative sign
tkn2=: ' _' split
 1.1 2.1
 2.2 2.2
 2   2
)
numb2=: no`inf`num...@.#@tkn2
numb2e=: '_.'"_`('-_'"_)`('-',nu...@}.) sel (1;'._')
inf=: numb1`('_'"_)@.('_'={.@;)

NB. exponential (scientific notation)
tkn3=: ' e'split
 0.6 1.1
 2.2 1
 3.2 3.2
 3   3
)
numb3=: no`numb2`no`num...@.#@tkn3
numb3e=: ('(',nu...@{.,')*10^',nu...@{:) sel (1;'e')

NB.  Complex
tkn4=: ' a dr j'split
 0.6 0.6 0.6 1.1
 2.2 2.2 3.2 1
 3   3   4.2 4.2
 4.2 4.2 4.2 4.2
 4   4   4   4
)
numb4=: no`numb3`no`num...@.#@tkn4
numb4e=: numb4j`numb4ad`numb4ar sel (1;'j ad ar')
numb4j=: '(',nu...@{.,')j.',nu...@{:
numb4ar=: 'j./(',nu...@{.,')*1 2 o.',nu...@{:
numb4ad=: 'j./(',nu...@{.,')*1 2 o.180%~o.',nu...@{:

NB. other exponentials and extended precision
tkn5=: ' px'split
 0.6 1.1
 2.2 1
 3.2 3.2
 3   3
)
numb5=: no`numb4`extend`num...@.#@tkn5
numb5e=: numb5p`numb5x sel (1;'p x')
numb5p=: '(',nu...@{.,')*(o.1)^',nu...@{:
numb5x=: '(',nu...@{.,')*^',nu...@{:

NB. arbitrary bases
tkn6=: ' b'split
 0.6 1.1
 2.2 1
 3.2 3.2
 3   3
)
numb6=: no`numb5`no`num...@.#@tkn6
numb6e=: ('(',nu...@{.,')#.',signed) sel (1;'b')

NB. signed support for arbitrary bases
signed=: NO`arbitrary`('-',arbitrary)@....@tkn2@{:

NB. vectors
tkn7=: '/ 'split
 0   1.1
 0.2 1
)
numb7=: [:;('(',[,'),',])&.>/@:(numb6&.>)@tkn7

require'strings'
pc_rdm1=: nu...@tolower

-- 
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to