keyfiles looks interesting.  There is a keyfiles.txt in addons/data/jfiles, but 
no keyfiles.ijs.  Google couldn't help me either.

There is also a bigger and slightly different implementation done here.  
http://olegykj.sourceforge.net/scripts/map.htm


I've updated the utilities such that kvset will delete a key if given a 
empty/missing value, and kvgk will retrieve all keys that include x as an 
element.  This will fail to retrieve keys if the values are shaped (nested 
boxed) differently.

kvgi =: #@:{.@:]  _:`]@.> (boxopen@[ i.~  (0&{@:]))"0 _

kvadd =: 4 : 0 NB. assumes at least one row. preapply ,. if just one pair
i=. ({.x) kvgi y 
if. _=i do.  y ,."_ 1 x else. ,&(>}. x) L:0 amend (<1,i)  y end.
)
kvset =: 4 : 0 NB. assumes at least one row
i=. ({.x) kvgi y [ d=. (> }.x)
if. _=i do.  if. notEmpty d do.y ,."_ 1 x end. else. if. notEmpty d do. d"_ L:0 
amend (<1,i) y 
else. (i{."1 y) ,"1 (>:i)}."1  y end. end.
)
kvgv =: ((1&{@:]) {~  boxopen@[ i.~  (0&{@:]))"0 _ :: empty
kvgk =: ((0&{@:]) #~  >@: (+./L:0) @:([ e.~ L:0  (1&{@:])))"0 _ :: empty NB. 
keys that include element.  Fails if values contain nexted k,v
kvlabel =: boxopen@[ , boxopen@:]
notEmpty =: 0 < #@:]

amend_z_ =: 2 : 0  NB. v is n or n{"num
s=. v"_ y
(u (s{y)) (s}) y 
:
s=. v"_ y
(x u (s{y)) (s}) y 
)

h=. (1;6) kvset (5; 4) kvadd (5; 3 ) kvadd (5;2) kvadd (3;1) kvadd ('a';3) 
kvadd (3;4) kvadd 1 kvlabel 2
h2=. ('sidebar';'<') kvadd 'title' kvlabel < (('list2';3) kvset L:1 
(<'list')&kvlabel L:0) amend 3 amend 1 h
 (< 'sidebar') kvset h2
┌───────────────────────┐
│title                  │
├───────────────────────┤
│┌─┬───┬─┬─────────────┐│
││1│3  │a│5            ││
│├─┼───┼─┼─────────────┤│
││6│4 1│3│┌─────┬─────┐││
││ │   │ ││list │list2│││
││ │   │ │├─────┼─────┤││
││ │   │ ││2 3 4│3    │││
││ │   │ │└─────┴─────┘││
│└─┴───┴─┴─────────────┘│
└───────────────────────┘

   3 kvgk >5 kvgv >(<'title') kvgv h2
┌────┬─────┐
│list│list2│
└────┴─────┘


 ([ kvadd ,.@:])/ (<"0 ] i.20) ,.~ ,. (('not prime';'prime')  {~ (1&p:)) i.20
┌───────────────────┬─────────────────────────────┐
│prime              │not prime                    │
├───────────────────┼─────────────────────────────┤
│19 17 13 11 7 5 3 2│18 16 15 14 12 10 9 8 6 4 1 0│
└───────────────────┴─────────────────────────────┘


slow for massive data, but the ,. done to the whole kv data on each add in 
order to enable the '/' application causes much of that slowdown.

   ts '([ kvadd ,.@:])/ (<"0 ] i.1e5) ,.~ ,. ((''not prime'';''prime'')  {~ 
(1&p:)) i.1e5'
0.0564289/sec 24.0244MB
   ts '([ kvadd ,.@:])/ (<"0 ] i.1e4) ,.~ ,. ((''not prime'';''prime'')  {~ 
(1&p:)) i.1e4'
3.32603/sec 2.65766MB
   ts '([ kvadd ,.@:])/ (<"0 ] i.1e3) ,.~ ,. ((''not prime'';''prime'')  {~ 
(1&p:)) i.1e3'
39.745/sec 0.237952MB

on another note, is it possible to make the following adverb (PowerA) tacit?  
It just accumulates repeated application of u.  Or if there is a way to use '/' 
when x and (far right) y are of different shapes?

powerA =: 1 : 0
:
c =. y
while. 0<#x do.
a=. {. x
c =. a u c
x =. }. x
end.
c
)

   (;~"0  i.10) kvset powerA h
┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐
│1│3│a│5│0│2│4│6│7│8│9│
├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
│1│3│3│5│0│2│4│6│7│8│9│
└─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘

   ts '(;~"0  i.1e3) kvset powerA h'
2.82845/sec 0.630528MB

________________________________
From: bill lam <[email protected]>
To: [email protected] 
Sent: Friday, September 27, 2013 12:46:30 AM
Subject: Re: [Jprogramming] some key,value utilties


You may also interested to know J has jfile/keyfile/kfile
packages either in its base library or as addons. They use
3!:1/3!:2 so perhaps more efficient.

Чт, 26 сен 2013, Pascal Jasmin писал(а):
> I noticed that some of you have been looking for or talked about a YAML 
> implementation.  J actually has a great portable format built in with 5!:5, 
> though maybe there needs a human readable output that could be as simple as 
> indenting after <.
> 
> Here are some verbs for working with key,value data in trees.  They're pretty 
> forgiving about boxing parameters, and intended mostly for display and small 
> datasets.
> 
> NB. key value boxed data.  All verbs use y as the kv store, and x as the 
> query/new val
> kvgi =: #@:{.@:]  _:`]@.> (boxopen@[ i.~  (0&{@:]))"0 _  NB. get index
> kvadd =: 4 : 0 NB. assumes at least one row.  Appends if key exists
> 
> i=. ({.x) kvgi y 
> if. _=i do.  y ,."_ 1 x else. ,&(>}. x) L:0 amend i amend 1 y end.
> )
> kvset =: 4 : 0 NB. assumes at least one row.  Replaces if key exists
> i=. ({.x) kvgi y 
> if. _=i do.  y ,."_ 1 x else. (> }.x)"1 L:0 amend i amend 1 y end.
> )
> kvgv =: ((1&{@:]) {~  boxopen@[ i.~  (0&{@:]))"0 _ :: empty  NB. get value
> kvgk =: ((0&{@:]) {~  boxopen@[ i.~  (1&{@:]))"0 _ :: empty  NB. get first key
> kvlabel =: boxopen@[ , boxopen@:]                            NB. turns value 
> into k,v
> 
> 
> 
>    ] h=. (1;6) kvset (5; 4) kvadd (5; 3 ) kvadd (5;2) kvadd (3;1) kvadd 
> ('a';3) kvadd (3;4) kvadd 1 kvlabel 2
> ┌─┬───┬─┬─────┐
> │1│3  │a│5    │
> ├─┼───┼─┼─────┤
> │6│4 1│3│2 3 4│
> └─┴───┴─┴─────┘
>    5 kvgv h
> ┌─────┐
> │2 3 4│
> └─────┘
>  
>   (('list2';3) kvset L:1 (<'list')&kvlabel L:0) amend 3 amend 1 h
> ┌─┬───┬─┬─────────────┐
> │1│3  │a│5            │
> ├─┼───┼─┼─────────────┤
> │6│4 1│3│┌─────┬─────┐│
> │ │   │ ││list │list2││
> │ │   │ │├─────┼─────┤│
> │ │   │ ││2 3 4│3    ││
> │ │   │ │└─────┴─────┘│
> └─┴───┴─┴─────────────┘
> 
>    (<5) kvgv (('list2';3) kvset L:1 (<'list')&kvlabel L:0) amend 3 amend 1 h
> ┌─────────────┐
> │┌─────┬─────┐│
> ││list │list2││
> │├─────┼─────┤│
> ││2 3 4│3    ││
> │└─────┴─────┘│
> └─────────────┘
> 
> 
> ] h2=. ('sidebar';'<') kvadd 'title' kvlabel < (('list2';3) kvset L:1 
> (<'list')&kvlabel L:0) amend 3 amend 1 h
> ┌───────────────────────┬───────┐
> │title                  │sidebar│
> ├───────────────────────┼───────┤
> │┌─┬───┬─┬─────────────┐│<      │
> ││1│3  │a│5            ││       │
> │├─┼───┼─┼─────────────┤│       │
> ││6│4 1│3│┌─────┬─────┐││       │
> ││ │   │ ││list │list2│││       │
> ││ │   │ │├─────┼─────┤││       │
> ││ │   │ ││2 3 4│3    │││       │
> ││ │   │ │└─────┴─────┘││       │
> │└─┴───┴─┴─────────────┘│       │
> └───────────────────────┴───────┘
> 
>    5 kvgv > (<'title') kvgv h2
> ┌─────────────┐
> │┌─────┬─────┐│
> ││list │list2││
> │├─────┼─────┤│
> ││2 3 4│3    ││
> │└─────┴─────┘│
> └─────────────┘
> 
>    5!:5 <'h2'
> 2 2$(<'title'),(<'sidebar'),(<2 4$(<1),(<3),(<'a'),(<5),(<,6),(<4 1),(<3),<2 
> 2$'list';'list2';2 3 4;3),<'<'
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm

-- 
regards,
====================================================
GPG key 1024D/4434BAB3 2008-08-24
gpg --keyserver subkeys.pgp.net --recv-keys 4434BAB3
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to