Remember, The need for private multiline functions?
http://www.jsoftware.com/pipermail/general/2006-April/026718.html

I have some fresh insight after looking at R example,

  > open.account <- function(total) {
    list(deposit = function(amount) {
        if (amount <= 0) stop("Deposits must be positive!\n")
        total <<- total + amount
        cat(amount, "deposited. Your balance is", total, "\n\n")
    }, withdraw =  .... [TRUNCATED] 

  > ross <- open.account(100)

  > ross$withdraw(30)
    30 withdrawn.  Your balance is 70 
  > ross$balance()
    Your balance is 70 

This pattern of declaring a family of functions or
a whole class within one function is common for many
scripting languages, such as JavaScript, and is sometimes
favored for compactness and reduction name/objects clutter over
even existing more traditional alternatives of separate definitions.

The previous thread quickly became boring with variantions
of the adverb theme. So here's what I think a different
more natural looking approach: transformational (Chomskyan). It roots
from experience with JHP and SQLite. The undercurrent for it
is the distinction between most other (semi)interpretive 
languages (JavaScript, Python, Ruby, SmallTalk) which use
a LISP based transitional form between text and execution
(the parse tree), and APL based languages such as J which go
directly from text to execution for expressions and explicit verbs
(or at least percieved as such, see end of message *).

So instead of transforming the parse tree, the text of
the definition can be manipulated.

For an example of injecting prolog and epilog code text 
around verbs see
  D:\Math\j601\addons\jhp\examples\phrasedb\phrdb.ijs

where the new compact definitions looks like

   getGroups=: verb sdefine
     r=. query__db sqlGroups
   )

and a try/catch with error handling is injected for "sdefine".

In JHP the whole .jhp page definition becomes a single
verb with verbatim static content injected as print
commands.


So the point is: why not use a similar textual transformation
to process the extended explicit definition format into
something acceptable by standard definition (3 : n).
For example,

BigVerb=: verb muldef
  privC=. 2 : 0
    privC sent1
    privC sent2
  )
  privV=. 3 : 0
    privV sentences
  )
  BigVerb stuff here...
)

into eqvivalent of

BigVerb=: 3 : 0
  privC=. 2 : ('privC sent1';'privC sent2')
  privV=. 3 : ('privV sentences';...)
  BigVerb stuff here...
)


_______
  * It turns out, explicit definitions can be manipulated similar
to LISP (executable) structures. Here we reassemble an explicit
definition using explicit representation. Obviously, transformations
are possible at this control-grained level.

   v1=: 3 : ('if.y do.a=.123';'a+y end.')
   {:"1]1 (5!:7)<'v1'
+---+-+---+------+---+----+
|if.|y|do.|a=.123|a+y|end.|
+---+-+---+------+---+----+
   3 : ({:"1]1 (5!:7)<'v1')
3 : 0
if.
y
do.
a=.123
a+y
end.
)



 
____________________________________________________________________________________
Yahoo! Music Unlimited
Access over 1 million songs.
http://music.yahoo.com/unlimited
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to