forgot the definition of amend
amend_z_ =: 2 : 0
s=. v"_ y
(u (s{y)) (s}) y
:
s=. v"_ y
(x u (s{y)) (s}) y
)
________________________________
From: Pascal Jasmin <[email protected]>
To: "[email protected]" <[email protected]>
Sent: Friday, September 27, 2013 12:17:20 AM
Subject: [Jprogramming] some key,value utilties
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
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm