The standard answer to "Does J have a dictionary data type?" seems to be "Use symbols." (Two other answers today recommend locales.) To which we ask, "How do we use symbols?" Given that I may have overlooked a lab, I'll show my effort. My goal is to maintain a database of probabilities written in a standard long form. "P(a)" is an invalid j pronoun. I solve this with string keys mapped to symbols. J implements symbols with red-black trees. They're quickly accessed. The symbol values are indexes into a boxed list of values. This is my dict.ijs file. The note at the end gives example use. I've fancifully called the wikipedia "Encyclopedia Galactica", so the example refers to that of http://en.wikipedia.org/wiki/Posterior_probability .

coclass 'dict'

NB. implement associative array

VALUE =: 0$a:

Monad =: :[:
symbol =: s:^:('literal'-:datatype) Monad

NB. index'`literal'
index =: 6&(s: symbol) Monad

NB. may fetch multiple items, returns boxed items
get =: (index { ".@:('VALUE'"_)) Monad

NB. may fetch multiple items, opens the boxes!
openGet =: >@:get Monad

put =: dyad define
KEY =. symbol y
if. 1 (= #) KEY do.
  V =. < x
else.
  assert KEY =&# x
  V =. ;/ x
end.
I =. index KEY
L =. >: >./ I
if. L (> #) VALUE do. VALUE =: (+:L){.VALUE end.
VALUE =: V I} VALUE
KEY
)

cutSTRING =: #@:[ }.&.> , <;.1~ [ E. ,
assert ((,0);4 5 6 7) -: 1 2 3 cutSTRING i.8

embed =: >@:{.@:[ , ] , >@:{:@:[
assert 'bats' -: 'bs'embed'at'

Filter =: (#~`)(`:6)
assert 0 1 2 -: <&3 Filter i.6
assert 'SUPPER'-:<&(a.i.'a')Filter&.:(a.&i.)'SelectUPPERcase'

getAllSymbols =: _6 s: [: i. s:~@:0:

mp =: ($: |:@:+) :(+/ .*)

NB. probability of pants, from law of total probability

NB. y is the value for which to compute total probability
NB. totalProbability =: openGet :: (verb define)

NB. y is the value for which to compute total probability
totalProbability =: verb define
 NB. returns P(y|CONDITIONi)P(CONDITIONi)
 assert 'P()' -: 1 2 _1 { y
 CONDITIONAL_GOAL =. '|'_1}y
 SYMBOLS =. getAllSymbols''
 LITERALS =. ": SYMBOLS
 A =. ('`'~:{.&>) Filter CONDITIONAL_GOAL cutSTRING LITERALS
 B =. ({.~i.&')')&.>A  NB. B are boxes of conditionals.
 PYI =. openGet;('`P(';')')&embed&.>B
 PXYI =. openGet;(CONDITIONAL_GOAL;')')&embed&.>B
 assert PYI -:&# PXYI
 y put~ PYI mp PXYI
 openGet y
)

Note 'Encyclopedia Galactica: posterior probability'
   NB. wikipedia example on Tue Oct 22 15:18:33 EDT 2013
   load'dict.ijs'
   D=:conew'dict'

   NB. probability student is a boy, complementary girl
   ((,-.)0.6)put__D'`P(B)`P(G)'

   NB. probability wearing pants given sex
   1 0.5 put__D'`P(P|B)`P(P|G)'

   NB. compute and store probability to observe pants.
   EMPTY [ totalProbability__D'`P(P)'

   NB. Finally, Bayes Law.  Find P(G|P).
   %/ ; openGet__D'`P(P|G)`P(P)`P(G)'
0.25
)

Date: Mon, 3 Mar 2014 09:06:00 -0600
From: Yike Lu<yikelu.h...@gmail.com>
To:programm...@jsoftware.com
Subject: Re: [Jprogramming] Sorry state of slow parsing of JSON in J
Message-ID:
<CAO=ap+cujrjnshftlhipnyzrk2mfrav_wak+-qzbl5vigee...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

A tangential but related question: does J have a dictionary data type?
Where can I read about it?

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

Reply via email to