Here's a couple of expansions of inl,
lr_z_ =: 3 : '5!:5 < ''y'''
lrA_z_ =: 1 : '5!:5 < ''u'''
inlC_z_ =: 2 : 0
(([: <^:(0=L.) v"_) inl~ m , ' ', lr@:]) : (([: <^:(0=L.) v"_) inl~ (lr@:[), '
' ,m , ' ', lr@:] )
)
inlC2_z_=: 2 : 0
(([: <^:(0=L.) v"_) inl~ , '(' , u lrA , ') ', lr@:]) : (([: <^:(0=L.) v"_)
inl~ (lr@:[), ' (' , u lrA , ') ', lr@:] )
)
add_tbl_ =: ( 3 : 'coname '''' [ data =: data + y') : (4 : 'coname '''' [ data
=: x + y ')
t =. conew 'tbl'
t
┌─┐
│1│
└─┘
2 add__t i.5
┌─┐
│1│
└─┘
data__t
2 3 4 5 6
add inlC2 t
(([: <^:(0 = L.) (<,'1')"_) inl~ 'add' , ' ' , lr@:]) :(([: <^:(0 = L.)
(<,'1')"_) inl~ lr@:[ , ' ' , 'add' , ' ' , lr@:])
add inlC2 t 3
┌─┐
│1│
└─┘
data__t
5 6 7 8 9
2 add inlC2 t i.5
┌─┐
│1│
└─┘
data__t
2 3 4 5 6
'add' inlC t i.5
┌─┐
│1│
└─┘
data__t
2 4 6 8 10
inlC2 is not as safe as inlC because it relies on add not being defined in
caller, or at least sometimes is flakey in resolving the name.
tools to avoid the nightmare of quoting and nested quoting are possible.
lrA is the core of such tools, but there is also this fanciness
a_base_ =. 3
2 (a + 'data' inl add) inlC2 t i.5
5 6 7 8 9
a is early bound in above expression.
which makes an adverb that can be applied to a list of objects later,
late bound a version
2 t ((('a' inl <@('base'"_)) + 'data' inl add) inlC2) i.5
5 6 7 8 9
or simpler,
2 t ((3 : 'a_base_'@] +'data'inl add)inlC2) i.5
5 6 7 8 9
for efficiency though, raw inl is the most flexible. With the each keyword you
can even have separate code strings sent to each item in a list of objects.
The objects in list just have to be ducktyped to process
strinsert =: 1 : ' [ , u , ]'
2 ('data' inl t inl~ ' add 'strinsert/&lr) i.5
2 3 4 5 6
building multiple strings,
4 2 ' add 'strinsert/&lr each 3; i.5
┌───────┬───────────────┐
│4 add 3│2 add 0 1 2 3 4│
└───────┴───────────────┘
----- Original Message -----
From: Joe Bogner <[email protected]>
To: [email protected]
Cc:
Sent: Sunday, November 8, 2015 8:11 AM
Subject: Re: [Jprogramming] invoking object verbs without polluting global
namespace
thanks for the ideas.
Henry,
add__tbl 4 add__tbl 2
Is the right way to go except for two thoughts:
1. I was hoping to avoid the visual clutter of having __tbl on each call
2. A call could return a new instance of the object which would need
to be invoked against, that's why I was using CURRENT to resolve the
current instance in the chain (which can be changed by a verb)
Pascal,
inl is a useful. I tinkered with it and it could be a viable option
too. It will have a problem if a method returns a new instance of the
object I think
Aside from that, this is pretty close:
The main thing I dislike about is having to quote the expression,
because I may use quotes within that. I could use double quote (") as
the identifier and replace with double single quotes before executing
though.
'"foo" exp' would become '''foo'' exp'
Sidebar: The plot dsl is along the lines of what I would like to
accomplish but for another object. I was just interested in seeing if
the commands could be entered without being quoted and dispatched
appropriately without having a name in the global locale for each cmd.
coclass 'table'
create=: 3 : 0
data=:y
)
add=: 4 : 0
data=:data+x
)
sub=: 4 : 0
data=:data-x
)
to=: ''
coclass 'base'
inl_z_ =: (cocurrent@] ".@] [)"1 0
tbl=:((i.10) conew 'table')
'5 add 2 add to' inl tbl
tbl2=:((i.5) conew 'table')
'2 sub 1 add 2 add to' inl tbl2
Messing around with copath, it seems like I can refer to items in base too
'base' copath 'table'
copath 'table'
┌────┐
│base│
└────┘
A=: 5
'A sub 1 add 2 add to' inl tbl2
I'm still not sure this is a good idea but it's been interesting to
learn more about locales
On Sat, Nov 7, 2015 at 11:03 PM, 'Pascal Jasmin' via Programming
<[email protected]> wrote:
> my all time favorite J fork
>
> inl_z_ =: (cocurrent@] ".@] [)"1 0
>
> 'add 2' inl tbl
>
> Henry's suggestion makes more sense as you described your objects.
>
> Where inl is more convenient is if tbl is a list of objects, in which case
> inl executes the string in all of them.
>
>
> but if tbl is just one object, then my code is equivalent to
>
> add__tbl 2
>
>
> another way to keep the flow of your dsl, but losing to keyword
>
>
> add=: 4 : 0
> data__y =:data__y+x
> y
>
> )
>
> 2 add 2 add tbl
>
>
> ----- Original Message -----
> From: Joe Bogner <[email protected]>
> To: [email protected]
> Cc:
> Sent: Saturday, November 7, 2015 9:07 PM
> Subject: [Jprogramming] invoking object verbs without polluting global
> namespace
>
> I'm playing with the idea of a domain specific language against a
> object that will have a fair amount of state.
>
> The syntax will be something like
>
> 4 add 2 add to tbl
>
> or ideally
>
> add 4 add 2 to tbl
>
> I think the latter will have difficulty implementing but would be
> preferred if possible.
>
> There will be many verbs in the dsl (maybe 10-15).
>
> Below is a proof of concept that works fine. I think using the 'to' to
> set a global to the current locale is kind of hacky, but I can live
> with that.
>
> My bigger concern is making each of the table methods public in the z
> locale. Is there a way to dispatch them to the table/CURRENT locale
> without making clobbering other verbs in the z/base locale? My
> research suggests the answer is no, but was interested in any clever
> solutions. I'd also like to avoid adding many more keywords (such as
> modifier to intercept the call and convert it)
>
>
> Alternatively, I considered passing a string of the dsl and chopping
> it apart and interpreting it, but I liked the idea of avoiding the
> string argument if possible.
>
> Thanks
> Joe
>
>
>
> coclass 'table'
>
> create=: 3 : 0
> data=:y
> )
>
> add=: 4 : 0
> data=:data+x
> )
>
>
> coclass 'base'
>
>
>
> to =: 3 : 'CURRENT=: y'
> add =: 4 : 'x add__CURRENT '''''
>
> tbl=:(i.10) conew 'table'
>
> 2 add 2 add to tbl
>
> tbl2=:(i.5) conew 'table'
>
> 5 add to tbl2
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm